Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyActivity extends Activity implements View.OnTouchListener {
- // Angle between pivot point and initial point of contact with the button
- private float downRadians = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.my_activity);
- // Links button to XML layout and intercepts touches
- Button button = (Button) findViewById(R.id.bt_hello_button);
- button.setOnTouchListener(this);
- }
- }
- @Override
- public boolean onTouch(View view, MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- // Angle between the point where we first touch the view and the pivot point
- downRadians = (float) Math.atan2((event.getY() - view.getPivotY()), (event.getX() - view.getPivotX()));
- break;
- case MotionEvent.ACTION_MOVE:
- // Angle between the point the finger has moved to and the pivot point
- float moveRadians = (float) Math.atan2((event.getY() - view.getPivotY()), (event.getX() - view.getPivotX()));
- // Determines the change in rotation from when we first touched the view
- float deltaRadians = moveRadians - downRadians;
- // Rotates the button by the difference in rotation (in degrees)
- float deltaDegrees = (float) (180f * deltaRadians / Math.PI);
- view.setRotation(view.getRotation() + deltaDegrees);
- break;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement