How to Use gesturedetector and simpleongesturelistener

Source: Internet
Author: User

1. when you touch the screen, many gestures are generated, such as Down, up, scroll, and filing. 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, using this interface will be very troublesome (because we need to determine the gesture based on the user's touch track). The Android SDK provides us with the gesturedetector (gesture: gesture detector: Recognition) class, through this class, we can recognize a lot of gestures, mainly through its ontouchevent (event) method to recognize different gestures. Although he can recognize gestures, how to handle different gestures should be providedProgramThis class provides two external interfaces: ongesturelistener, ondoubletaplistener, and an internal class simpleongesturelistener, the simpleongesturelistener class is a class provided by gesturedetector for us to easily respond to different gestures. This class implements the above two interfaces (but all method bodies are empty ), this class is static class, that is, it is actually an external class. Programmers can inherit this class externally and rewrite the gesture processing method.

Through the construction method of gesturedetector, you can pass the simpleongesturelistener object, so that gesturedetector can process different gestures.

2. Usage:

2.1

Private class defaultgesturelistener extends simpleongesturelistener {

@ Override
Public Boolean onsingletapup (motionevent e ){
Return false;
}

@ Override
Public void onlongpress (motionevent e ){

}
/**
* @ Param E1 the first down motion event that started the scrolling.
@ Param E2 the move motion event that triggered the current onscroll.
@ Param distancex the distance along the X axis (axis) that has 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 has 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 scroll by throwing the action, it will be triggered multiple times. This method will trigger when the action_move action occurs. See the source code of the ontouchevent method of gesturedetector.
**/
@ Override
Public Boolean onscroll (motionevent E1, motionevent E2,
Float distancex, float distancey ){
Return false;
}
/**
* @ Param E1 1st action_down motionevent and only one
* @ Param E2 the last action_move motionevent
* @ Param velocityx the moving speed on the X axis, pixel/second
* @ Param velocityy the moving speed on the Y axis, pixel/second
* This method triggers the ontouchevent method source code of the gesturedetector when action_up occurs.
*
**/
@ Override
Public Boolean onfling (motionevent E1, motionevent E2, float velocityx,
Float velocityy ){
Return false;
}

@ Override
Public void onshowpress (motionevent e ){

}
@ Override
Public Boolean ondown (motionevent e ){
Return false;
}
@ Override
Public Boolean ondoubletap (motionevent e ){
Return false;
}
@ Override
Public Boolean ondoubletapevent (motionevent e ){
Return false;
}
/**
* Unlike onsingletapup, this method is triggered when gesturedetector is sure that the user is behind the screen for the first time without following the screen for the second touch.
**/
@ Override
Public Boolean onsingletapconfirmed (motionevent e ){
Return false;
}

}

 

2.2 Public gesturedetector (context, gesturedetector. ongesturelistener listener) hand over the gesture response to the gesture recognition class through the Construction Method

2.3 In the ontouch method of ontouchlistener

Private ontouchlistener gesturetouchlistener = new ontouchlistener (){
@ Override
Public Boolean ontouch (view V, motionevent event ){
Return gdetector. ontouchevent (event );
}
};

 

OK. It will end now.

Problems:

1. onfling (***) cannot be triggered

By setting mlistview. setlongclickable (true); (the listview gesture event is handled). Only in this way can the view process the hold (action_move, or multiple action_down), we can also achieve this through Android: longclickable in the layout definition.

2. If you press the phone screen for a long time, the long press event is triggered. When you exit the screen, the up event is triggered. However, simpleongesturelistener does not provide an interface for the up event of the longpress event.

Solution:

In this way, the up event is intercepted because all the events are obtained first by ontouchlistener and then passed to simpleongesturelistener. Note the following:

After we have intercepted the up event, we must hand it over to the simpleongesturelistener for processing. Although we only intercept the up of the long-press event, however, simpleongesturelistener does some processing for the Long-press event up, but does not provide external interfaces.

What has been done:

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

}

If you do not submit the parameter to simpleongesturelistener, The onlongpress method is triggered when you click the parameter.

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;

}
};

Summary:

1. There are two scenarios for clicking an item on the screen: Short Time and short time.

Short Time: ondown -------- onsingletapup -------- onsingletapconfirmed

A little longer:Ondown -------- onshowpress ------ onsingletapup -------- onsingletapconfirmed

2. Long press event

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

3. Throw: After the screen is touched by your finger, slide a little and release immediately.

Ondown -----, onscroll ----... -----> Onfling

4. Drag

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

Note: Sometimes the onfiling will be triggered, but sometimes it will not. My personal understanding is that the human action is not standard.

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.