gpt4 book ai didi

android - MotionEvent.Action_UP 总是被调用

转载 作者:行者123 更新时间:2023-11-29 15:13:00 26 4
gpt4 key购买 nike

这是我的 onTouchEvent(MotionEvent 事件)的代码:

@Override
public boolean onTouchEvent(MotionEvent event){
int action = event.getAction();
int x = Math.round(event.getX());
int y = Math.round(event.getY());
//play is an imageView
int viewLeft = play.getLeft();
int viewRight = play.getRight();
int viewTop = play.getTop();
int viewBottom = play.getBottom();
boolean onPoint = false;

switch (action) {
case MotionEvent.ACTION_DOWN:
//Checks if the touched area falls within the imageView play
if ((x >= viewLeft && x <= viewRight) && (y >= viewTop && y <= viewBottom)) {
onPoint = true;
play.setImageResource(R.drawable.playyellow);
}
case MotionEvent.ACTION_UP:
//if the finger is lifed on the imageView, the intent is called
play.setImageResource(R.drawable.play1);
if (onPoint) {
if ((x >= viewLeft && x <= viewRight) && (y >= viewTop && y <= viewBottom)) {
Intent i = new Intent(this, InGame.class);
startActivity(i);
}
}

}
return true;
}

这里的问题是 ACTION_UP 总是被调用,无论我是否真的将手指从屏幕上移开。我在 Debug模式下运行它,同时在案例 MotionEvent.ACTION_UP 上放置一个断点,当我按下(但没有释放)时,它被调用了。有人可以解释为什么会这样吗?

最佳答案

我分析了您的代码,我认为这只是因为您没有在 Action_Down 和 Action_Up 情况之间插入中断。

试试下面的代码......

switch (action) {
case MotionEvent.ACTION_DOWN:
//Checks if the touched area falls within the imageView play
if ((x >= viewLeft && x <= viewRight) && (y >= viewTop && y <= viewBottom)) {
onPoint = true;
play.setImageResource(R.drawable.playyellow);
}
break;
case MotionEvent.ACTION_UP:
//if the finger is lifed on the imageView, the intent is called
play.setImageResource(R.drawable.play1);
if (onPoint) {
if ((x >= viewLeft && x <= viewRight) && (y >= viewTop && y <= viewBottom)) {
Intent i = new Intent(this, InGame.class);
startActivity(i);
}
}

}
return true;

希望对你有帮助...

关于android - MotionEvent.Action_UP 总是被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28533003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com