Android gesture Operation Programming detailed _android

Source: Internet
Author: User
Tags abs

Gesture manipulation offers a different experience in our use of smart devices. Android is bound to be programmed for gesture manipulation. So what is the principle of it? How do we do gesture manipulation programming?

The principle of gesture operation

First of all, in the Android system, each gesture interaction is executed in the following order.

1. Contact screen Flash, triggering a motionevent event.

2. The event is Ontouchlistener listening and obtains the Motionevent object in its Ontouch () method.

3. Forwards the 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 operation.

Gesture manipulation classes and interfaces

Let's take a look at motionevent, Gesturedetector and Ongesturelistenertogether.

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

Gesturedetector: Identify various gestures.

Ongesturelistener: This is a gesture interaction listening interface, which provides a number of abstract methods, and according to the gesturedetector gesture recognition results call the corresponding method.

Gestures Operation Example

Below I pass a code example which toggles the beautiful picture, demonstrates the gesture interaction realization, lets the everybody to the above execution order, as well as each gesture movement's distinction has a more profound understanding and the memory.

First, provide a ImageView layout file--main.xml.

xml/html Code

<?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, to complete our activity, because we want to listen to touch events and gesture time on the touchscreen, it must implement the Ontouchlistener and Ongesturelistener two interfaces and rewrite the methods. The specific code is as follows:

Java code

public class Mainactivity extends activity implements Ontouchlistener, Ongesturelistener {//Create a gesture to identify the pack  
   Detector Object Waiyuwu.blogcn.com Private Gesturedetector detector = new Gesturedetector (this);  
   Define an array to put the beautiful girl int[] girls = new INT[]{R.DRAWABLE.GIRL1, R.DRAWABLE.GIRL2, r.drawable.girl3};  
   Define array subscript to facilitate viewing of each girl's 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 Girl bar Image.setimageresource (Girls[index]);  
    Monitor the touch screen time on this ImageView component Image.setontouchlistener (this);  
    Here are two things to remember, or you won't be able to handle events other than light touch, such as throwing motions.  
    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]);  
     }//The user calls a girl's method public void Goprevious () {index--;  
     index = math.abs (index% girls.length);  
   Image.setimageresource (Girls[index]);  
   //Rewrite the Ontouchlistener Ontouch method//This method is invoked when the touch screen is touched, that is, when a touch event occurs (touching and stroking two events, a pretty image).  
     @Override public boolean Ontouch (View V, motionevent event) {detector.ontouchevent (event);  
   return true;  
   is called @Override public boolean ondown (Motionevent e) {return False when the action is pressed;  
       Called @Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, when throwing the action)  
     Float velocityy) {//velocityx represents lateral movement, switching the girl if (Velocityx < 0) {GoNext () according to the direction of the finger movement;  
     }else if (Velocityx > 0) {goprevious ();  
   return false;  
   @Override public void onlongpress (Motionevent e) {}//is called on the long time @Override PublicBoolean onscroll (Motionevent E1, motionevent E2, float Distancex, float Distancey) {return false;  
   @Override public void onshowpress (Motionevent e) {}//is invoked when holding down @Override  
   public boolean onsingletapup (Motionevent e) {return false;  
 }  
 }

The meaning of each method of gesture operation

When I first started to learn Android, I thought Google's documents were bad, and when I was studying gestures, I felt that Google's documents were really bad. Many constants, properties, and methods are not even described. There is no description, but ongesturelistener gestures so much, it also does not have an introduction to explain, in the absence of continuous before trying, who can understand onlongpress and onshowpress,onscroll and onfling relationship and difference? Google really needs to do a big surgery on the document. But fortunately, after my repeated attempts. Define these gestures from a personal point of view.

    1. Press (Ondown): The moment when the finger touches the touch screen, is the touch of that.
    2. Toss (onfling): The finger moves quickly on the touch screen and loosens.
    3. Long Press (onlongpress): The finger presses for a period of time and does not loosen.
    4. Scrolling (onscroll): The finger slides on the touch screen.
    5. Press and Hold (onshowpress): The finger presses on the touch screen, its time range is pressed to work, before the long press.
    6. Lift (Onsingletapup): The moment the finger leaves the touch screen.

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

Any gesture action will perform a press (Ondown) action first.

    1. You must perform a hold (onshowpress) action before the Long Press (onlongpress) action.
    2. Hold (onshowpress) action and press (Ondown) action to perform a lift (onsingletapup) action.
    3. The lift (Onsingletapup) action is not performed after the long Press (onlongpress), scrolling (onscroll), and throwing (onfling) actions.

The above is the android gesture operation of the data collation, follow-up to continue to supplement the relevant knowledge, thank you for your support for this site!

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.