spentak
spentak

Reputation: 4727

Android Pinch/Two Finger rotation on view

On iPhone I simply do this (and it works flawlessly):

view.transform = CGAffineTransformRotate(transform, [(UIRotationGestureRecognizer *)recognizer rotation]);

I of course am expecting to do more work on the Android side (as usual). I noticed that Android does not have a two finger rotation gesture detector. Any thoughts of how I could implement such behavior?

This is what I use for my pinch scale detector:

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();

        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

        invalidate();
        return true;
    }
}

Upvotes: 3

Views: 2721

Answers (1)

John Ericksen
John Ericksen

Reputation: 11113

Out of the box, Android does not have a Rotate Gesture Detector.

What you can do, however, is calculate rotation from a two point touch event. Just determine a fulcrum for your rotation (either the center point of your 2 touch points, or one of the touch points) and determine the angle delta between the first touch event and the nth touch event. You can then rotate the given Canvas by that amount using the rotate() method.

In fact, this may be pretty easy to write your own 'Gesture Detector' for.

Upvotes: 4

Related Questions