Advertisement
Guest User

Untitled

a guest
Sep 20th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. public class MyActivity extends Activity implements View.OnTouchListener {
  2.  
  3. // Angle between pivot point and initial point of contact with the button
  4. private float downRadians = 0;
  5.  
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.my_activity);
  10.  
  11. // Links button to XML layout and intercepts touches
  12. Button button = (Button) findViewById(R.id.bt_hello_button);
  13. button.setOnTouchListener(this);
  14. }
  15. }
  16.  
  17. @Override
  18. public boolean onTouch(View view, MotionEvent event) {
  19. switch (event.getAction()) {
  20. case MotionEvent.ACTION_DOWN:
  21. // Angle between the point where we first touch the view and the pivot point
  22. downRadians = (float) Math.atan2((event.getY() - view.getPivotY()), (event.getX() - view.getPivotX()));
  23. break;
  24.  
  25. case MotionEvent.ACTION_MOVE:
  26. // Angle between the point the finger has moved to and the pivot point
  27. float moveRadians = (float) Math.atan2((event.getY() - view.getPivotY()), (event.getX() - view.getPivotX()));
  28.  
  29. // Determines the change in rotation from when we first touched the view
  30. float deltaRadians = moveRadians - downRadians;
  31.  
  32. // Rotates the button by the difference in rotation (in degrees)
  33. float deltaDegrees = (float) (180f * deltaRadians / Math.PI);
  34. view.setRotation(view.getRotation() + deltaDegrees);
  35. break;
  36. }
  37. return false;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement