Android gestures & Touch Event motionevent

Source: Internet
Author: User

1.http://blog.csdn.net/omg_2012/article/details/7881443

This one's pretty 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/



Action_mask is applied to multi-touch operations in Android, literally meaning the action mask.
In Ontouchevent (Motionevent event), use switch (event.getaction ()) to handle Action_down and ACTION_UP events, using switch ( Event.getaction () & Motionevent.action_mask) are able to handle Action_pointer_down and ACTION_POINTER_UP events that handle multi-touch.
Ask
Can you give me another explanation? Switch (event.getaction () & Motionevent.action_mask) and
Some of the events of Motionevent
Reply
Action_down and Action_up is a single touch screen, press down and release the operation;
Action_pointer_down and Action_pointer_up is a multi-touch screen, when there is only one finger press down, there is only a finger press and release action capture;
Action_move is the operation of the finger moving on the screen;
These are the ones that are often used.


Public   void onpagescrollstatechanged (int.   ) {
called when the scrolling state changes. Used to discover when a user starts dragging, when the page itself is actively settling to the current page (when the user does not drag), or when it completely stops/spare.




===================================================================================================

2014-2-27 Supplement:

Http://developer.android.com/reference/android/view/MotionEvent.html
Http://developer.android.com/training/gestures/multi.html (Official document: About Multitouch) There's a series of documents here, and there are other gestures about it.-Great.
Http://my.eoe.cn/31127/threadhttp://www.eoeandroid.com/thread-259702-1-1.html

Gesturedetector (Ongesturelistener, Ondoubletaplistener, Simpleongesturelistener) http://blog.csdn.net/ xiezhenxiang/article/details/6659506


Http://www.cnblogs.com/akira90/archive/2013/03/10/2952886.html


http://blog.csdn.net/xipiaoyouzi/article/details/7974898

Sample samples
Http://www.2cto.com/kf/201209/156450.html

Http://www.cnblogs.com/devinzhang/archive/2012/02/27/2369942.html

http://blog.csdn.net/cloudzfy1/article/details/6582707

http://www.icodelogic.com/?p=601


Http://www.blogjava.net/TiGERTiAN/archive/2011/02/22/344869.html

Http://www.linuxidc.com/Linux/2012-06/62567.htm


===================================================================================================


When I first started to learn Android, I thought Google's documents are not good, in the study of gestures, more feel that Google's documents are too poor. Very many constants, properties and methods, even a descriptive narrative did not.
No description of the narrative is just, but ongesturelistener gesture so much, it does not have a description, in the absence of continuous before trying, who can understand onlongpress and onshowpress,
Is there a difference between onscroll and onfling? Google really needs to do a big surgery on the documentation. Just good after the 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 run the Ondown action first.
Press and hold (onshowpress) action once before long pressing (onlongpress) action.
Holding (onshowpress) and pressing (Ondown) actions will run a lift (onsingletapup) action.
The lift (Onsingletapup) action is not run after long press (onlongpress), scrolling (onscroll), and throw (onfling) actions.

Android Recognition touch screen gestures make the user experience much more. There is a View.ontouchlistener internal interface in the view class, and by overriding his Ontouch (View V, motionevent event) method, we can handle some simple touch events, but this method does not recognize gestures, Given the need to deal with some complex gestures, it can be cumbersome to use this interface (because we want to infer 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 do gesture recognition. Below I make a brief introduction.
I. Gesturedetector Brief INTRODUCTION
1. Composition
The Gesturedetector class is used to identify the various gestures of the touchscreen, which includes two interfaces and an inner class:
Interface:
Ongesturelistener: Used to listen for gesture events (6 kinds).
Ondoubletaplistener: Used to listen for double-click events.
Inner class:
Simpleongesturelistener: Used to listen for all gestures. In fact, it implements the above two interfaces, but the method body is empty, we need to write ourselves. We can inherit this class and rewrite the method inside to do gesture processing.
2. Construction
Gesturedetector gesturedetector=new gesturedetector (Gesturedetector.ongesturelistener listener);
Gesturedetector gesturedetector=new gesturedetector (Context context,gesturedetector.ongesturelistener listener);
Gesturedetector gesturedetector=new gesturedetector (Context context,gesturedetector.simpleongesturelistener Listener);
3. Methods
(1) ontouchevent (motionevent ev) analysis captured touch event triggering corresponding callback function
(2) setislongpressenabled (Boolean islongpressenabled) setting "Long Press" is available
(3) Setondoubletaplistener (Gesturedetector.ondoubletaplistener Ondoubletaplistener) set double-click Listener
4. Use
Process:
First, the system captures the touch events (Ontouchlistener) of the screen, and no detailed gestures are involved, simply capturing the touch.
Next, call Gesturedetector's Ontouchevent () method in the Ontouch () method to give the captured motionevent to Gesturedetector to handle
Finally, you need to implement an abstract approach.
Realize:
(1) Create Gesturedetector instance gesturedetector in activity.
(2) Depending on the need to choose:
Rewrite the Ongesturelistener and pass in the Gesturedetector through the constructor function
Override Ondoubletaplistener and pass Gesturedetector.setondoubletaplistener method to Gesturedetector
Rewrite the Simpleongesturelistener and pass in the Gesturedetector through the constructor function
(3) Rewrite the Ontouchevent method of activity, give all touch events to Gesturedetector to handle
@Override
public boolean ontouchevent (Motionevent event) {
Return Gesturedetector.ontouchevent (event);
}
Two. Ongesturelistener
1.onGestureListener recognizes 6 gestures, each of which is:
(1) Ondown (motionevent e): down event;
(2) Onsingletapup (motionevent e): one click Up event;
(3) onshowpress (motionevent e): A Down event occurs while move or up has not occurred before the event is triggered;
(4) onlongpress (motionevent E): Long press events;
(5) Onfling (motionevent E1, motionevent E2, float Velocityx, float velocityy): swipe gesture event;
(6) Onscroll (motionevent E1, motionevent E2, float Distancex, float Distancey): Drag the event on the screen.
Here I need to wordy on the internet to find out about onfling and onscroll a little difference.
Onfling () is a dump, which runs when a motionevent.action_up (finger is lifted) occurs, and onscroll () simply moves the finger. He will not run MOTIONEVENT.ACTION_UP. Onfling-Pass is often used to achieve page flipping effects, while onscroll is often used to zoom in and out and move.
2. Rewrite
Ongesturelistener 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;
}
}
It is possible to add a detailed processing method to the function as needed. Then pass in the Gesturedetector by the constructor.
Gesturedetector gesturedetector=new Gesturedetector (This,ongesturelistener);
Two. Ondoubletaplistener
1.OnDoubleTapListener is used to detect the mouse double-click event. The abstract methods that need to be implemented are:
(1) Ondoubletap (motionevent e): Double-click the event.
(2) ondoubletapevent (Motionevent e): Other actions occur in the double-click interval. Notifies events in the Doubletap gesture, including down, up, and move events
(This refers to an event that occurs between double-clicking, such as a doubletap gesture in the same place, and a down and up event in the Doubletap gesture, both of which are notified by the function);
(3) onsingletapconfirmed (motionevent e): Click event. Used to determine that the click is Singletap instead of DOUBLETAP, assuming that two consecutive clicks are doubletap gestures,
Assuming a single click, the system waits for a period of time without receiving a second click to determine that the click is Singletap instead of DOUBLETAP and then triggers the Singletapconfirmed event.
A little bit different about onsingletapconfirmed and Onsingletapup: Ongesturelistener has this one method Onsingletapup, and Onsingletapconfirmedeasy confused.
The difference between the two is: Onsingletapup, only to lift the hand will run, and for onsingletapconfirmed, assuming double-click, then onsingletapconfirmed will not run.
2. Rewrite
Ondoubletaplistener 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;
}
)
It is possible to add a detailed processing method to the function according to the need, and then pass the Setondoubletaplistener to the Gesturedetector.
Gesturedetector.setondoubletaplistener (Ondoubletaplistener);
Three. Simpleongesturelistener
Simpleongesturelistener is an inner class of the Gesturedetector class, which is a static class, which means that it is actually an outer one. The ability to inherit the class externally, overriding the gesture handling method inside.
1.SimpleOnGestureListener actually implements the Ongesturelistener and Ondoubletaplistener, so it can complete all of the above mentioned gesture recognition (9 kinds), 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);
}
}
It is possible to add a detailed processing method to the function as required, and then pass the constructor to the Gesturedetector.
Gesturedetector gesturedetector=new Gesturedetector (This,simpleongesturelistener);
Four. There is another way of thinking
To achieve capture screen gestures, in addition to creating gesturedetector in the activity, another way of thinking: Build a overlay, this overlay implement Ongesturelistener interface, Make it maintain its own gesturedetector.
By adding this overlay to the main view and passing in the corresponding listener, you can realize the function of capturing gestures.




These 2 things I have been unclear, has been misty, today is free, and looked at the next, it seems that some understand, wrote a test code tested under, but still not very clear. This document was introduced in this article: http://blog.csdn.net/android_tutor/article/details/7193090.
First, this article is only for events triggered by touch.

Android events: OnClick, onscroll, onfling, etc., are made up of a lot of touch. The first state of touch is definitely a action_down, which means that the screen is pressed. After that, touch will have a possible event, which may be:

Action_move//represented as move gesture

ACTION_UP//indicates leaving the screen

Action_cancel//indicates that the cancellation gesture is not generated by the user, but is generated by the program

A action_down, n Action_move, and 1 action_up, make up many of the events in Android.

In Android, there is a class of controls that can also include other child controls, which are inherited from the ViewGroup class, such as the ListView, Gallery, GridView.

Another type of control is no longer able to include child controls, such as: TextView.

The main discussion object of this article is the event firing situation when the control of the ViewGroup class is nested.

For a control of the ViewGroup class, there is a very important method, that is, onintercepttouchevent (), which handles the event and alters the direction of the event's delivery, and its return value is a Boolean value that determines whether the touch event will continue to be passed to the child view it includes. This method is passed from the parent view to the child view.

The method, Ontouchevent (), is used to receive and process the event, and its return value is also a Boolean value that determines whether the event and perhaps event continue to pass upward, which is passed from the child view to the parent view.

The transfer mechanism of the touch event between Onintercepttouchevent () and ontouchevent and each childview depends entirely on the return value of Onintercepttouchevent () and Ontouchevent (). A return value of TRUE indicates that the event was received and processed correctly, and a return value of FALSE indicates that the event was not processed and will continue to be passed.

The Action_down event is passed to the onintercepttouchevent of a ViewGroup class, assuming that it returns false, the down event continues to pass Onintercepttouchevent to the child ViewGroup class. If a child view is not a control of the ViewGroup class, it is passed to its ontouchevent.

Assuming Onintercepttouchevent returns True, the down event is passed to its ontouchevent, no longer passed, and the subsequent event is passed to its ontouchevent.

Assuming that the ontouchevent of a view returns false, the down event continues to pass to the ontouchevent of its parent ViewGroup class, and if true is returned, the event may be passed directly to its ontouchevent to continue processing. (perhaps events are only passed to the ontouchevent that Action_down returns true for the necessary event)

To summarize: Onintercepttouchevent is able to 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

Motionevent events in Onintercepttouchevent (), ontouchevent () Order of delivery
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



Android gestures & Touch Event motionevent

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.