【问题标题】:MotionEvent.Action_UP is always calledMotionEvent.Action_UP 总是被调用
【发布时间】:2015-02-16 00:43:18
【问题描述】:

这是我的 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 总是被调用,无论我是否真的将手指从屏幕上抬起。我在调试模式下运行它,同时在案例 MotionEvent.ACTION_UP 上放置一个断点,当我按下(并且没有释放)时,它被调用了。有人可以解释为什么会这样吗?

【问题讨论】:

    标签: android motionevent


    【解决方案1】:

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

    希望对你有帮助……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 2012-02-19
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多