"Andorid------gesture Recognition" gesturedetector and Simpleongesturelistener use tutorial (GO)--

Source: Internet
Author: User

From:http://www.cnblogs.com/transmuse/archive/2010/12/02/1894833.html

1. When the user touches the screen, there will be many gestures, such as down,up,scroll,filing and so on, 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, it will be cumbersome to use this interface (because we have to determine the user's touch on the trajectory of what is the gesture) Android The SDK provides us with the Gesturedetector (Gesture: Gesture Detector: Recognition) class, which allows us to identify a lot of gestures, mainly through his ontouchevent (event) method, to complete the identification of different gestures. Although he can identify gestures, but what to do with different gestures, should be provided to the programmer to implement, so this class provides two interfaces externally: Ongesturelistener,ondoubletaplistener, There is also an inner class Simpleongesturelistener,simpleongesturelistener class that Gesturedetector provides us with a more convenient response to different gesture classes, This class implements both interfaces (but all of the method bodies are empty), which is the static class, meaning that it is actually an external class. The programmer can inherit this class externally, overriding the gesture handling method inside.

The Simpleongesturelistener object can be passed in by Gesturedetector's construction method, so that gesturedetector can handle different gestures.

2. Specific usage:

2.1

1 Private classDefaultgesturelistenerextendssimpleongesturelistener{2 @Override3          Public BooleanOnsingletapup (motionevent e) {4             return false;5         }6 @Override7          Public voidonlongpress (motionevent e) {8             9         }Ten         /** One          * @paramE1 the first down motion event, that started the scrolling. A            @paramE2 The move motion event triggered the current onscroll. -            @paramDistancex the distance along the X axis (axis) that have been scrolled since the last call to Onscroll. This is not the distance between E1 and E2. -            @paramDistancey the distance along the Y axis that had been scrolled since the last call to Onscroll. This is not the distance between E1 and E2. the Whether you drag the view by hand, or the action is to throw the scroll, will be triggered many times, this method in Action_move action occurs when the Gesturedetector Ontouchevent method is triggered to see the source  -          * */ - @Override -          Public BooleanOnscroll (motionevent E1, motionevent E2, +                 floatDistancex,floatDistancey) { -             return false; +         } A         /** at          * @paramE1 1th Action_down motionevent and only one -          * @paramE2 last Action_move motionevent -          * @paramVelocityx movement speed on x-axis, pixels per second -          * @paramvelocityy movement speed on y-axis, pixels per second - * This method will only trigger the Ontouchevent method to see Gesturedetector when it occurs in action_up. -          *  in          * */ - @Override to          Public BooleanOnfling (motionevent E1, motionevent E2,floatVelocityx, +                 floatvelocityy) { -             return false; the         } * @Override $          Public voidonshowpress (motionevent e) {Panax Notoginseng              -         } the @Override +          Public BooleanOndown (motionevent e) { A             return false; the         } + @Override -          Public BooleanOndoubletap (motionevent e) { $             return false; $         } - @Override -          Public Booleanondoubletapevent (motionevent e) { the             return false; -         }Wuyi         /** the * This method is different from Onsingletapup, he is in Gesturedetector convinced that the user after the first touch of the screen, not immediately following the second touch screen, that is, not "double-click" When the trigger -          * */ Wu @Override -          Public Booleanonsingletapconfirmed (motionevent e) { About             return false; $         } -          -}

2.2 Public Gesturedetector (context context, Gesturedetector.ongesturelistener listener) hand gesture responses to gesture recognition classes by constructing methods

1 Private New Ontouchlistener () {2        @Override3public         boolean OnTouch (View V, motionevent event) {4             return gdetector.ontouchevent (event); 5         }6     };

OK, it's over.

Problems encountered:

1. Onfling (* *) cannot be triggered

By setting Mlistview.setlongclickable (TRUE), you can (I'm dealing with a ListView gesture event), so that the view is able to handle the hold (i.e. Action_move, which is different from tap) or multiple Action_down), we can also do this through the android:longclickable in the layout definition.

2. When the user presses the phone screen, the long-press event is triggered, and the up event is triggered when the screen is left, but Simpleongesturelistener does not provide an interface to the up event of the Longpress event

Workaround:

Similar to this, intercept up events, because all are ontouchlistener first obtained, and then passed to Simpleongesturelistener, here is a point to note:

After we have intercepted the up event and we have processed it, we have to give the event to Simpleongesturelistener processing, although we only intercept the up of the long-press event, but the Simpleongesturelistener has also done some processing for the long press event up , but there is no external interface available.

What to do with it:

1 if (minlongpress) {2                mhandler.removemessages (TAP); 3                 false ; 4 }

If you do not give the Simpleongesturelistener processing, the Click action will also trigger the Onlongpress method.

1 PrivateOntouchlistener Gesturetouchlistener =NewOntouchlistener () {2 @Override3          Public BooleanOnTouch (View V, motionevent event) {4             Switch(Event.getaction ()) {5              CaseMotionevent.action_down:6                 returngdetector.ontouchevent (event);7             Casemotionevent.action_up:8Mygesture.flaginfo info =mgesture.getflaginfo ();9                 if(info.isconnected==true){Ten                     intFirstvisibleposition =mlistview.getfirstvisibleposition (); OneView view = Mlistview.getchildat (info.position-firstvisibleposition); A                     if(view!=NULL){ - View.setbackgroundresource (r.drawable.listitem_background_blue); -info.isconnected =false; the                     } -                 } -                 returngdetector.ontouchevent (event); -              CaseMotionevent.action_move: +                 returngdetector.ontouchevent (event); -             } +             return false; A              at         } -};

Summarize:

1. Click on the execution of an item on the screen there are two cases, one is short time, one time is slightly longer

Time is short: Ondown--------"Onsingletapup--------" onsingletapconfirmed

A little longer: Ondown--------"onshowpress------" Onsingletapup--------"onsingletapconfirmed

2. Long Press Events

Ondown--------"onshowpress------" onlongpress

3. Toss: After the finger touches the screen, slightly swipe and release immediately

Ondown-----"onscroll----" onscroll----"onscroll----" ...----->onfling

4. Drag

Ondown------"onscroll----" onscroll------"onfiling

Note: Sometimes it triggers onfiling, but sometimes it does not trigger, and personal understanding is caused by the non-standard movement of people.

"Andorid------gesture Recognition" gesturedetector and Simpleongesturelistener use tutorial (GO)--

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.