Find the angle and speed for a custom touch event around a circle on Android

Mats again came and defeated me. I need your help to regroup and attack again.

What I have:

enter image description here

I have a surface view and a circle as a bitmap. I need to rotate the bitmap when the user moves his finger along the edge of the circle. The faster the user slides, the more I need to rotate the image. It seems simple, but not very easy to implement.

What I need

I need to calculate the angle of rotation of an image for an onDraw event. From what I have been thinking so far, I need two things: - the angle between the new touching point and the old one. I performed a simple function that will take care of this:

private int getAngleBetweenTwoTouchedPoints(double oldX, double oldY, double newX, double newY) { return (int) Math.abs(Math.atan2(newY - oldY, newX - oldX)); } 

the angle returned by this varies from 0 to 1, and I believe that this is correct. Increasing the angle of the image rotation matrix by this value, I get a slow rotation, mainly by 1 unit. So, there is a chance to work, but so far not in order. - So, secondly, I may need the speed at which the user slides a finger across the screen. So basically something like this:

rotationAngle + = angleBetweenTouches + speed

Speed ​​or speed, as I called it, is a problem in my case, given that I am not moving only in X or Y, but in a circle. I do not know how to calculate it. I saw VelocityTracker on Android help, but I don't know how this could help.

So, to repeat: I need to be able to rotate the image when the user moves his finger around the border of the image. Even easier, when the user stops sliding, the pixel of the image under the finger on the slide should be the same when the slide stops.

Any help is appreciated. Thanks you

+6
source share
2 answers

I suspect that the given β€œIt's even easier when the user stops sliding, the pixel of the image that was below the finger when the slide started should be the same when the slide stops.” What could be better for you with absolute rotations rather than relative ones.

The increment of rotation with each event can accumulate errors, which lead to the fact that the bitmap does not appear "stick" on the finger.

If you just keep track of the start event and the last event, always move from the original view to what is suitable for the last event, your code will be simpler (avoiding speed) and behave better.

+1
source

I had the same task, but for a different purpose with angles. I hope this link can give you some hint http://www.andengine.org/forums/post9226.html#p9226

By time difference, did you try to get getTime () from the Date class between two different clicks?

Hope this gives you some idea.

+2
source

Source: https://habr.com/ru/post/886855/


All Articles