1. http://blog.csdn.net/omg_2012/article/details/7881443
This article is quite good.
2. http://blog.csdn.net/android_tutor/article/details/7193090
3. http://blog.csdn.net/heng615975867/article/details/8791937
4. http://www.dewen.org/q/2438/
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.
Android recognition touch screen gestures greatly improve user experience. There is a view in the View class. ontouchlistener Internal interface, through rewriting his ontouch (view V, motionevent event) method, we can handle some simple touch events, but this method does not recognize gestures, if you need to process some complex gestures, it will be very troublesome to use this interface (because we need to determine what gestures are based on the trajectory of the user's touch ). Fortunately, Android provides us with the gesturedetector class, through which we can easily perform gesture recognition. Here is a brief introduction.
1. Introduction to gesturedetector
1. Composition
The gesturedetector class is used to recognize various touch screen gestures. It contains two interfaces and an internal class:
Interface:
Ongesturelistener: Used to listen for gesture events (6 types ).
Ondoubletaplistener: used to listen to double-click events.
Internal class:
Simpleongesturelistener: used to listen to all gestures. In fact, it implements the above two interfaces, but the method body is empty and needs to be written by ourselves. We can inherit this class and rewrite the methods in it for gesture processing.
2. Construct
Gesturedetector = new gesturedetector (gesturedetector. ongesturelistener listener );
Gesturedetector = new gesturedetector (context, gesturedetector. ongesturelistener listener );
Gesturedetector = new gesturedetector (context, gesturedetector. simpleongesturelistener listener );
3. Method
(1) ontouchevent (motionevent eV) analyzes the captured touch events to trigger the corresponding callback function
(2) setislongpressenabled (Boolean islongpressenabled) is used to set "Long press"
(3) setondoubletaplistener (gesturedetector. ondoubletaplistener) set double-click listener
4. Use
Process:
First, the system captures the screen touch event (ontouchlistener). At this time, no specific gesture is involved, but the touch is simply captured.
Then, call the ontouchevent () method of gesturedetector in the ontouch () method, and assign the motionevent to gesturedetector for processing.
Finally, an abstract method needs to be implemented.
Implementation:
(1) create a gesturedetector instance gesturedetector in the activity.
(2) select as needed:
Override ongesturelistener and pass in gesturedetector through the constructor
Override ondoubletaplistener and pass in gesturedetector. setondoubletaplistener
Override simpleongesturelistener and pass in gesturedetector through the constructor
(3) override the ontouchevent method of the activity and hand over all the touch events to gesturedetector for processing.
@ Override
Public Boolean ontouchevent (motionevent event ){
Return gesturedetector. ontouchevent (event );
}
Ii. ongesturelistener
1. ongesturelistener identifies 6 kinds of gestures:
(1) ondown (motionevent E): Down event;
(2) onsingletapup (motionevent E): one-click up event;
(3) onshowpress (motionevent E): When a down event occurs, move or up does not trigger the event before it expires;
(4) onlongpress (motionevent E): Long press event;
(5) onfling (motionevent E1, motionevent E2, float velocityx, float velocityy): A sliding gesture event;
(6) onscroll (motionevent E1, motionevent E2, float distancex, float distancey): drag the event on the screen.
Here I need to find the differences between onfling and onscroll on the Internet.
Onfling () is a false action, which is executed when a motionevent. action_up (finger-up) occurs, and onscroll () is executed as long as the finger moves. It does not execute motionevent. action_up. Onfling is usually used to achieve the page turning effect, while onscroll is usually used to zoom in, out, and move.
2. Rewrite
Ongesturelistener = new ongesturelistener (){
@ Override
Public Boolean ondown (motionevent e ){
Return false;
}
@ Override
Public Boolean onfling (motionevent E1, motionevent E2,
Float velocityx, float velocityy ){
Return false;
}
@ Override
Public Boolean onlongpress (motionevent e ){
Return false;
}
@ Override
Public Boolean onscroll (motionevent E1, motionevent E2,
Float distancex, float distancey ){
Return false;
}
@ Override
Public void onshowpress (motionevent e ){
}
@ Override
Public Boolean onsingletapup (motionevent e ){
Return false;
}
}
You can add a specific processing method to the function as needed. Then pass in gesturedetector through the constructor.
Gesturedetector = new gesturedetector (this, ongesturelistener );
Ii. ondoubletaplistener
1. ondoubletaplistener is used to detect double-click events. The following abstract methods must be implemented:
(1) ondoubletap (motionevent E): Double-click an event.
(2) ondoubletapevent (motionevent E): Another action occurs during the double-click interval. Notifies the events in the doubletap gesture, including Down, up, and move events.
(This refers to an event that occurs between double-clicking. For example, double-clicking in the same place will generate a doubletap gesture, and a down or up event will occur in the doubletap gesture, these two events are notified by the function );
(3) onsingletapconfirmed (motionevent E): Click the event. It is used to determine whether this click is a singletap rather than a doubletap. If you click it twice consecutively, It is a doubletap gesture,
If only one click is received, the system determines that this click is singletap rather than doubletap, and then triggers the singletapconfirmed event.
One difference between onsingletapconfirmed and onsingletapup: ongesturelistener has such a method onsingletapup, which is easy to confuse with onsingletapconfirmed.
The difference between the two is: onsingletapup, which will be executed as long as the hand is lifted. For onsingletapconfirmed, if you double-click it, onsingletapconfirmed will not be executed.
2. Rewrite
Ondoubletaplistener new ondoubletaplistener (){
@ Override
Public Boolean onsingletapconfirmed (motionevent e ){
Return false;
}
@ Override
Public Boolean ondoubletapevent (motionevent e ){
Return false;
}
@ Override
Public Boolean ondoubletap (motionevent e ){
Return false;
}
)
You can add a specific processing method to the function as needed, and then pass in gesturedetector through setondoubletaplistener.
Gesturedetector. setondoubletaplistener (ondoubletaplistener );
Iii. simpleongesturelistener
Simpleongesturelistener is an internal class of the gesturedetector class, which is static class, that is, it is actually an external class. You can inherit this class from the outside and override the gesture Processing Method in it.
1. simpleongesturelistener actually implements ongesturelistener and ondoubletaplistener, so it can complete all the gesture recognition mentioned above (9 types), as described above.
2. Rewrite
Public class simpleongesturelistener extends simpleongesturelistener {
@ Override
Public Boolean ondoubletap (motionevent e ){
Return super. ondoubletap (E );
}
@ Override
Public Boolean ondoubletapevent (motionevent e ){
Return super. ondoubletapevent (E );
}
@ Override
Public Boolean ondown (motionevent e ){
Return super. ondown (E );
}
@ Override
Public Boolean onfling (motionevent E1, motionevent E2, float X,
Float y ){
Return super. onfling (E1, E2, x, y );
}
@ Override
Public void onlongpress (motionevent e ){
Super. onlongpress (E );
}
@ Override
Public Boolean onscroll (motionevent E1, motionevent E2, float X,
Float y ){
Return super. onscroll (E1, E2, x, y );
}
@ Override
Public void onshowpress (motionevent e ){
Super. onshowpress (E );
}
@ Override
Public Boolean onsingletapconfirmed (motionevent e ){
Return super. onsingletapconfirmed (E );
}
@ Override
Public Boolean onsingletapup (motionevent e ){
Return super. onsingletapup (E );
}
}
You can add a specific processing method to the function as needed, and then pass in gesturedetector through the constructor.
Gesturedetector = new gesturedetector (this, simpleongesturelistener );
4. Another idea
To capture screen gestures, in addition to creating gesturedetector in the activity, there is also an idea: Build an overlay, which implements the ongesturelistener interface to maintain its own gesturedetector.
Add the overlay in the main view and input the corresponding listener to capture the gesture.
I have never understood these two things, but I have never been blind. Today I am free to read them again. It seems that I have understood some of them and wrote the test code for testing, but I still don't quite understand. For more information, see http://blog.csdn.net/android_tutor/article/details/7193090.
First, this article is only intended for events triggered by touch.
Android events: onclick, onscroll, onfling, and so on are composed of many touch events. The first status of touch must be action_down, indicating that the screen is pressed. Later, touch will have a follow-up event, which may be:
Action_move // indicates a mobile gesture.
Action_up // indicates leaving the screen
Action_cancel // indicates that the canceling gesture is not generated by the user, but by the program.
One action_down, N action_move, and one action_up constitute many events in Android.
In Android, a type of control can also contain other child controls, which inherit from the viewgroup class, such as listview, gallery, and gridview.
Another type of controls cannot contain child controls, such as textview.
This article mainly discusses the trigger of nested event items of viewgroup controls.
An important method for viewgroup controls is onintercepttouchevent (), which is used to process events and change the transfer direction of events. Its return value is a Boolean value, determines whether the touch event needs to be passed to the Child view it contains. This method is to pass from the parent View to the Child view.
The ontouchevent () method is used to receive and process events. Its return value is also a Boolean value, which determines whether the event and subsequent events continue to pass up, this method is passed from the Child View to the parent view.
The transfer mechanism between onintercepttouchevent () and ontouchevent and each childview depends entirely on the return values of onintercepttouchevent () and ontouchevent. If the returned value is true, the event is correctly received and processed. If the returned value is false, the event is not processed and will be passed on.
The action_down event is uploaded to the onintercepttouchevent of a viewgroup class. If false is returned, the down Event continues to be passed to the onintercepttouchevent of the child viewgroup class. If the child view is not a control of the viewgroup class, the ontouchevent that is passed to it.
If onintercepttouchevent returns true, the ontouchevent that the down event is passed to it will not be passed, and subsequent events will also be passed to its ontouchevent.
If the ontouchevent of a view returns false, the down Event continues to be passed to the ontouchevent of its parent viewgroup class. If true is returned, subsequent events will be directly passed to its ontouchevent for further processing. (Subsequent events will only be passed to ontouchevent that returns true for the required event action_down)
To sum up, onintercepttouchevent can accept all touch events, while ontouchevent is not necessarily.
Http://hao3100590.iteye.com/blog/1267294
Http://blog.csdn.net/liutao5757124/article/details/6097125
Http://wenku.baidu.com/view/bcd001c608a1284ac850430c.html
Http://www.2cto.com/kf/201109/104205.html
The order in which motionevent events are transmitted in onintercepttouchevent () and ontouchevent ()
Http://www.cnblogs.com/kingcent/archive/2011/03/08/1977064.html
Http://glason.diandian.com/post/2011-12-19/12743121
Http://www.cnblogs.com/kingcent/archive/2011/03/08/1977064.html
Http://wenku.baidu.com/view/bcd001c608a1284ac850430c.html
Http://blog.csdn.net/ddna/article/details/5473293
Http://blog.csdn.net/ddna/article/details/5451722
Http://blog.csdn.net/G_rrrr/article/details/4861189
Official Document Translation:
Http://blog.csdn.net/iefreer/article/details/4586351