Tutorials for using gesturedetector and Simpleongesturelistener

Source: Internet
Author: User

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

Private classDefaultgesturelistener extends simpleongesturelistener{@Override PublicBoolean onsingletapup (Motionevent e) {return false; } @Override Public voidonlongpress (Motionevent e) {}/** * @param e1 The first down motion event, that started the scrolling.           @param E2 The move motion event triggered the current onscroll. @param Distancex 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. @param distancey 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. Whether you drag the view by hand, or the action is to throw the scroll, will be triggered many times, this method will trigger the Action_move action when the Gesturedetector Ontouchevent method source **/@Override Publicboolean onscroll (motionevent E1, motionevent E2,floatDistancex,floatDistancey) {            return false; }        /** * @param e1 1th Action_down motionevent and only one * @param E2 last action_move motionevent * @pa Ram Velocityx X-axis movement speed, pixels per second * @param velocityy y-axis movement speed, pixels per second * This method occurs at Action_up to trigger the Gesturedetector O Ntouchevent method source Code * **/@Override PublicBoolean onfling (Motionevent E1, motionevent E2,floatVelocityx,floatvelocityy) {            return false; } @Override Public voidonshowpress (Motionevent e) {} @Override PublicBoolean Ondown (Motionevent e) {return false; } @Override PublicBoolean Ondoubletap (Motionevent e) {return false; } @Override PublicBoolean ondoubletapevent (Motionevent e) {return false; }, which is not a "double click" Trigger **/@Override PublicBoolean onsingletapconfirmed (Motionevent e) {return false; }            } 

2.2

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

2.3

In the Ontouch method of Ontouchlistener

Private New Ontouchlistener () {        @Override        publicevent) {            return Gdetector.ontouchevent (event);        }    };

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:

if (minlongpress) {
Mhandler.removemessages (TAP);
Minlongpress = false;

}

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

Private Ontouchlistener Gesturetouchlistener = new Ontouchlistener () {
@Override
public boolean OnTouch (View V, motionevent event) {
Switch (event.getaction ()) {
Case Motionevent.action_down:
Return Gdetector.ontouchevent (event);
Case MOTIONEVENT.ACTION_UP:
Mygesture.flaginfo info = Mgesture.getflaginfo ();
if (info.isconnected==true) {
int firstvisibleposition = Mlistview.getfirstvisibleposition ();
View view = Mlistview.getchildat (info.position-firstvisibleposition);
if (view!=null) {
View.setbackgroundresource (R.drawable.listitem_background_blue);
info.isconnected = false;
}
}
Return Gdetector.ontouchevent (event);
Case Motionevent.action_move:
Return Gdetector.ontouchevent (event);
}
return false;

}
};

Summarize:

1 . Tap the execution flow of an item on the screen  there are two cases, one is short time, one time is short: Ondown--------"Onsingletapup--------" Onsingletapconfirmed time: Ondown--------"onshowpress------" Onsingletapup--------"onsingletapconfirmed  2. Long press event Ondown--------"onshowpress------" onlongpress3. Toss: When the finger touches the screen, Loosen ondown-----"onscroll----" onscroll----"onscroll----" ....----->onfling4 drag ondown------"onscroll----" onscroll------"onfiling Note: Sometimes it triggers onfiling, but sometimes it doesn't. Personal understanding is caused by the non-standard of human action. 

Tutorials for using gesturedetector and Simpleongesturelistener

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.