检查Motion事件中的滑动方向在Android中是什么。

3
我希望能检测用户在触摸屏幕时手指移动的方向。目前已经可以检测三个方向,但"上"方向的移动没有被调用。
以下是我的代码:
@Override
public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
    // store the X value when the user's finger was pressed down
    downXValue = event.getX();
    downYValue = event.getY();
    break;
}

case MotionEvent.ACTION_UP: {
    // Get the X value when the user released his/her finger
    float currentX = event.getX();
    float currentY = event.getY();
        //check if horizontal or vertical movement was bigger
    if (Math.abs(downXValue - currentX) > Math.abs(downYValue)
        - currentY) {
    Log.e("motionevent", "x");
    // going backwards: pushing stuff to the right
    if (downXValue < currentX) {
        Log.e("motionevent", "right");

    }

    // going forwards: pushing stuff to the left
    if (downXValue > currentX) {
        Log.e("motionevent", "left");

    }

    } else {
    Log.e("motionevent", "y");
    if (downYValue < currentY) {
        Log.e("motionevent", "up");

    }
    if (downYValue > currentY) {
        Log.e("motionevent", "down");

    }
    }
    break;
}
}

    return true;
}

检测水平或垂直移动时是否存在问题?因为每当我向上移动时,右侧或左侧会被调用。向下运动正常。

2个回答

17

您的移动计算有误。我已经修正了它,现在可以了。

switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN: {
                    // store the X value when the user's finger was pressed down
                    downXValue = event.getX();
                    downYValue = event.getY();
                    Log.v("", "= " + downYValue);
                    break;
                }

                case MotionEvent.ACTION_UP: {
                    // Get the X value when the user released his/her finger
                    float currentX = event.getX();
                    float currentY = event.getY();
                    // check if horizontal or vertical movement was bigger

                    if (Math.abs(downXValue - currentX) > Math.abs(downYValue
                            - currentY)) {
                        Log.v("", "x");
                        // going backwards: pushing stuff to the right
                        if (downXValue < currentX) {
                            Log.v("", "right");

                        }

                        // going forwards: pushing stuff to the left
                        if (downXValue > currentX) {
                            Log.v("", "left");

                        }

                    } else {
                        Log.v("", "y ");

                        if (downYValue < currentY) {
                            Log.v("", "down");

                        }
                        if (downYValue > currentY) {
                            Log.v("", "up");

                        }
                    }
                    break;
                }

            }

如果您认为这是正确的答案,请给我点赞并标记为正确答案。 - Chinmoy Debnath
是的,我在这一行代码if (Math.abs(downXValue - currentX) > Math.abs(downYValue)-currentY)中有一个括号位置错误,并且下上混淆了,谢谢! - Yalla T.

3
你可以使用GestureDetector.OnGestureListener接口来检测触摸事件,它提供了几种方法:滚动、快速滑动等。
用法:
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (detector == null) {
        detector = new GestureDetector(this);
    }
    return detector.onTouchEvent(event);
}

从现在开始,每个事件都将被识别并传递到指定的方法中。


对于我需要的小事情,我并不想使用GestureDetector(它让我感到非常困惑)。我的代码可以检测,但似乎存在逻辑问题,所以MotionEvent中的“up”无法正常工作。而且我认为即使我使用了GestureDetector,在他的onFling方法中仍然需要检查抛掷的方向。 - Yalla T.
做一些数学怎么样:斜率 = (y2 - y1)/(x2 - x1) 的正切 - S.D.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接