检查哪个方向在运动事件中的Andr​​oid flinged [英] Check which direction was flinged in a Motion event android

查看:112
本文介绍了检查哪个方向在运动事件中的Andr​​oid flinged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想检测触摸屏幕时,哪个方向,用户移动他的手指
现在它的工作了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.

这是我的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.

推荐答案

您在您的移动计算有错误。我现在已经修好了,其确定。

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

            }

这篇关于检查哪个方向在运动事件中的Andr​​oid flinged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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