GestureDetector interaction tutorial, gesturedetector Interaction
GsetureDetector interaction process
- Trigger the MotionEvent at the instant of the touch screen
- Listened by OnTouchListener and obtained the MotionEvent object in onTouch ().
- GestureDetector forwards the MotionEvent object to OnGestureListener
- OnGestureListener obtains the object and makes appropriate feedback based on the encapsulated information of the object.
Internal class: Two listener interfaces OnGestureListener: Click Class Method
- Click onDown (MotionEvent e)
- Lift onSingleTapUp (MotionEvent e)
- Short press onShowPress (MotionEvent e)
- Long press onLongPress (MotionEvent e)
- Rolling onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
- Sliding onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
OnDoubleTapListener: Double-click Method
- Double-click OnDoubleTap (MotionEvent e)
- Double-click and press and lift each trigger once: onDoubleTapEvent (MotionEvent e )? Application Example?
- Click OK onSingleTapConfirmed (MotionEvent e )?
SimpleOnGestureListener
Overview: SimpleOnGestureListener implements the above interfaces OnGestureListener and OnDoubleTapListener. You can inherit this class to implement gesture interaction. You can find the corresponding method in the interface for the required action. If the method returns false, nothing is done.
Example
In this example, an image is loaded on the main interface, and the text is displayed by sliding left and right.
Process
- OnTouchListener listener, get the MotionEvent object in onTouch
Img. setOnTouchListener (new OnTouchListener () {@ Override // events that occur on the captured touch screen public boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stubreturn true; // remember to change it to true }});
- Create a class in Activity that inherits from SimpleOnGestureListener
Class MyGestureListener extends SimpleOnGestureListener {@ Override public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {// TODO Auto-generated method stub // e1 and e2 are the start and end action objects respectively. // compare the e1 and e2 positions to determine the gesture action if (e1.getX () -e2.getX ()> 50) {Toast. makeText (MainActivity. this, "sliding from right to left", 0 ). show ();} else if (e2.getX ()-e1.getX ()> 50) {Toast. makeText (MainActivity. this, "sliding from left to right", 0 ). show ();} return super. onFling (e1, e2, velocityX, velocityY );}}
3. Get a GestureDetextor object (the Declaration is omitted)
gestureDetector = new GestureDetector(MainActivity.this, new MyGestureListener());
4. In the onTouch method, gestureDetector forwards the MotionEvent object to the OnGestureListener (here MyGestureListener)
gestureDetector.onTouchEvent(event);
5. MyGestureListener obtains the object and makes appropriate feedback based on the encapsulated information of the object (text corresponding to the left and right sliding prompt)