Android 始终调用MotionEvent.Action\u UP

Android 始终调用MotionEvent.Action\u UP,android,motionevent,Android,Motionevent,以下是我的onTouchEventMotionEvent事件的代码: @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 =

以下是我的onTouchEventMotionEvent事件的代码:

@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;
}

这里的问题是,无论我是否真的将手指从屏幕上抬起,动作总是被调用。我在调试模式下运行它,同时在case MotionEvent.ACTION\u上放置一个断点,当我按下并没有释放时,它被调用。有人能解释一下为什么会发生这种情况吗?

我分析了您的代码,我认为这只是因为您没有在Action\u Down和Action\u 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;

我希望它能帮助您……

我分析了您的代码,我认为这只是因为您没有在Action\u Down和Action\u 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;
我希望它能帮助你