GestureDetector interaction tutorial, gesturedetector Interaction

Source: Internet
Author: User

GestureDetector interaction tutorial, gesturedetector Interaction
GsetureDetector interaction process

  1. Trigger the MotionEvent at the instant of the touch screen
  2. Listened by OnTouchListener and obtained the MotionEvent object in onTouch ().
  3. GestureDetector forwards the MotionEvent object to OnGestureListener
  4. 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
  1. Click onDown (MotionEvent e)
  2. Lift onSingleTapUp (MotionEvent e)
  3. Short press onShowPress (MotionEvent e)
  4. Long press onLongPress (MotionEvent e)
  5. Rolling onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
  6. Sliding onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
OnDoubleTapListener: Double-click Method
  1. Double-click OnDoubleTap (MotionEvent e)
  2. Double-click and press and lift each trigger once: onDoubleTapEvent (MotionEvent e )? Application Example?
  3. 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
  1. 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 }});
  1. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.