Skip to content

Instantly share code, notes, and snippets.

@fdoyle
Created October 16, 2013 21:47
Show Gist options
  • Save fdoyle/7015476 to your computer and use it in GitHub Desktop.
Save fdoyle/7015476 to your computer and use it in GitHub Desktop.
fancy button animation scales when pressed, tilts based on finger movement
b.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
float touchPositionX = motionEvent.getX();
float touchPositionY = motionEvent.getY();
float touchNormalX = touchPositionX / b.getWidth()-.5f;
float touchNormalY = touchPositionY / b.getHeight()-.5f;
touchNormalX = Math.max(Math.min(touchNormalX, .5f), -.5f);
touchNormalY = Math.max(Math.min(touchNormalY, .5f), -.5f);
switch(motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
b.animate().scaleX(.9f).scaleY(.9f).setDuration(300).rotationY(touchNormalX * 20).rotationX(-touchNormalY * 20).start();
break;
case MotionEvent.ACTION_MOVE:
b.animate().rotationY(touchNormalX * 20).rotationX(-touchNormalY * 20).setDuration(0).start();
break;
case MotionEvent.ACTION_OUTSIDE:
break;
case MotionEvent.ACTION_UP:
b.animate().scaleX(1).scaleY(1).setDuration(300).rotationX(0).rotationY(0).start();
break;
}
return false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment