Android Learning (16) gesture recognition via Gesturedetector

Source: Internet
Author: User

First, the gesture interaction process:

1) trigger the Motionevent event when touching the screen.

2) be Ontouchlistener listening, get Motionevent object in Ontouch ().

3) Gesturedetector forwards Motionevent object to Ongesturelistener.

4) Ongesturelistener obtains the object and makes appropriate feedback based on the information encapsulated by the object.

II. classes and interfaces that need to be used

  1, Motionevent:

1) used to encapsulate gestures, touch pens, trackball and other action events.

2) The internal package is used to record the properties X and y of the horizontal and vertical axis coordinates.


  2, Gesturedetector:

Identify various gestures: press, move, bounce.

  3. Ongesturelistener interface

1) The listener interface for gesture interaction, which provides multiple abstract methods.

A) Ondown (Motionevent e); Click

b) Onsingletapup (Motionevent e); Lift up

c) onshowpress (Motionevent e); Short Press

d) onlongpress (Motionevent e); Long Press

e) onscoll (motionevent e1,motionevent e2,float distancex,float Distancey)//Scroll

f) onfling (motionevent e1,motionevent e2,float velocityx,float velocityy)//Sliding

  2) The corresponding method is called according to the gesturedetector gesture recognition result.

4. Ondoubletaplistener Interface:

A) Ondoubletap (Motionevent e); Double click

b) ondoubletapevent (Motionevent e); Double-click to press and lift each trigger once

c) onsingletapconfirmed (Motionevent e); Click Confirm, press and pop quickly, but do not click Second.

5, Simpleongestturelistener class, implements the Ongesturelistener and Ondoubletaplistener interface, if we need to implement gestures, only need to inherit this class, to implement the corresponding gesture method can be.


Example: directly implement the interface by dragging and dropping the image according to the left and right

Main.xml, add a imageview display picture, display the first picture by default

<LinearLayoutxmlns:android= "Http://"Xmlns:tools= "Http://"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " >    <ImageViewAndroid:id= "@+id/img_girl"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:src= "@drawable/girl1" /></LinearLayout>

 PackageCom.example.gesturedetectordemo;ImportAndroid.os.Bundle;;ImportAndroid.view.GestureDetector;ImportAndroid.view.GestureDetector.OnGestureListener;ImportAndroid.view.GestureDetector.SimpleOnGestureListener;ImportAndroid.view.Menu;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.View.OnTouchListener;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivityImplementsOntouchlistener, Ongesturelistener {//defines a Gesturedetector object to handle various gestures and implements the Ontouchlistener interfaceGesturedetector Mygesturedetector; //Picture FrameImageView Imggirl; //an array of pictures of girls stored    int[] Girls ={r.drawable.girl1, r.drawable.girl2, R.drawable.girl3, R.drawable.girl4, R.DRAWABLE.GIRL5}; //Storing array subscripts    Private intindex; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mygesturedetector=NewGesturedetector ( This); Imggirl=(ImageView) Findviewbyid (; //Add Ontouchlistener EventImggirl.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. Imggirl.setlongclickable (true); Mygesturedetector.setislongpressenabled (true); }    //Move Backwards     Public voidGoNext () {index++; Index= Math.Abs (index%girls.length);    Imggirl.setimageresource (Girls[index]); }    //move forward     Public voidgoprevious () {index--; Index= Math.Abs (index%girls.length);    Imggirl.setimageresource (Girls[index]); } @Override//handling User Touch Requests     Public BooleanOnTouch (View V, motionevent event) {//turn it over to Gesturedetector to handle it.mygesturedetector.ontouchevent (event); return false; } @Override//called when an action is pressed     Public BooleanOndown (motionevent e) {//GoNext ();        return false; } @Override//press the action to release the call     Public voidonshowpress (motionevent e) {//TODO auto-generated Method Stub} @Override//be called when bouncing     Public BooleanOnsingletapup (motionevent e) {//TODO auto-generated Method Stub        return false; } @Override//is called when scrolling     Public BooleanOnscroll (motionevent E1, motionevent E2,floatDistancex,floatDistancey) {        //TODO auto-generated Method Stub        return false; } @Override//be called on long and on time     Public voidonlongpress (motionevent e) {//TODO auto-generated Method Stub} @Override//be called when throwing an action     Public BooleanOnfling (motionevent E1, motionevent E2,floatVelocityx,floatvelocityy) {        if(E1.getx ()-e2.getx () > 50) {GoNext (); Toast.maketext (mainactivity. This, "Drag from right to left" +index, Toast.length_short). Show (); } Else if(E2.getx ()-e1.getx () > 50) {goprevious (); Toast.maketext (mainactivity. This, "Drag from left to right" +index, Toast.length_short). Show (); }        return false; }}


Android Learning (16) gesture recognition via Gesturedetector

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: 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.