Reputation: 453
I want to rotate my object when I PRESS_DOWN button and stop rotating when I PRESS_UP.
I tried to do it with OnTouchListener but seems there is no way to kmow that button is still pressed.
For example I touch button 1st time, and there is 1st touch event raised with ACTION_DOWN, but then if I keep my finger not moving nothing will happen (no more events). Or if I will move it very fast there will be different speed of raising on touch event ACTION_MOVE. Different speed that depends on finger's speed moves is unacceptable for my task. So I decided to start timer (that do rotating with fixed speed) when ACTION_DOWN raised and to cancel TimerTask when ACTION_UP is raised, it works good, just like I need. But I think it's not the best solution or even the worst. Please give me an advice of other possible solutions.
Upvotes: 0
Views: 86
Reputation: 192
Using timer is a good way. To my knowledge, I think using a simple if-else may solve your problem more efficiently. For example,
boolean state = true;
if(state)
{
// do the code for ACTION_DOWN
}
else
{
// do the code for ACTION_UP
state = true;
}
Give a loop to this if statement, as many times you want. But I don't know, whether this solution meets your requirements, or not.
Upvotes: 1
Reputation: 176
Your solution of using a timer is good, and is what you are supposed to you. If you are running an animation, you cannot rely on UI events, you have to have a separate thread or timer for periodic updates.
Upvotes: 1