DragGestureDetector检测左,右,上和下

时间:2017-03-01 11:52:25

标签: android gesturedetector

// On swipe down -> RIGHT and then DOWN
// On swipe right -> RIGHT and then DOWN
// On swipe left -> LEFT
// On swipe up -> LEFT and then UP
public class DragGestureDetector {
    public static String DEBUG_TAG = "DragGestureDetector";
    private GestureDetectorCompat mGestureDetector;
    private DragListener mListener;
    private boolean mStarted = false;
    private MotionEvent mOriginalEvent;

    float x1,x2;
    float y1, y2;

    public interface DragListener {
        boolean onDragStart(MotionEvent e1, MotionEvent e2, float distanceX,
                                float distanceY);
        boolean onDragContinue(MotionEvent e1, MotionEvent e2, float distanceX,
                                   float distanceY);
        boolean onDragEnd(MotionEvent e1, MotionEvent e2);

        boolean onTapUp();
    }

    public DragGestureDetector(Context context, DragListener myDragListener){
        mGestureDetector = new GestureDetectorCompat(context,new MyGestureListener());
        mListener = myDragListener;
    }

    public void onTouchEvent(MotionEvent event){
        mGestureDetector.onTouchEvent(event);
        int action = MotionEventCompat.getActionMasked(event);
        switch(action) {
            case MotionEvent.ACTION_UP:
                if(mStarted) {
                    mListener.onDragEnd(mOriginalEvent, event);
                }
                mStarted = false;
                x2 = event.getX();
                y2 = event.getY();

                if (x1 < x2)
                {
                    System.out.println("sammy_RIGHT");
                }

                if (x1 > x2)
                {
                    System.out.println("sammy_LEFT");
                }


                if (y1 < y2)
                {
                    System.out.println("sammy_DOWN");
                }

                if (y1 > y2)
                {
                    System.out.println("sammy_UP");
                }
                break;
            case MotionEvent.ACTION_DOWN:
                //need to set this, quick tap will not generate drap event, so the
                //originalEvent may be null for case action_up
                //which lead to null pointer
                mOriginalEvent = event;
                x1 = event.getX();
                y1 = event.getY();
                break;
        }
    }

    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                                float distanceY) {
            if(mListener == null) return true;
            if(!mStarted){
                mListener.onDragStart(e1,e2,distanceX,distanceY);
                mStarted = true;
            }
            else{
                mListener.onDragContinue(e1,e2,distanceX,distanceY);
            }
            mOriginalEvent = e1;
            return true;
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {

            return mListener.onTapUp();
        }
    }


}

我正在从library实现DragGestureDetector。我想正确检测UP,LEFT,RIGHT和DOWN的滑动动作。我当前的代码仅检测到左侧正确滑动。我在滑动时显示LOGCAT中的LOG打印。

1 个答案:

答案 0 :(得分:0)

请参阅以下链接,了解您的问题的答案。

Android: How to handle right to left swipe gestures