Reputation: 51
My code is the following:
ss.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int loucodecatuaba = 0;
if (event.getAction()==MotionEvent.ACTION_DOWN){
redBlock.setY(event.getY()-(imagewidth));
redBlock.setX(event.getX()-(imageheight));
return true;
}
if (event.getAction()==MotionEvent.ACTION_UP){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
redBlock.setX(centroX);
redBlock.setY(centroY);
}
return false;
}
})
What I want is for the program to wait for the 1000 milliseconds unless there is another click, in which case it should go back to the first if loop ( the one with "event.getAction()==MotionEvente.ACTION_DOWN")
Upvotes: 1
Views: 148
Reputation: 51
I found a way to do that better, is more like a version of mister MeLine code.
final Thread dois = new Thread(){
public void run() {
try{
Thread.sleep(10000);
redBlock.setX(centroX);
redBlock.setY(centroY);
}catch (InterruptedException e){
}
}
};
ss.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, final MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN){
dois.interrupt();
redBlock.setY(event.getY()-(imagewidth));
redBlock.setX(event.getX()-(imageheight));
return true;
}
if (event.getAction()==MotionEvent.ACTION_UP){
dois.start();
}
return false;
}
});
}
By creating the thread outside the onTouchListener I can set a timer and stop and restart it better.
Upvotes: 1
Reputation: 3421
You should start a new thread and and should have a global boolean value isWaitingStarted.
boolean isWaitingStarted = false;
...
if (event.getAction()==MotionEvent.ACTION_UP){
if(!isWaitingStarted){
isWaitingStarted = true;
Thread one = new Thread() {
public void run() {
try {
Thread.sleep(1000);
setRedBlock();
} catch(InterruptedException v) {
}
}
};
one.start();
}
} else {
setRedBlock();
}
Try to declare a method:
public void setRedBlock() {
redBlock.setX(centroX);
redBlock.setY(centroY);
isWaitingStarted = false;
}
Upvotes: 0