I’m programming a game in which as I touch the screen a ball follows my movement.
My touch code resembles this:
...
case MotionEvent.ACTION_MOVE:
ballX = currentX;
ballY = currentY;
invalidate();
And naturally I handle the OnDraw event and drawing the Oval with drawOval.
As the application starts in the simulator all works as instructed but it seem that by increasing the velocity of the touch (mouse) the ball moves with a very high latency so that as I stop to move, I should wait up to 1 sec while the ball reaches the last position. Is there a way to increase the frequency of the ACTION_MOVE events to fire faster or to improve the overall behavior of this program ?
The situation you’re describing typically indicates that you’re receiving the ACTION_MOVE events faster than you’re handling them. This causes the events to be queued up and handled after you lift your finger. The source for your problem is, most probably, that the call to invalidate() takes a lot of time.
A possible solution to your problem can be to handle a ACTION_MOVE event once every N events:
In the example above, you’ll handle the ACTION_MOVE events every 5 samples. Since invalidate() is called less frequently it can better track your finger’s motion in real-time, instead of wasting processing time on past, and irrelevant, events.