Simplest way to do two-finger rotation on Android
private MotionEvent mLastMultitouchEvent = null;
if (mLastMultitouchEvent == null) {
mLastMultitouchEvent = MotionEvent.obtain(event);
} else {
float currentRotation = rotation(event);
float prevRotation = rotation(mLastMultitouchEvent);
CGPoint rotationFocus = midpoint(event);
mGestureListener.onRotate(event, mLastMultitouchEvent, currentRotation - prevRotation, rotationFocus);
mLastMultitouchEvent = MotionEvent.obtain(event);
}
private float rotation(final MotionEvent event) {
double deltaX = (event.getX(0) - event.getX(1));
double deltaY = (event.getY(0) - event.getY(1));
double radians = Math.atan2(deltaY, deltaX);
return (float) Math.toDegrees(radians);
}
private CGPoint midpoint(final MotionEvent event) {
double x = (event.getX(0) + event.getX(1)) / 2.0;
double y = (event.getY(0) + event.getY(1)) / 2.0;
return CGPoint.make((float) x, (float) y);
}
Written by Anuraag Sridhar
Related protips
1 Response
Sorry, I'm new to this site; how do I put my code in a code block like yours on a protip? I tried [code] and <code.> tags but neither gave the correct result.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Java
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#