Android slide gesture
Usage: First let the specified Activity implement Android. view. OnGestureListener, then add onTouchListener on The View where you need to call GestureDetector, and then setLongClickable (true.
For example:
CustomView. setOnTouchListner (xxxListener );
CustomView. setLongClickable (true );
Then you can set the expected gesture in the onFling method.
Description of onFling method parameters:
OnFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
Official explanation:
Notified of a fling event when it occurs with the initial on downMotionEventAnd the matching upMotionEvent. The calculated velocity is supplied along the x and y axis in pixels per second.
Specified:OnFling (...) in OnGestureListener Parameters:
E1The first down motion event that started the fling.
E2The move motion event that triggered the current onFling.
VelocityXThe velocity of this fling measured in pixels per second along the x axis.
VelocityYThe velocity of this fling measured in pixels per second along the y axis. Returns: true if the event is consumed, else false roughly means:
E1 indicates an event triggered instantly when the finger is pressed,
E2 indicates the event triggered when the finger is not started and moved
The remaining two parameters roughly mean the number of pixel points moving in the x and y directions per second.
Therefore, you can use e1 and e2 to create conditions for gesture judgment.
Simple left and right sliding gesture code:
@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {// TODO Auto-generated method stubtoif (e1.getX() > e2.getX()) {Log.i(msg, <<<<<<<<<);} else {Log.i(msg, >>>>>>>>>);}Toast.makeText(this, Fling, 0).show();return false;}