Android gesture operation Recognition

Source: Internet
Author: User

SummaryFirst, in the Android system, each gesture interaction is executed in the following order. 1. A MotionEvent event is triggered when you touch the screen. 2. The event is monitored by OnTouchListener and the MotionEvent object is obtained in the onTouch () method. 3. MotionEvent object forwarding through GestureDetector

First, in the Android system, each gesture interaction is executed in the following order.

1. A MotionEvent event is triggered when you touch the screen.

2. The event is monitored by OnTouchListener and the MotionEvent object is obtained in the onTouch () method.

3. Forward the MotionEvent object to OnGestureListener through GestureDetector.

4. OnGestureListener obtains the object and makes appropriate feedback based on the encapsulated information of the object.

This sequence can be said to be the principle of gesture interaction. Let's take a look at MotionEvent, GestureDetector, and OnGestureListener.

MotionEvent: this class is used to encapsulate action events such as gestures, touch pens, and trackballs. It encapsulates two important attributes X and Y, which are used to record the coordinates of the horizontal and vertical axes respectively.

GestureDetector: identifies various gestures.

OnGestureListener: This is a listener interface for gesture interaction. It provides multiple Abstract methods and calls corresponding methods based on GestureDetector's gesture recognition results.

Next, I will use a code example to demonstrate the implementation of gesture interaction, so that everyone can have a deeper understanding and memory of the execution sequence of the above and the distinction between gesture actions.

First, provide a layout file named main. xml with only ImageView.

  
      
   
 


Then, complete our Activity. To listen to touch events and gesture time on the touch screen, the Activity must implement the OnTouchListener and OnGestureListener interfaces and rewrite the methods. The Code is as follows:

Public class MainActivity extends Activity implements OnTouchListener, OnGestureListener {// create a GestureDetector object to identify and tidy up using private GestureDetector detector = new GestureDetector (this); // defines an array, used to put beautiful girl int [] girls = new int [] {R. drawable. girl1, R. drawable. girl2, R. drawable. girl3}; // defines the array subscript to facilitate viewing of private int indexes of girls; private ImageView image; @ Override public void onCreate (Bundle s AvedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); image = (ImageView) findViewById (R. id. image); // set an initial girl image. setImageResource (girls [index]); // listens to the touch screen time image on this ImageView component. setOnTouchListener (this); // remember to set the following two. Otherwise, you will not be able to handle events other than the touch, such as throwing. Image. setLongClickable (true); detector. setIsLongpressEnabled (true);} // call the method public void goNext () {index ++; index = Math. abs (index % girls. length); image. setImageResource (girls [index]);} // rewrite the onTouch method of OnTouchListener // This method is touched on the touch screen, that is, a touch event occurs (touch and touch two events, it is quite image) is called. @ Override public boolean onTouch (View v, MotionEvent event) {detector. onTouchEvent (event); return true;} // @ Override public boolean onDown (MotionEvent e) {return false ;} // called during the throwing action @ Override public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {// velocityX indicates horizontal movement, switch the girl if (velocityX <0) {goNext ();} else if (velocityX> 0) {goPrevious ();} return false ;} // The user calls the method public void goPrevious () {index --; index = Math. abs (index % girls. length); image. setImageResource (girls [index]);} // called on time @ Override public void onLongPress (MotionEvent e) {}// call @ Override public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {return false ;} // called at press and hold @ Override public void onShowPress (MotionEvent e) {}// called at lift @ Override public boolean onSingleTapUp (MotionEvent e) {return false ;}}


When I first started learning Android, I felt that Google's documents were not the same. When I was studying gestures, I felt that Google's documents were too poorly written. Many constants, attributes, and methods are not even described. There is no description, but there are so many gestures in OnGestureListener, and there is no description. Who can understand onLongPress and onShowPress before trying them continuously, is the relationship and difference between onScroll and onFling? Google really needs to perform a major operation on the documentation. However, after repeated attempts. These gesture actions are defined from an individual's perspective.

Press (onDown): the moment when the finger touched the touch screen, it was the touch.

OnFling: the action of quickly moving and releasing fingers on the touch screen.

OnLongPress: the finger is pressed for a period of time and has not been released.

OnScroll: slide your finger on the touch screen.

OnShowPress: the finger is pressed on the touch screen, and its time range is effective after being pressed.

OnSingleTapUp: the moment when your finger leaves the touch screen.

In addition to these definitions, I have also summarized some experience and shared it with you here.

Any gesture action will first execute the onDown action.

Before the onLongPress action, you must execute the onShowPress action.

After the onShowPress action and the onDown action, the onSingleTapUp action is executed.

The onSingleTapUp action is not executed after the onLongPress, onScroll, and onFling actions.

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.