全方位滑动手势(向左,向右,向上,向下) [英] Swipe gesture in all direction(Left,Right, Up, Down )

查看:241
本文介绍了全方位滑动手势(向左,向右,向上,向下)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在代码中检测滑动方向.我可以检测到方向,但是就像我在右上右或左上滑动时一样.对于左侧相同,我的要求是如果我向左滑动,则无需举起手指,它应该只能向左移动,所有方向都一样.谁能帮我吗.预先感谢!

I need to detect swipe direction in my code. I can detect the direction but it comes like if I swipe right top right or left-top like that coming. same for the left, my requirement is without lifting finger if i swipe left it should come only left ,likewise all the direction. Can anyone help me out. Thanks in advance!

@Override 布尔布尔onTouchEvent(MotionEvent touchevent){

@Override public boolean onTouchEvent(MotionEvent touchevent) {

    switch (touchevent.getAction()) {
        // when the user first touches the screen we get x and y coordinate
        case MotionEvent.ACTION_DOWN: {
            x1 = touchevent.getX();
            y1 = touchevent.getY();
            break;
        }

        case MotionEvent.ACTION_MOVE: {


            x2 = touchevent.getX();
            y2 = touchevent.getY();


            float deltaX = x2 - x1;


            if (Math.abs(deltaX) > MIN_DISTANCE) {
                // Left to Right swipe action
                if (x2 > x1) {;
                    Log.e("RTL", "Right to Left Swap Performed");
                }

                else {
                    Log.e("LTR", "Left to Right Swap Performed");
                }

            } else {

                if (y2 > y1) {

                    Log.e("UTD", "UP to Down Swap Performed");
                }

                // Right to left swipe action
                else {
                    Log.e("DTU", "Down to UP Swap Performed");
                }
            }
        }
    }
    return false;
}

推荐答案

您应该计算角度,然后再确定角度.如果矢量大于最小距离值,则可以检查角度.

You should calculate angle then decide with that. If the vector is bigger than your min distance value, then you can check for angle.

float angle = (float) Math.toDegrees(Math.atan2(y2 - y1, x2 - x1));
if(angle > 45 && angle <= 135) return UP;
else if(angle > 135 && angle <= 225) return LEFT;
else if(angle > 225 && angle <= 315) return DOWN;
else return RIGHT;

这篇关于全方位滑动手势(向左,向右,向上,向下)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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