Thanks to the many pioneers of Android Chinese developers, the main content of this article comes from the summary, a small part of which is my own experience. ReferencesArticleIs:
Http://www.williamhua.com/2009/04/23/android-touchscreen-gesture-recogniton/
Http://goro.iteye.com/blog/402163
Unlike the traditional click touch screen, Android touch screen has some gesture gestures, such as fling and scroll. These gesture greatly improves user experience. In Android, gesture recognition (detector) is implemented through the gesturedetector. ongesturelistener interface.
First, the Android event processing mechanism is implemented based on listener. For example, a touch screen event is implemented through ontouchlistener;
Secondly, all view subclasses can add listener for a certain type of events by using methods such as setontouchlistener () and setonkeylistener;
Third, listener is generally provided in interface mode, which contains one or more abstract methods. We need to implement these methods to complete ontouch (), onkey (), and other operations. In this way,ProgramYou can use the callback function to give an appropriate response when a specific event is sent to the view by dispatch.
1. Touch Screen click example
Java Code
-
- Public class mygesture extends activity implements ontouchlistener {
-
- Public void oncreate (bundle savedinstancestate ){
-
- Super. oncreate (savedinstancestate );
-
- Setcontentview (R. layout. Main );
-
- Textview TV = (textview) findviewbyid (R. Id. TV );
-
- TV. setontouchlistener (This );
-
- }
- Public Boolean ontouch (view V, motionevent event ){
-
- Toast. maketext (This, "Touch touch", Toast. length_short). Show ();
-
- Return false;
-
- }
-
- }
We can use the getaction () method of motionevent to obtain the touch event type, including action_down (by pressing the touch screen), action_move (by pressing the touch screen), action_up (by releasing the touch screen) and action_cancel (not directly triggered by the user ). After obtaining coordinates using getrawx (), getrawy (), getx (), getx (), and Gety () methods based on different user operations, we can implement operations such as dragging a button, drag a scroll bar.
2. Back to today's focus, how can we identify users' gesture when capturing touch operations? Here we need help from the gesturedetector. ongesturelistener interface. The Code is as follows:
Java code
- Public class mygesture extends activity implements ontouchlistener, ongesturelistener {
-
- Private gesturedetector mgesturedetector;
-
- Public mygesture (){
-
- Mgesturedetector =New gesturedetector (this );
-
- }
-
- Public void oncreate (bundle savedinstancestate ){
-
- Super. oncreate (savedinstancestate );
-
- Setcontentview (R. layout. Main );
- Textview TV = (textview) findviewbyid (R. Id. TV );
-
- TV. setontouchlistener (This );
-
- TV. setfocusable (True );
-
- TV. setclickable (True );
-
- TV. setlongclickable (True );
-
- Mgesturedetector. setislongpressenabled (True );
-
- }
-
-
- /*
- * In the ontouch () method, we call the ontouchevent () method of gesturedetector and deliver the captured motionevent to gesturedetector.
-
- * To analyze whether there is a proper callback function to process users' gestures
-
- */
-
- Public Boolean ontouch (view V, motionevent event ){
-
- Return mgesturedetector. ontouchevent (event );
-
- }
-
-
- // Touch the touch screen, triggered by one motionevent action_down
-
- Public Boolean ondown (motionevent arg0 ){
- Log. I ("Mygesture", "ondown ");
-
- Toast. maketext (This, "ondown", Toast. length_short). Show ();
-
- Return true;
-
- }
-
-
- /*
-
- * If you touch the touch screen, you have not released or dragged it. It is triggered by one motionevent action_down.
-
- * Pay attention to the difference from ondown (), emphasizing that the status is not released or dragged.
-
- */
- Public void onshowpress (motionevent e ){
-
- Log. I ("Mygesture", "onshowpress ");
-
- Toast. maketext (This, "onshowpress", Toast. length_short). Show ();
-
- }
-
-
- // The user (after touching the touch screen) is released and triggered by one motionevent action_up
-
- Public Boolean onsingletapup (motionevent e ){
-
- Log. I ("Mygesture", "onsingletapup ");
- Toast. maketext (This, "onsingletapup", Toast. length_short). Show ();
-
- Return true;
-
- }
-
-
- // The user presses the touch screen and moves quickly before releasing it. It is triggered by one motionevent action_down, multiple action_move, and one action_up.
-
- Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy ){
-
- Log. I ("Mygesture", "onfling ");
- Toast. maketext (This, "onfling", Toast. length_long). Show ();
-
- Return true;
-
- }
-
-
- // Press the touch screen and drag it. It is triggered by one motionevent action_down and multiple action_move operations.
-
- Public Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey ){
-
- Log. I ("Mygesture", "onscroll ");
- Toast. maketext (This, "onscroll", Toast. length_long). Show ();
-
- Return true;
-
- }
-
-
- // The user presses the touch screen for a long time and is triggered by multiple motionevent action_down
-
- Public void onlongpress (motionevent e ){
-
- Log. I ("Mygesture", "onlongpress ");
-
- Toast. maketext (This, "onlongpress", Toast. length_long). Show ();
- }
-
- }
3. fling event processing code: Except for the first action_down that triggers fling and the coordinates contained in the last action_move, we can also use the user's moving speed on the X or Y axis as a condition. For example, in the following code, we can only process a user moving more than 100 pixels and moving more than 200 pixels per second on the X axis.
Java code
-
- Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy ){
-
- // Parameter explanation:
-
- // E1: 1st action_down motionevent
-
- // E2: The last action_move motionevent
-
- // Velocityx: moving speed on the X axis, pixel/second
- // Velocityy: The moving speed on the Y axis, pixel/second
-
-
- // Trigger condition:
-
- // The coordinate displacement of the X axis is greater than fling_min_distance, and the movement speed is greater than that of fling_min_velocity pixels/s.
-
-
- Final int fling_min_distance = 100, fling_min_velocity = 200;
-
- If (e1.getx ()-e2.getx ()> fling_min_distance & math. Abs (velocityx)> fling_min_velocity ){
-
- // Fling left
- Log. I ("Mygesture", "fling left ");
-
- Toast. maketext (This, "fling left", Toast. length_short). Show ();
-
- }Else if (e2.getx ()-e1.getx ()> fling_min_distance & math. Abs (velocityx)> fling_min_velocity ){
-
- // Fling right
-
- Log. I ("Mygesture", "fling right ");
-
- Toast. maketext (This, "fling right", Toast. length_short). Show ();
- }
-
- Return false;
-
- }
In this example,TV. setlongclickable (
True
) Is required because
Only in this way can the view process be different from the hold (action_move, or multiple action_down) of the TAP (touch). We can also achieve this through Android: longclickable in the layout definition.
4. simpleongesturelistener
Java code
-
- Public class mygesture extends activity implements ontouchlistener {
-
- Private gesturedetector mgesturedetector;
-
- Public mygesture (){
- Mgesturedetector =New gesturedetector (New mysimplegesture ());
-
- }
-
- Public void oncreate (bundle savedinstancestate ){
-
- Super. oncreate (savedinstancestate );
-
- Setcontentview (R. layout. Main );
-
- Textview TV = (textview) findviewbyid (R. Id. TV );
-
- TV. setontouchlistener (This );
- TV. setfocusable (True );
-
- TV. setclickable (True );
-
- TV. setlongclickable (True );
-
- }
-
- Public Boolean ontouch (view V, motionevent event ){
-
- If (event. getaction () = motionevent. action_up ){
-
- Log. I ("Mygesture", "motionevent. action_up ");
-
- }
- Return mgesturedetector. ontouchevent (event );
-
- }
-
-
- // Simpleongesturelistener implements gesturedetector. ondoubletaplistener, gesturedetector. ongesturelistener
-
- Private class mysimplegesture extends simpleongesturelistener {
-
- // Triggered when the second touch is down.
-
- Public Boolean ondoubletap (motionevent e ){
- Log. I ("Mygesture", "ondoubletap ");
-
- Return super. ondoubletap (E );
-
- }
-
-
- // Both touch down and up will be triggered under double-click, which can be differentiated by E. getaction ().
-
- Public Boolean ondoubletapevent (motionevent e ){
-
- Log. I ("Mygesture", "ondoubletapevent ");
- Return super. ondoubletapevent (E );
-
- }
-
-
- // Triggered when touch is down
-
- Public Boolean ondown (motionevent e ){
-
- Log. I ("Mygesture", "ondown ");
-
- Return super. ondown (E );
-
- }
-
- // Triggered when up is triggered when a sliding distance is reached.
-
- Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy ){
-
- Log. I ("Mygesture", "onfling ");
-
- Return super. onfling (E1, E2, velocityx, velocityy );
-
- }
-
- // Triggered when touch is down without moving
-
- Public void onlongpress (motionevent e ){
-
- Log. I ("Mygesture", "onlongpress ");
-
- Super. onlongpress (E );
-
- }
-
-
- // Triggered when sliding is reached
- Public Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey ){
-
- Log. I ("Mygesture", "onscroll ");
-
- Return super. onscroll (E1, E2, distancex, distancey );
-
- }
-
-
- /*
-
- * Triggered when touch is not sliding
- * (1) ondown must be triggered immediately as long as touch down
-
- * (2) after the touch is down, the onshowpress is triggered before the onlongpress is triggered.
-
- * So: do not slide after the touch is down. ondown-> onshowpress-> onlongpress is triggered in this order.
-
- */
-
- Public void onshowpress (motionevent e ){
-
- Log. I ("Mygesture", "onshowpress ");
- Super. onshowpress (E );
-
- }
-
-
- /*
-
- * Both functions are triggered when the touch is down without moving (onscroll) or long-pressed (onlongpress ).
-
- * Click very fast (do not slide) touch up: ondown-> onsingletapup-> onsingletapconfirmed
-
- * Click touch up: ondown-> onshowpress-> onsingletapup-> onsingletapconfirmed.
- */
-
- Public Boolean onsingletapconfirmed (motionevent e ){
-
- Log. I ("Mygesture", "onsingletapconfirmed ");
-
- Return super. onsingletapconfirmed (E );
-
- }
-
- Public Boolean onsingletapup (motionevent e ){
- Log. I ("Mygesture", "onsingletapup ");
-
- Return super. onsingletapup (E );
-
- }
-
- }
-
- }