(Transferred from: http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1020/448.html)
First, in an Android system, each gesture interaction is performed in the following order.
1. Touch the touch screen flash, triggering a motionevent event.
2. The event is ontouchlistener monitored to obtain the Motionevent object in its Ontouch () method.
3. Forward the secondary Motionevent object to Ongesturelistener via Gesturedetector (gesture recognizer).
4. Ongesturelistener obtains the object and listens to the information encapsulated by the object to make appropriate feedback.
This order can be said to be the principle of gesture interaction, the following together to learn about Motionevent, Gesturedetector and Ongesturelistener.
Motionevent: This class is used to encapsulate action events such as gestures, touch pens, trackball, and so on. It internally encapsulates two important attributes X and Y, which are used to record the coordinates of the horizontal and vertical axes, respectively.
Gesturedetector: Identify various gestures.
Ongesturelistener: This is a listener interface for gesture interaction, which provides several abstract methods, and calls the corresponding method according to the gesturedetector gesture recognition result.
Let me show you the implementation of the gesture interaction by a code example that toggles the image, so that we have a deeper understanding and memory of the order of execution and the distinction between gestures.
First, provide a layout file that has only ImageView--main.xml.
1 2 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" 3 Android:o rientation= "Vertical" 4 android:layout_width= "Fill_parent" 5 6 7 < ImageView android:id= "@+id/image" 8 android:layout_width= "Fill_parent" 9 android:layout_height= "Fill_parent" </LinearLayout>
Then, complete our activity, because to listen to touch screen touch events and gesture time, so the activity must implement Ontouchlistener and Ongesturelistener two interfaces, and override the methods. The specific code is as follows:
1 2 //Create a Gesturedetector object to identify the pack waiyuwu.blogcn.com3 PrivateGesturedetector detector =NewGesturedetector ( This); 4 //define an array to put the beautiful girl5 int[] Girls =New int[]{r.drawable.girl1, R.drawable.girl2, r.drawable.girl3};6 //define array subscripts for easy viewing of individual girls7 Private intindex;8 PrivateImageView image;9 Ten @Override One Public voidonCreate (Bundle savedinstancestate) { A Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main); -Image =(ImageView) Findviewbyid (r.id.image); the //set an initial display of the girl bar - Image.setimageresource (Girls[index]); - //Listen for touch screen time on this ImageView component -Image.setontouchlistener ( This); + //The following two should remember to set oh, otherwise it will not be able to deal with other than the light touch events, such as throwing action. -Image.setlongclickable (true); +Detector.setislongpressenabled (true); A}//a way to shout the next girl at Public voidGoNext () { -index++; -index = math.abs (index%girls.length); - Image.setimageresource (Girls[index]); - } - in //Ontouch method for overriding Ontouchlistener - //This method is called when the touchscreen is touched, that is, when touch events occur (touching and stroking two events, pretty image). to @Override + Public BooleanOnTouch (View V, motionevent event) { - detector.ontouchevent (event); the return true; * } $ Panax Notoginseng //called when an action is pressed - @Override the Public BooleanOndown (motionevent e) { + return false; A } the + //be called when throwing an action - @Override $ Public BooleanOnfling (motionevent E1, motionevent E2,floatVelocityx, $ floatvelocityy) { - //Velocityx indicates lateral movement, switching girls according to the direction of the finger movement - if(Velocityx < 0){ the GoNext (); -}Else if(Velocityx > 0){ Wuyi goprevious (); the } - return false; Wu } - About //the way the user calls on a girl $ Public voidgoprevious () { -index--; -index = math.abs (index%girls.length); - Image.setimageresource (Girls[index]); A } + the //be called on long and on time - @Override $ Public voidonlongpress (motionevent e) { the } the the //Called when scrolling the @Override - Public BooleanOnscroll (motionevent E1, motionevent E2,floatDistancex, in floatDistancey) { the return false; the } About the //called when pressed the @Override the Public voidonshowpress (motionevent e) { + } - the //is called when liftedBayi @Override the Public BooleanOnsingletapup (motionevent e) { the return false; - } -}
When I first started learning Android, I felt that Google's documents were not good, and when we studied gestures, I felt that Google's documents were too poor. A lot of constants, properties, and methods don't even have a description. There is no description, but there are so many gestures in the ongesturelistener, it does not have a description, who can understand the relationship and difference between onlongpress and Onshowpress,onscroll and onfling before attempting to do it continuously? Google really needs to do a big surgery on the documentation. But fortunately, after my repeated attempts. These gestures are defined from a personal point of view.
Press (Ondown): Just the moment the finger touches the touch screen, it is the touch of the moment.
Throw (onfling): The finger moves quickly on the touchscreen and releases the action.
Long Press (onlongpress): The finger is pressed for a period of time and is not loosened.
Scrolling (onscroll): Fingers slide on the touchscreen.
Press and Hold (onshowpress): The finger is pressed on the touchscreen, its time range is pressed, and before the long press.
Lift (Onsingletapup): The moment the finger leaves the touch screen.
In addition to these definitions, I also summed up a bit of experience, and here to share with you.
Any gesture action will first perform a pressing (Ondown) action.
Press and hold (onshowpress) action before long pressing (onlongpress) action.
A lift (Onsingletapup) action is performed after holding (onshowpress) the action and pressing (Ondown) the action.
Hold (onlongpress), scroll (onscroll), and throw (onfling) actions do not perform a lift (onsingletapup) action.
Android gesture Operation recognition