3

所以我想检测用户在触摸屏幕时移动手指的方向现在它适用于 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;
}

检查水平或垂直移动是否有问题?因为每当我向上移动时,都会调用右或左。下来工作正常。

4

2 回答 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;
                }

            }
于 2012-09-06T11:20:45.727 回答
3

您可以使用GestureDetector.OnGestureListener提供多种方法来检测触摸事件的接口:滚动、滑动等。

用法:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (detector == null) {
        detector = new GestureDetector(this);
    }
    return detector.onTouchEvent(event);
}

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

于 2012-09-06T10:44:57.737 回答