Android L (5.0) Gesturedetector of gesture recognition

Source: Internet
Author: User

I novice, recently under the Android L source code, is studying gesture recognition, limited ability, now summarized as follows:

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, If you need to deal with some complex gestures, it can be cumbersome to use this interface (because we want to determine what gesture is 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 Introduction:

1. Composition

The Gesturedetector class is used to identify the various gestures of the touchscreen, which contains two interfaces and two internal classes:

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.

Gesturehandler: Inherited the handler, this is the 5.0 new inner class

2. Construction

3. Methods

4. Use

Process:

First, the system captures the touch event of the screen (Ontouchlistener), which does not involve a specific gesture, but simply captures the touch. Next, call Gesturedetector's Ontouchevent () method in the Ontouch () method to give the captured motionevent to gesturedetector for processing. Finally, you need to implement an abstract method.

Realize:

(1) Create Gesturedetector instance gesturedetector in activity.

(2) can choose according to the need:

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 activity's Ontouchevent method, give all touch events to Gesturedetector to handle

public boolean ontouchevent (Motionevent event) {
Return Gesturedetector.ontouchevent (event);
}

Two. Interface Ongesturelistener

The 1.onGestureListener recognizes 6 gestures, namely:
(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.

A little bit different about onfling and onscroll.
Onfling () is a fling, which is performed when a motionevent.action_up (finger lift) occurs, and onscroll () is executed as long as the finger moves. He will not execute motionevent.action_up. Onfling are often used for page flipping, while onscroll is typically 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;
}
}
3. You can add specific processing methods to the function as needed. Then pass in the Gesturedetector through the constructor function.
Gesturedetector gesturedetector=new Gesturedetector (This,ongesturelistener);

Three. Interface 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): Additional 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, if the consecutive click is Doubletap gesture, if only click once, the system waits for a period of time not received a second click to determine that the click is Singletap instead of Doubletap, The Singletapconfirmed event is then triggered.

A little bit different about onsingletapconfirmed and Onsingletapup: Ongesturelistener has such a way of Onsingletapup, and onsingletapconfirmed is easy to confuse. the difference between the two is : Onsingletapup, as long as the hand is lifted up will be executed, and for onsingletapconfirmed, if the double-click, then onsingletapconfirmed will not execute.

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;
}
)

You can add specific processing methods to the function as needed, and then pass the Setondoubletaplistener to the Gesturedetector.
Gesturedetector.setondoubletaplistener (Ondoubletaplistener);

Four. Internal class Simpleongesturelistener

Android L (5.0) Gesturedetector of gesture recognition

Related Article

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.