I have an ImageView that you can use to create a finger with one finger or with two fingers. It is working fine. I expanded it to handle rotation, and its behavior causes some confusion.
When an event with multiple touches occurs, this method is called, which should return the angle of rotation in degrees. It does not do what I expect.
private int fingersAngle(MotionEvent event) { float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); int degrees = (int)(Math.toDegrees(Math.atan2(y, x))); return degrees; }
When I rotate two fingers, I expect the exits ...
158 166 168 169 174 176 179 181 etc.
But what I actually get is more like this:
158 166 -179 179 -179 179 -179
The problem seems to be related to the symptoms. How does this method know if it will be 180 or -180, or 90 or -270? The image often rotates in the wrong way, and then suddenly jumps and starts to rotate in the opposite way. Worse, the direction it initially rotates is random.
I am testing the application using Nexus One, but also see the same problem on the Advent Vega tablet. Both devices work fine with Google Maps in 3D to rotate the screen (if sometimes jumping a little), so the data does not indicate a hardware limitation.
The second problem is that when two fingers are approximately vertically or horizontally aligned, the angle simply does not change, so the rotation “sticks” about 10-20 degrees up / down / left / right.
I'm currently doing a check to see if the angle has suddenly changed, and if so, subtract it from 360. Ugly, damn it, but it helps a little.
Hopefully one of you has seen this before and knows how to extract angular rotation from multi-touch gestures.
Some things on Android are so light that it's awesome, but such things are very painful.