[英]Java Android : onTouchListener release?
Java/android noob here, I have a problem where using onTouchListener I change the variable x to x+1 and that works fine, but when I release I would like the x variable to decrease in value until it hits zero, like so: Java / android noob在这里,我遇到一个问题,即使用onTouchListener将变量x更改为x + 1并可以正常工作,但是当我释放时,我希望x变量的值减小直至其达到零,如下所示:
Imageview image = (ImageView) findViewById(R.id.fgView);
image.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
mode=1;
break;
case MotionEvent.ACTION_UP:
mode=2;
break;
case MotionEvent.ACTION_POINTER_UP:
mode2=2;
break;
case MotionEvent.ACTION_MOVE:
mode=1;
break;
case MotionEvent.ACTION_POINTER_DOWN:
mode2=1;
break;
}
}
if(mode==1){
x++;
}
if(mode==2){
x * 0.9
}
//mode2 is filler
}
Now that works fine, my problem comes when I want my x to continue to decrease after release, but since it's under setOnTouchListener then it doesn't work. 现在可以正常工作了,当我希望我的x在发行后继续减少时,我的问题就来了,但是由于它在setOnTouchListener下,因此它不起作用。 Maybe something like this:
也许是这样的:
public boolean onTouchFalse(View v){
if(x>0){
x * 0.9
}
}
or even something like 甚至像
public boolean image.isOnScreen{
if(x>0){
x = x * 0.9
}
}
tl;dr: I'm looking for some kind of listener that I could use to depreciate my x. tl; dr:我正在寻找某种可以用来折旧x的监听器。
Is there a listener or some class i'm missing? 我是否缺少听众或某个班级? Thanks for any and all help!
感谢您提供的所有帮助!
final Handler handler = new Handler();
image.post( new Runnable() {
@Override
public void run() {
x--;
handler.postDelayed(this, 100);
}
});
** Note that the 100 is how long in between each subtraction (in millisecs) **请注意,100是每个减法之间的间隔时间(以毫秒为单位)
There are no touch events if you're not touching the screen. 如果您不触摸屏幕,则没有触摸事件。 Events need a trigger, maybe a timer could help.
事件需要触发器,也许计时器可以提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.