Android手势监听GestureDetector

1.GestureDetector.OnGestureListener

onDown:表示用户的按下动作,只要用户触摸屏幕,不管是点击、长按、拖动、滑动都会先触发。

onShowPress:表示用户按下后,并没有立刻松开,而是停留了极短的时间后就会触发。

onSingleTapUp:表示用户单纯的一次点击事件,并没有其他事件,就会触发。

onScroll:表示用户的滑动,滑动时会连续触发。

onLongPress:表示用户的长按事件,按下后超过一定的时间会触发。

onFling:表示用户滑动后,剩下的惯性会继续滑动,就会触发。

------GestureDetector.OnGestureListener事件执行顺序------

快速点击屏幕:onDown→onSingleTapUp

稍微慢速的点击屏幕:onDown→onShowPress→onSingleTapUp

长按屏幕:onDown→onShowPress→onLongPress

快速点击屏幕后滑动无惯性:onDown→onScroll→onScroll→onScroll...........

慢速点击屏幕后滑动无惯性:onDown→onShowPress→onScroll→onScroll→onScroll...........

快速点击屏幕后滑动有惯性:onDown→onScroll→onScroll→onScroll...........→onFling

慢速点击屏幕后滑动有惯性:onDown→onShowPress→onScroll→onScroll→onScroll...........→onFling

2.GestureDetector.OnDoubleTapListener

onSingleTapConfirmed:单击事件,用来判定该次点击是单纯的SingleTap而不是DoubleTap。

onDoubleTap:双击事件

onDoubleTapEvent:

------GestureDetector.OnGestureListener加上GestureDetector.OnDoubleTapListener事件执行顺序------

快速点击屏幕:onDown→onSingleTapUp→onSingleTapConfirmed

稍微慢速的点击屏幕:onDown→onShowPress→onSingleTapUp→onSingleTapConfirmed

双击屏幕:onDown→onSingleTapUp→onDoubleTap→onDoubleTapEvent→onDown→onDoubleTapEvent

3.用法

public class GestureActivity extends AppCompatActivity {

    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gesture);

        gestureDetector = new GestureDetector(this, new MyGestureListener());
        gestureDetector.setOnDoubleTapListener(new MyDoubleTapListener());

        ImageView imageView = findViewById(R.id.image);
        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return gestureDetector.onTouchEvent(motionEvent);
            }
        });
    }

    class MyGestureListener implements GestureDetector.OnGestureListener {

        @Override
        public boolean onDown(MotionEvent motionEvent) {
            System.out.println("OnGestureListener:" + "onDown");
            return true;
        }

        @Override
        public void onShowPress(MotionEvent motionEvent) {
            System.out.println("OnGestureListener:" + "onShowPress");
        }

        @Override
        public boolean onSingleTapUp(MotionEvent motionEvent) {
            System.out.println("OnGestureListener:" + "onSingleTapUp");
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
            System.out.println("OnGestureListener:" + "onScroll");
            return true;
        }

        @Override
        public void onLongPress(MotionEvent motionEvent) {
            System.out.println("OnGestureListener:" + "onLongPress");
        }

        @Override
        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
            System.out.println("OnGestureListener:" + "onFling");
            return true;
        }
    }

    class MyDoubleTapListener implements GestureDetector.OnDoubleTapListener{

        @Override
        public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
            System.out.println("OnDoubleTapListener:" + "onSingleTapConfirmed");
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent motionEvent) {
            System.out.println("OnDoubleTapListener:" + "onDoubleTap");
            return true;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent motionEvent) {
            System.out.println("OnDoubleTapListener:" + "onDoubleTapEvent");
            return true;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    class MyContextClickListener implements GestureDetector.OnContextClickListener {

        @Override
        public boolean onContextClick(MotionEvent motionEvent) {
            System.out.println("OnContextClickListener:" + "onContextClick");
            return true;
        }
    }
}

你可能感兴趣的:(Android手势监听GestureDetector)