When you touch the screen, many gestures are generated, such as Down, up, scroll, and filing.
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,OndoubletaplistenerAnd an internal classSimpleongesturelistener.
Gesturedetector. ondoubletaplistenerInterface: Used for notificationDoubletapAn event is similar to a double-click event.
1,Ondoubletap (motionevent E): InDouble-click,Touch downTrigger .
2,Ondoubletapevent (motionevent E): NotificationDoubletapEvents in gestures, includingDown,UpAndMoveEvent (this refers to the event that occurs between double-clicking, for example, double-clicking in the same place will generateDoubletapGestures, whileDoubletapIt will also happen in the gesture.DownAndUpEvents, these two events are notified by the function); double-click the secondTouch downAndUpWill be triggered, availableE. getaction ()Yes.
3,Onsingletapconfirmed (motionevent E): Used to determine whether this click isSingletapInsteadDoubletapIf you click it twice in a rowDoubletapGesture. If you click only once, the system does not receive the second click after a period of time.SingletapInsteadDoubletapAnd then triggerSingletapconfirmedEvent.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. ongesturelistenerInterface: Used to notify common gesture events. This interface has the following six callback functions:
1. Ondown (motionevent E):DownEvent;
2. Onsingletapup (motionevent E): One clickUpEvent; inTouch downNo sliding
(Onscroll), And there is no long press (Onlongpress), And thenTouchup.
Click it very quickly(Do not slide)Touchup:
Ondown-> onsingletapup-> onsingletapconfirmed
A little slowPoint (do not slide)Touchup:
Ondown-> onshowpress-> onsingletapup-> onsingletapconfirmed
3. Onshowpress (motionevent E):DownWhen an event occursMoveOrUpNot triggered before the lifetime
Event;TouchNo sliding trigger (Ondown,Onlongpress) ComparisonOndownAs longTouch downMust be triggered immediately. WhileTouchdownAfter a while, it is triggered before sliding.OnshowpressThenOnlongpress. SoTouchdownDo not slide after
FollowOndown-> onshowpress-> onlongpressThis order is triggered.
4. Onlongpress (motionevent E): Long press event;TouchKeep movingTouch downTrigger
5. Onfling (motionevent E1, motionevent E2, float velocityx, float velocityy): Slide hand
Potential events;TouchAfter sliding a little distance,Triggered only when action_up
Parameters:E1 1st action_down motionevent 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.
Tip: when your fingers touch the screen, slide a little and then release immediately
Ondown -----"Onscroll ----"Onscroll ----"Onscroll ----"......... -----> Onfling
Drag
Ondown ------ onscroll ---- onscroll ------ onfiling
SimpleongesturelistenerClassYesGesturedetectorProvides a class for us to easily respond to different gestures. This class implements the above two interfaces.(But all method bodies are empty). This class is static class, that is, it is actually an external class. Programmers can inherit this class externally and rewrite the gesture processing method.
Procedure
Example 1:
1, ThroughGesturedetectorThe constructorSimpleongesturelistenerThe object is passed in.GesturedetectorYou can process different gestures.
Public gesturedetector
(Context, gesturedetector. ongesturelistener listener)
2. In the ontouch method of ontouchlistener
Private ontouchlistener gesturetouchlistener = new ontouchlistener (){
Public Boolean ontouch (view V, motionevent event ){
Return gdetector. ontouchevent (event );
}
};
Example 2:
Usage
Private gesturedetector mgesturedetector;
Mgesturelistener = new bookongesturelistener ();
Constructed mgesturedetector = new gesturedetector (mgesturelistener );
Class bookongesturelistener implements ongesturelistener {
Public Boolean ontouchevent (motionevent event ){
Mgesturelistener. ontouchevent (event );
}
Third sample code
Code:
01. Private gesturedetector mgesturedetector;
[Email protected]
03. Public void oncreate (bundle savedinstancestate ){
04. Super. oncreate (savedinstancestate );
05. mgesturedetector = new gesturedetector (this, new learngesturelistener ());
06 .}
[Email protected]
08. Public Boolean ontouchevent (motionevent event ){
09. If (mgesturedetector. ontouchevent (event ))
10. Return true;
11. Else
12. Return false;
13 .}
14. Class learngesturelistener extends gesturedetector. simpleongesturelistener {
15. @ override
16. Public Boolean onsingletapup (motionevent eV ){
17. log. D ("onsingletapup", Ev. tostring ());
18. Return true;
19 .}
20. @ override
21. Public void onshowpress (motionevent eV ){
22. log. D ("onshowpress", Ev. tostring ());
23 .}
24. @ override
25. Public void onlongpress (motionevent eV ){
26. log. D ("onlongpress", Ev. tostring ());
27 .}
28. @ override
29. Public Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey ){
30. log. D ("onscroll", e1.tostring ());
31. Return true;
32 .}
33. @ override
34. Public Boolean ondown (motionevent eV ){
35. log. D ("ondownd", Ev. tostring ());
36. Return true;
37 .}
38. @ override
39. Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy ){
40. log. D ("D", e1.tostring ());
41. log. D ("E2", e2.tostring ());
42. Return true;
43 .}
44 .}
1. Create a gesturedetector instance in the current class.
Private gesturedetector mgesturedetector;
2. Create a listener to monitor the operation gesture of the current Panel in real time.
Class learngesturelistener extends gesturedetector. simpleongesturelistener
3. During initialization, the listener instance is associated with the current gesturedetector instance.
Mgesturedetector = new gesturedetector (this, new learngesturelistener ());
4. The ontouchevent method is used as the entry detection, and the motionevent parameter is passed to listen for Operation gestures.
1. mgesturedetector. ontouchevent (Event)
Fourth sample code
Private gesturedetector mgesturedetector;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Mgesturedetector = new gesturedetector (this, new mygesturelistener ());
}
@ Override
Public Boolean ontouchevent (motionevent event ){
Return mgesturedetector. ontouchevent (event );
}
Class mygesturelistener extends gesturedetector. simpleongesturelistener {
@ Override
Public Boolean onsingletapup (motionevent eV ){
Log. D ("onsingletapup", Ev. tostring ());
Return true;
}
@ Override
Public void onshowpress (motionevent eV ){
Log. D ("onshowpress", Ev. tostring ());
}
@ Override
Public void onlongpress (motionevent eV ){
Log. D ("onlongpress", Ev. tostring ());
}
...
}
The basic content is to create a gesturedetector object, pass in the listener object, and pass the event to the gesturedetector in the ontouchevent you receive for analysis. The listener calls back the corresponding action to us. Here, gesturedetector. simpleongesturelistener (framework simplifies) is a class that implements the ongesturelistener and ondoubletaplistener interfaces mentioned above. We only need to inherit it and override the callback we care about.
Finally, let's take a look at the double-click and triple-click recognition process: when the first click is down, a message with a latency of MS is sent to hanlder, if the next click event occurs, double-click the event and remove the previously sent delayed message. If there is still no second down message after ms, it is determined as a singletapconfirmed event (of course, the user's finger should have completed the up process for the first click ). The trigger is similar to the double-click trigger. It only takes one more time to send a delayed message.