Last Updated: February 25, 2016
·
4.961K
· anuraags

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);
}

1 Response
Add your 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 ·