Android Official Development Document Training Series Course Chinese version: Gesture processing monitoring universal gestures

Source: Internet
Author: User

Original address: http://android.xsoftlab.net/training/gestures/index.html

Introduction

This lesson will learn how to make the user interact with the app through touch gestures. Android offers a number of related APIs to help you create and detect gestures.

Although the app should not use touch gestures as a basic input feature, touch gestures can make apps fast and easy to manipulate and appeal.

To provide a consistent, intuitive user experience, apps should use Android's universal touch gestures standard.

Detect Universal gestures

Touch events occur when a user places one or more fingers on the touchscreen, and the application needs to interpret this touch behavior as a special gesture event. The following steps are performed to detect gesture events:

    • 1. Collect data about the touch events.
    • 2. Interpret the data to see if any standard gestures are supported by the program.
Collect Data

When the user places his finger on the screen, this will call back the Ontouchevent () method of the corresponding view.

The gesture begins when the user first touches the screen, then the system tracks the position of the finger and ends with the last event when the finger leaves the screen. Throughout this interaction, the Motionevent object is distributed by the Ontouchevent () method and provides detailed information about each event. The app can use the data provided by the Motionevent object to check if any of the events that the app cares about occur.

Capturing touch events for activity or view

To intercept activity or view touch events, you need to rewrite their ontouchevent () callback methods.

The following code uses the Getactionmasked () method to extract the user execution behavior that is contained in the event parameter. It provides the raw data that you care about:

 Public  class mainactivity extends Activity {...//This example shows a Activity, but do would use the same approach if//You were subclassing a View.@Override Public Boolean ontouchevent(Motionevent event) {intAction = motioneventcompat.getactionmasked (event);Switch(action) { Case(Motionevent.action_down): Log.d (Debug_tag,"Action is Down");return true; Case(motionevent.action_move): Log.d (Debug_tag,"Action was MOVE");return true; Case(MOTIONEVENT.ACTION_UP): Log.d (Debug_tag,"Action is Up");return true; Case(motionevent.action_cancel): Log.d (Debug_tag,"Action was CANCEL");return true; Case(motionevent.action_outside): Log.d (Debug_tag,"movement occurred outside Bounds"+"Of the current screen element");return true;default:return Super. Ontouchevent (event); }      }
Make a single view capture touch events

In addition to the Ontouchevent () method, you can also use View.ontouchlistener to listen for touch gestures. This makes it possible to listen for touch events without rewriting ontouchevent ():

View myView = findViewById(R.id.my_view); myView.setOnTouchListener(new OnTouchListener() {    publicbooleanonTouch(View v, MotionEvent event) {        // ... Respond to touch events               returntrue;    }});

Be aware that the listener you are creating returns False when the Action_down event occurs. If you do, then the listener will not be called for a series of events such as Action_move and action_up. This is because the Action_down event is the starting point for all touch events.

If you created a custom view, you can rewrite the Ontouchevent () method as described above.

Detect gestures

Android provides the Gesturedetector class to detect generic gestures. These gestures include Ondown (), onlongpress (), onfling (), and so on. You can use Gesturedetector in conjunction with Ontouchevent ().

Check for all supported gestures

When you instantiate a Gesturedetectorcompat object, one of the parameters needs to implement the Gesturedetector.ongesturelistener interface.

The purpose of the Gesturedetector.ongesturelistener interface is to notify the user when a specified touch event occurs. In order for the Gesturedetector object to receive touch events, you need to rewrite the view or activity's ontouchevent () method and pass all the events to the Gesturedetector object.

In the following code, the true returned by the Ontouchevent () method indicates that you are responsible for handling this touch event. A return value of FALSE indicates that you want to ignore this event until the touch event is processed.

Run the following code to find the feeling: how each behavior is triggered when you are working on the touchscreen, and what the contents of the Motionevent object are for each touch event. You will realize how large a simple touch event is generated by data processing.

 Public  class mainactivity extends Activity implementsGesturedetector . Ongesturelistener, gesturedetector. Ondoubletaplistener {             Private Static FinalString Debug_tag ="Gestures";PrivateGesturedetectorcompat Mdetector;//called when the activity is first created.    @Override     Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Instantiate the gesture detector with the        //application context and an implementation of        //Gesturedetector.ongesturelistenerMdetector =NewGesturedetectorcompat ( This, This);//Set The gesture detector as the double tap        //listener.Mdetector.setondoubletaplistener ( This); }@Override      Public Boolean ontouchevent(Motionevent event) { This. Mdetector.ontouchevent (event);//Be sure to the superclass implementation        return Super. Ontouchevent (event); }@Override     Public Boolean Ondown(Motionevent event) {LOG.D (Debug_tag,"Ondown:"+ event.tostring ());return true; }@Override     Public Boolean onfling(Motionevent event1, Motionevent Event2,floatVelocityx,floatVelocityy) {LOG.D (Debug_tag,"onfling:"+ event1.tostring () +event2.tostring ());return true; }@Override     Public void onlongpress(Motionevent event) {LOG.D (Debug_tag,"onlongpress:"+ event.tostring ()); }@Override     Public Boolean onscroll(Motionevent E1, motionevent E2,floatDistancex,floatDistancey) {LOG.D (Debug_tag,"Onscroll:"+ e1.tostring () +e2.tostring ());return true; }@Override     Public void onshowpress(Motionevent event) {LOG.D (Debug_tag,"onshowpress:"+ event.tostring ()); }@Override     Public Boolean Onsingletapup(Motionevent event) {LOG.D (Debug_tag,"Onsingletapup:"+ event.tostring ());return true; }@Override     Public Boolean Ondoubletap(Motionevent event) {LOG.D (Debug_tag,"Ondoubletap:"+ event.tostring ());return true; }@Override     Public Boolean ondoubletapevent(Motionevent event) {LOG.D (Debug_tag,"Ondoubletapevent:"+ event.tostring ());return true; }@Override     Public Boolean onsingletapconfirmed(Motionevent event) {LOG.D (Debug_tag,"onsingletapconfirmed:"+ event.tostring ());return true; }}
Detect a subset of supported gestures

If you just want to handle several gestures, then you can inherit the Gesturedetector.simpleongesturelistener interface.

Gesturedetector.simpleongesturelistener provides all the implementations for the Ontouchevent () method, so you can only rewrite the methods you care about. For example, in the following code, you create a class that inherits the Gesturedetector.simpleongesturelistener interface, and then rewrite its onfling () method and the Ondown () method.

Whether or not you use the Gesturedetector.ongesturelistener interface, the best practice point is to rewrite the Ondown () method that returns true. This is because all gestures start with ondown (). If False is returned in the Ondown () method, As Gesturedetector.simpleongesturelistener does by default, the system will assume that you want to ignore the rest of the gesture and that the other methods of the Gesturedetector.ongesturelistener interface will not be called. This will bury a potentially imperceptible problem within the app. If you confirm that you want to ignore the entire gesture flow, the result false in Ondown () will be the only chance.

 Public  class mainactivity extends Activity {     PrivateGesturedetectorcompat Mdetector;@Override     Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Mdetector =NewGesturedetectorcompat ( This,NewMygesturelistener ()); }@Override      Public Boolean ontouchevent(Motionevent event) { This. Mdetector.ontouchevent (event);return Super. Ontouchevent (event); } class Mygesturelistener extends Gesturedetector.simpleongesturelistener {Private Static FinalString Debug_tag ="Gestures";@Override         Public Boolean Ondown(Motionevent event) {LOG.D (Debug_tag,"Ondown:"+ event.tostring ());return true; }@Override         Public Boolean onfling(Motionevent event1, Motionevent Event2,floatVelocityx,floatVelocityy) {LOG.D (Debug_tag,"onfling:"+ event1.tostring () +event2.tostring ());return true; }    }}

Android Official Development Document Training Series Course Chinese version: Gesture processing monitoring universal gestures

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.