Android gesture Manipulation recognition

Source: Internet
Author: User

abstract First, in Android, each gesture interaction is performed in the following order. 1. Touch the touch screen flash, triggering a motionevent event. 2. The event is ontouchlistener monitored to obtain the Motionevent object in its Ontouch () method. 3. Forward secondary motionevent objects via Gesturedetector (gesture recognizer)

First, in an Android system, each gesture interaction is performed in the following order.

1. Touch the touch screen flash, triggering a motionevent event.

2. The event was ontouchlistener monitored and the Motionevent object was obtained in its Ontouch () method.

3. Forwards the secondary Motionevent object to Ongesturelistener via Gesturedetector (gesture recognizer).

4. Ongesturelistener obtains the object and listens to the information encapsulated by the object to make appropriate feedback.

This order can be said to be the principle of gesture interaction, the following together to learn about Motionevent, Gesturedetector and Ongesturelistener.

Motionevent: This class is used to encapsulate action events such as gestures, touch pens, trackball, and so on. It internally encapsulates two important attributes X and Y, which are used to record the coordinates of the horizontal and vertical axes, respectively.

Gesturedetector: identifies various gestures.

Ongesturelistener: This is a listener interface for gesture interaction, which provides several abstract methods and calls the corresponding method according to the gesturedetector gesture recognition result.

I'm going to show you the implementation of the gesture interaction by a code example that toggles the picture, so that we have a deeper understanding and memory of the order of execution and the distinction between gestures.

First, provide a layout file that has only ImageView--main.xml.

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " >     <imageview android:id= "@+id/image" android:layout_width= "fill_parent" android:layout_height= "Fill_ Parent "android:layout_gravity=" center "/> </LinearLayout>


Then, complete our activity, because to listen to touch screen touch events and gesture time, so the activity must implement Ontouchlistener and Ongesturelistener two interfaces, and override the methods. The specific code is as follows:

public class Mainactivity extends Activity implements Ontouchlistener, Ongesturelistener {//Create a Gesturedetector to identify the pack Object Waiyuwu.blogcn.com Private Gesturedetector detector = new Gesturedetector (this); Define an array to put pretty girl int[] girls = new INT[]{R.DRAWABLE.GIRL1, R.DRAWABLE.GIRL2, r.drawable.girl3}; Define array subscript to facilitate viewing of individual girls private int index;    Private ImageView image; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); Image = (ImageView) Findviewbyid (r.id.image); Set an initial display of the Girl Bar Image.setimageresource (Girls[index]); Monitor the touch screen time Image.setontouchlistener (this) on this ImageView component; 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. Image.setlongclickable (TRUE); Detector.setislongpressenabled (TRUE); }//the way to shout the next girl public void GoNext () {index++; index = math.abs (index% girls.length); Image.setimageresource (girls[ Index]); }//Override Ontouchlistener's Ontouch method//This method is called when the touch screen is touched, that is, touch events (touching and stroking two events, pretty image). @Override public boolean OnTouch (View V,Motionevent event) {detector.ontouchevent (event); return true;}    Called when the action is pressed @Override public boolean ondown (Motionevent e) {return false;} Called @Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) when throwing an action {//velocit YX represents the horizontal movement, switching the girl if (Velocityx < 0) {GoNext () according to the direction of the finger movement;} else if (Velocityx > 0) {goprevious ();} return false; }//user calls the previous girl's method public void Goprevious () {index--; index = math.abs (index% girls.length); Image.setimageresource (Girl S[index]); }//is called @Override public void onlongpress (Motionevent e) {}//on a long-time call @Override public boolean onscroll (Motionev    ENT E1, motionevent E2, float Distancex, float Distancey) {return false;} Called when pressed @Override public void onshowpress (Motionevent e) {}//is called when lifted @Override public boolean onsingletapup (Motione Vent e) {return false;}}


When I first started learning Android, I felt that Google's documents were not good, and when we studied gestures, I felt that Google's documents were too poor. A lot of constants, properties, and methods don't even have a description. There is no description, but there are so many gestures in the ongesturelistener, it does not have a description, who can understand the relationship and difference between onlongpress and Onshowpress,onscroll and onfling before attempting to do it continuously? Google really needs to do a big surgery on the documentation. But fortunately, after my repeated attempts. These gestures are defined from a personal point of view.

    • Press (Ondown): Just the moment the finger touches the touch screen, it is the touch of the moment.

    • Throw (onfling): The finger moves quickly on the touchscreen and releases the action.

    • Long Press (onlongpress): The finger is pressed for a period of time and is not loosened.

    • Scrolling (onscroll): Fingers slide on the touchscreen.

    • Press and Hold (onshowpress): The finger is pressed on the touchscreen, its time range is pressed, and before the long press.

    • Lift (Onsingletapup): The moment the finger leaves the touch screen.

In addition to these definitions, I also summed up a bit of experience, and here to share with you.

    • Any gesture action will first perform a pressing (Ondown) action.

    • Press and hold (onshowpress) action before long pressing (onlongpress) action.

    • A lift (Onsingletapup) action is performed after holding (onshowpress) the action and pressing (Ondown) the action.

    • Hold (onlongpress), scroll (onscroll), and throw (onfling) actions do not perform a lift (onsingletapup) action.

 

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.