如何检测左/右和上/下的滑动方向

85

我的问题:如何检测用户是上下移动手指还是左右移动手指(并且我如何知道手指移动的方向属于哪个组)?

我的情况:当用户上下移动手指时,我想要改变我的应用程序的亮度(上=更亮,下=更暗),当用户左右滑动手指时,我想要在不同的活动和/或视图之间切换。


2
尝试这个:OnSwipeTouchListener.java: https://dev59.com/72855IYBdhLWcg3wy3oc#12938787 - live-love
13个回答

196

我为此编写了一个简单的类:它有很好的文档记录,所以我不会在这里解释它。

public class OnSwipeListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        // Grab two events located on the plane at e1=(x1, y1) and e2=(x2, y2)
        // Let e1 be the initial event
        // e2 can be located at 4 different positions, consider the following diagram
        // (Assume that lines are separated by 90 degrees.)
        //
        //
        //         \ A  /
        //          \  /
        //       D   e1   B
        //          /  \
        //         / C  \
        //
        // So if (x2,y2) falls in region:
        //  A => it's an UP swipe
        //  B => it's a RIGHT swipe
        //  C => it's a DOWN swipe
        //  D => it's a LEFT swipe
        //

        float x1 = e1.getX();
        float y1 = e1.getY();

        float x2 = e2.getX();
        float y2 = e2.getY();

        Direction direction = getDirection(x1,y1,x2,y2);
        return onSwipe(direction);
    }

    /** Override this method. The Direction enum will tell you how the user swiped. */
    public boolean onSwipe(Direction direction){
        return false;
    }

    /**
     * Given two points in the plane p1=(x1, x2) and p2=(y1, y1), this method
     * returns the direction that an arrow pointing from p1 to p2 would have.
     * @param x1 the x position of the first point
     * @param y1 the y position of the first point
     * @param x2 the x position of the second point
     * @param y2 the y position of the second point
     * @return the direction
     */
    public Direction getDirection(float x1, float y1, float x2, float y2){
        double angle = getAngle(x1, y1, x2, y2);
        return Direction.fromAngle(angle);
    }

    /**
     *
     * Finds the angle between two points in the plane (x1,y1) and (x2, y2)
     * The angle is measured with 0/360 being the X-axis to the right, angles
     * increase counter clockwise.
     *
     * @param x1 the x position of the first point
     * @param y1 the y position of the first point
     * @param x2 the x position of the second point
     * @param y2 the y position of the second point
     * @return the angle between two points
     */
    public double getAngle(float x1, float y1, float x2, float y2) {

        double rad = Math.atan2(y1-y2,x2-x1) + Math.PI;
        return (rad*180/Math.PI + 180)%360;
    }


    public enum Direction{
        up,
        down,
        left,
        right;

        /**
         * Returns a direction given an angle.
         * Directions are defined as follows:
         *
         * Up: [45, 135]
         * Right: [0,45] and [315, 360]
         * Down: [225, 315]
         * Left: [135, 225]
         *
         * @param angle an angle from 0 to 360 - e
         * @return the direction of an angle
         */
        public static Direction fromAngle(double angle){
            if(inRange(angle, 45, 135)){
                return Direction.up;
            }
            else if(inRange(angle, 0,45) || inRange(angle, 315, 360)){
                return Direction.right;
            }
            else if(inRange(angle, 225, 315)){
                return Direction.down;
            }
            else{
               return Direction.left;
           }

        }

       /**
         * @param angle an angle
         * @param init the initial bound
         * @param end the final bound
         * @return returns true if the given angle is in the interval [init, end).
         */
        private static boolean inRange(double angle, float init, float end){
            return (angle >= init) && (angle < end);
        }
    }
 }

要使用,只需扩展OnSwipeListener并覆盖onSwipe方法即可


11
到目前为止,这是我尝试过的4向滑动检测最好的实现(在检测准确性方面)。谢谢。 - Marco
3
你能否就在扩展Activity的类中使用它提出一些建议? - Ahmed I. Elsayed
2
寻找我的答案以获取使用示例。 - Mercury
2
要强制覆盖 onSwipe,请将其和类标记为 abstract - Adam Johns
2
这段代码看起来很优雅。我已将它集成到我的项目中,应用在了Webview类上。然而,向上/向下滑动运行得非常好,但是左/右滑动却经常出现问题 - 只有偶尔才能工作。我不知道是因为我使用的方式不正确,还是我的手机不够灵敏(那么为什么向上/向下滑动没问题),或者其他原因?我会尝试另一台手机并回来更新的。 - J.E.Y
显示剩余8条评论

63
您只需要扩展SimpleOnGestureListener类,将其声明在您的类中即可。
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

作为水平滑动的示例,您可以查看以下代码:

 class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH){
                return false;
            }
            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                onLeftSwipe();
            } 
            // left to right swipe
            else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                onRightSwipe();
            }
        } catch (Exception e) {

        }
        return false;
      }
   }

您可以类似地为垂直滑动目的进行此操作。


2
你在答案中提到的那些常量我可以用什么来表示? - kentcdodds
这代表了滑动距离,即用户应该滑动的最小距离来调用目的,其他名称也是如此。 - Saurabh Verma
1
@madprops 我写的这段代码被用在一个活动中,MyGestureDetector是一个内部类,而这些变量则是该活动的属性。 - Saurabh Verma
3
为什么你返回 false 而不是 true,难道你没有按照你想要的方式处理手势吗?另外为什么将 body 放在 try/catch 语句中? - emschorsch
请勿使用 getX(),仅使用 getRawX(),如果您想获取稳定的值。 - Inoy
显示剩余4条评论

37

Fernandour的答案非常完美,我将在这篇回答中介绍如何与ActivityFragment一起使用,因为很多人正在寻找它。

public class MyActivity extends Activity implements View.OnTouchListener{

     private RelativeLayout someLayout;
     //take any layout on which you want your gesture listener;

     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    gestureDetector=new GestureDetector(this,new OnSwipeListener(){

        @Override
        public boolean onSwipe(Direction direction) {
            if (direction==Direction.up){
               //do your stuff
                Log.d(TAG, "onSwipe: up");

            }

            if (direction==Direction.down){
               //do your stuff
                Log.d(TAG, "onSwipe: down");
            }
            return true;
        }


    });
    someLayout.setOnTouchListener(this);
}

    @Override
    public boolean onTouch(View v, MotionEvent event) {
      Log.d(TAG, "onTouch: ");
      gestureDetector.onTouchEvent(event);
      return true;
  }


}

22

以下是对上述fernandohur答案的完整使用示例

如果您想将OnSwipeListener应用于您的某个视图,则:
无论此视图在何处 - 为该视图设置触摸侦听器,如下所示:

myview.setOnTouchListener(this);

现在在您的Activity的OnCreate方法中或者您自定义视图的构造函数中执行以下操作:

// Global
private GestureDetectorCompat detector; 

// In OnCreate or custome view constructor (which extends one of Android views)
detector = new GestureDetectorCompat(context, onSwipeListener);

在同一类中覆盖onTouch事件,例如:

```java @Override public boolean onTouchEvent(MotionEvent event) { // your code here } ```
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    return detector.onTouchEvent(motionEvent);
}

而且在同一个类中还有这个监听器对象:

OnSwipeListener onSwipeListener = new OnSwipeListener() {
    @Override
    public boolean onSwipe(Direction direction) {
        // Possible implementation 
        if(direction == Direction.left|| direction == Direction.right) {
            // Do something COOL like animation or whatever you want
            // Refer to your view if needed using a global reference
            return true;
        }
        else if(direction == Direction.up|| direction == Direction.down) {
            // Do something COOL like animation or whatever you want
            // Refer to your view if needed using a global reference
            return true;
        }
        return super.onSwipe(direction);
    }
};

5

我测试多次后得出的最佳答案,对我很有用

float firstX_point, firstY_point;
@Override
public boolean onTouchEvent(MotionEvent event) {

    int action = event.getAction();

    switch (action) {

        case MotionEvent.ACTION_DOWN:
            firstX_point = event.getRawX();
            firstY_point = event.getRawY();
            break;

        case MotionEvent.ACTION_UP:

            float finalX = event.getRawX();
            float finalY = event.getRawY();

            int distanceX = (int) (finalX - firstX_point);
            int distanceY = (int) (finalY - firstY_point);

            if (Math.abs(distanceX) > Math.abs(distanceY)) {
                if ((firstX_point < finalX)) {
                    Log.d("Test", "Left to Right swipe performed");
                } else {
                    Log.d("Test", "Right to Left swipe performed");
                }
            }else{
                if ((firstY_point < finalY)) {
                    Log.d("Test", "Up to Down swipe performed");
                } else {
                    Log.d("Test", "Down to Up swipe performed");
                }
            }


            break;
    }

    return true;
}

3

这就是我做的方式,最简单的方法

float initialX, initialY;

@Override
public boolean onTouchEvent(MotionEvent event) {

    int action = event.getActionMasked();

    switch (action) {

        case MotionEvent.ACTION_DOWN:
            initialX = event.getX();
            initialY = event.getY();


            // Log.d(TAG, "Action was DOWN");
            break;

        case MotionEvent.ACTION_MOVE:

            //Log.d(TAG, "Action was MOVE");
            break;

        case MotionEvent.ACTION_UP:
            float finalX = event.getX();
            float finalY = event.getY();


            //Log.d(TAG, "Action was UP");

            if (initialX < finalX) {
                // Log.d(TAG, "Left to Right swipe performed");
            }

            if (initialX > finalX) {
                // Log.d(TAG, "Right to Left swipe performed");
            }

            if (initialY < finalY) {
                // Log.d(TAG, "Up to Down swipe performed");
            }

            if (initialY > finalY) {
                // Log.d(TAG, "Down to Up swipe performed");
            }

            break;

        case MotionEvent.ACTION_CANCEL:
            //Log.d(TAG,"Action was CANCEL");
            break;

        case MotionEvent.ACTION_OUTSIDE:
            // Log.d(TAG, "Movement occurred outside bounds of current screen element");
            break;
    }

    return super.onTouchEvent(event);
}

2
您可以重写SimpleGestureListener并计算开始、结束和当前坐标之间的差异:
private class GestureListener extends SimpleOnGestureListener {

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

        if (e2.getY() > e1.getY()) {
            // direction up
        }else {
            // direction down
        }

        if (e2.getX() > e1.getX()) {
            // direction right
        }else {
            // direction left
        }

        return true;
    }
}

这个答案是不正确的。设e=(x,y)为事件e的x,y坐标。令e1=(0,0)和e2=(200,1)。在这个简单的例子中(用户在x轴上滑动了200像素,在y轴上滑动了1像素),您的代码会将其解释为Y轴滑动,这对用户来说没有太多意义。 - fernandohur

1
我这样解决的:

viewPager.setOnTouchListener(new View.OnTouchListener() {
        float prevX = -1;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (prevX != -1) {
                if (event.getX() > prevX) {
                    if (viewPager.getCurrentItem() == 0) {
                         // Left to Right swipe
                    }
                    //Log.d("DEBUG", MotionEvent.ACTION_MOVE + ":" + event.getAction() + ":" + event.getActionMasked() + ":Left Swipe" + ":" + prevX + ":" + event.getX() + ":" + viewPager.getCurrentItem());
                } else if (prevX > event.getX()) {
                       // Right to left swipe
                    //Log.d("DEBUG", MotionEvent.ACTION_MOVE + ":" + event.getAction() + ":" + event.getActionMasked() + ":Right Swipe" + ":" + prevX + ":" + event.getX() + ":" + viewPager.getCurrentItem());
                }
            }
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                prevX = event.getX();
            } else {
                prevX = -1;
            }
            return false;
        }
    });

0

我在Bitbucket上有一个开源手势库可以实现此功能。这个库中有一个'HGFling'类。它演示了如何检测快速滑动的方向。您可以从https://bitbucket.org/warwick/hacergestov3下载该库。它是开源的。


0

检测左滑、右滑、上滑和下滑的最佳和简单方法

  1. 首先创建一个Java类并实现View.OnTouchListener,将以下代码添加到此类中:
    public  class OnSwipeTouchListener implements View.OnTouchListener {
        private final GestureDetector gestureDetector;
        Context context;
        OnSwipeTouchListener(Context ctx, View mainView) {
            gestureDetector = new GestureDetector(ctx, new GestureListener());
            mainView.setOnTouchListener(this);
            context = ctx;
        }
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
        public class GestureListener extends
                GestureDetector.SimpleOnGestureListener {
            private static final int SWIPE_THRESHOLD = 100;
            private static final int SWIPE_VELOCITY_THRESHOLD = 100;
            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                boolean result = false;
                try {
                    float diffY = e2.getY() - e1.getY();
                    float diffX = e2.getX() - e1.getX();
                    if (Math.abs(diffX) > Math.abs(diffY)) {
                        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                            if (diffX > 0) {
                                onSwipeRight();
                            } else {
                                onSwipeLeft();
                            }
                            result = true;
                        }
                    }
                    else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                        result = true;
                    }
                }
                catch (Exception exception) {
                    exception.printStackTrace();
                }
                return result;
            }
        }
        void onSwipeRight() {
            Toast.makeText(context, "You Swipe Right", Toast.LENGTH_SHORT).show();
            this.onSwipe.swipeRight();
        }
        void onSwipeLeft() {
            Toast.makeText(context, "You Swipe Left", Toast.LENGTH_SHORT).show();
            this.onSwipe.swipeLeft();
        }
        void onSwipeTop() {
            Toast.makeText(context, "You Swipe Up", Toast.LENGTH_SHORT).show();
            this.onSwipe.swipeTop();
        }
        void onSwipeBottom() {
            Toast.makeText(context, "You Swipe Down", Toast.LENGTH_SHORT).show();
            this.onSwipe.swipeBottom();
        }
        interface onSwipeListener {
            void swipeRight();
            void swipeTop();
            void swipeBottom();
            void swipeLeft();
        }
        onSwipeListener onSwipe;
    }

在你的MainActivity类中使用以下代码:

    public class MainActivity extends AppCompatActivity {
        OnSwipeTouchListener onSwipeTouchListener;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            onSwipeTouchListener = new OnSwipeTouchListener(this, findViewById(R.id.relativeLayout));
        }
    }

activity_main.xml 文件中:
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        tools:context=".MainActivity">
       
    </RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接