Check which direction was flinged in a Motion even

2019-05-14 22:45发布

问题:

So i want to detect which direction the user moved his finger when touching the screen Right now its working for 3 directions but the "up" movement does not get called.

This is my code:

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

Is there a Problem with checking for horizontal or vertical movement? because whenever i do an up movement, right or left gets called. down works fine.

回答1:

You have error in your movement calculation. I have fixed it, its ok now.

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

            }


回答2:

You can use GestureDetector.OnGestureListener interface which provides several methods to detect touch events: scroll, fling, etc.

Usage:

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

From now every events are recognized and passed to the specified method.