Recognition and processing of Android gestures (double-click onDoubleTap, sliding onFling, and dragging onScroll)
Overview:
Generally, we know that the View class has a View. onTouchListener Internal interface, by rewriting his onTouch (View v, MotionEvent event) method, we can handle some touch events, but this method is too simple, if you need to deal with some complex gestures, using this interface will be very troublesome (because we need to determine the gesture based on the user's touch track ).
The Android sdk provides us with the GestureDetector (Gesture: Gesture Detector: Recognition) class. Through this class, we can recognize many gestures, mainly through its onTouchEvent (event) the method completes the recognition of different gestures. Although he can recognize gestures, how to handle different gestures should be provided to programmers.
The GestureDetector class provides two external interfaces: OnGestureListener, OnDoubleTapListener, and an internal class SimpleOnGestureListener.
GestureDetector. OnDoubleTapListener interface: Used to notify DoubleTap events, similar to double-click events.
1, onDoubleTap (MotionEvent e): triggered when the Touch is down under the second double-click.
2, onDoubleTapEvent (MotionEvent e): Events in the DoubleTap gesture are notified, including down, up, and move events (events between double-click events, for example, double-click in the same place will generate a DoubleTap gesture, while a down and up event will occur in the DoubleTap gesture, which are notified by the function ); double-clicking the next Touch down and up will trigger, available e. getAction () is differentiated.
3. onSingleTapConfirmed (MotionEvent e): used to determine whether this click is a SingleTap rather than a DoubleTap. If you click it twice consecutively, It is a DoubleTap gesture. If you click it only once, after waiting for a while, the system determines that the second click is SingleTap instead of DoubleTap, and then triggers the SingleTapConfirmed event. This method is different from onSingleTapUp, which is triggered when GestureDetector is sure that the user is behind the screen for the first time without following the second touch screen.
GestureDetector. OnGestureListener interface: this interface is used to notify common gesture events. This interface has the following six callback functions:
1. onDown (MotionEvent e): down event;
2. onSingleTapUp (MotionEvent e): Click the up event at a time. The event does not slide after the touch is down.
(OnScroll), and there is no long press (onLongPress), then triggered when Touchup.
Click very fast (do not slide) Touchup:
OnDown-> onSingleTapUp-> onSingleTapConfirmed
Click a slightly slower (do not slide) Touchup:
OnDown-> onShowPress-> onSingleTapUp-> onSingleTapConfirmed
3. onShowPress (MotionEvent e)
Event; when the Touch is not sliding, It is triggered (compared with onDown and onLongPress) as long as the Touch is down, it must be triggered immediately. After Touchdown, onShowPress is triggered before onLongPress after a while without sliding. So do not slide after Touchdown
Triggered in the order of onDown-> onShowPress-> onLongPress.
4. onLongPress (MotionEvent e): A long press event. This event is triggered when Touch is down without moving it;
5. onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY): slide hand
It is triggered only when ACTION_UP is triggered when a sliding distance is reached;
Parameter: e1 contains 1st ACTION_DOWN motionevents and only one; e2 last ACTION_MOVE MotionEvent; velocityX X axis moving speed, pixel/second; velocityY Y axis moving speed, pixel/second. trigger condition: the coordinate displacement of the X axis is greater than FLING_MIN_DISTANCE, and the moving speed is greater than FLING_MIN_VELOCITY pixels/second.
6. onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY): on the screen
Drag events. Whether you drag the view by hand or scroll by throwing the action, this method is triggered multiple times when the ACTION_MOVE action occurs.
Click very fast (do not slide) Touchup:
OnDown-> onSingleTapUp-> onSingleTapConfirmed
Click a slightly slower (do not slide) Touchup:
OnDown-> onShowPress-> onSingleTapUp-> onSingleTapConfirmed
Demo
Customize the Button control to monitor and process gestures:
Public class MyButton extends Button {private GestureDetector mGesture; private OnDoubleClickListener onDoubleClickListener; // custom listener interface OnDoubleClickListener {void onDoubleClick (View view );} // set the public void setOnDoubleClickListener (OnDoubleClickListener onDoubleClickListener) {this. onDoubleClickListener = onDoubleClickListener;}; public MyButton (Context context) {super (cont Ext);} public MyButton (final Context context, AttributeSet attrs) {super (context, attrs); // mGesture = new GestureDetector (context, new GestureDetector. simpleOnGestureListener () {@ Override public boolean onDoubleTap (MotionEvent e) {if (onDoubleClickListener! = Null) {onDoubleClickListener. onDoubleClick (MyButton. this);} Toast. makeText (context, double-click event, Toast. LENGTH_SHORT ). show (); return true ;}@ Override public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {/*** it is best not to implement sliding and dragging together, conflicts will occur */if (Math. abs (e1.getX ()-e2.getX ()> 50) {setTranslationX (e2.getX ()-e1.getX ()); // sliding control ObjectAnimator horizontally Based on the sliding distance of the gesture. ofFloat (MyButton. this, translationX, getTranslationX (), e2.getX ()-e1.getX ()). setDuration (500 ). start (); return true;} return super. onFling (e1, e2, velocityX, velocityY) ;}@ Override public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {// move the setTranslationX (getTranslationX () + e2.getX ()-e1.getX (); setTranslationY (getTranslationX () + e2.getY () -e1.getY (); return super. onScroll (e1, e2, distanceX, distanceY) ;}});} public MyButton (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr );} @ Override public boolean dispatchTouchEvent (MotionEvent event) {if (event. getAction () = MotionEvent. ACTION_DOWN) {} return super. dispatchTouchEvent (event) ;}@ Override public boolean onTouchEvent (MotionEvent event) {// touch event is passed to onTouchEvent () mGesture. onTouchEvent (event); return super. onTouchEvent (event );}}
Layout:
Main activity:
Public class MainActivity extends Activity {private MyButton myButton; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); myButton = (MyButton) findViewById (R. id. button_doubleTap); myButton. setOnDoubleClickListener (new MyButton. onDoubleClickListener () {@ Override public void onDoubleClick (View view) {Log. d (, click twice );}});}}
Result demonstration:
Double-click:
Slide: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPjxiciAvPg0KPGltZyBhbHQ9 "here write picture description" src = "http://www.bkjia.com/uploads/allimg/151012/050PSZ2-1.gif" title = "\"/>
Drag: