检查在 Motion 事件 android 中抛出的方向 [英] Check which direction was flinged in a Motion event android

查看:21
本文介绍了检查在 Motion 事件 android 中抛出的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想检测用户在触摸屏幕时移动手指的方向现在它在 3 个方向上工作,但没有调用向上"运动.

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.

这是我的代码:

@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.

推荐答案

您的移动计算有误.我已经修好了,现在可以了.

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

            }

这篇关于检查在 Motion 事件 android 中抛出的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆