Android touch and gesture operations GestureDetector

Source: Internet
Author: User

Android touch and gesture operations GestureDetector
 

Today's smartphones dare not say that all of them are touch screens. They should also be touch screens over 9%. Touch Screens bring a lot of convenience to our mobile phone system without a keyboard or a mouse. When you touch the screen, many touch events, such as down, up, and move, are generated. The View class has an internal interface of View. OnTouchListener. By Rewriting its onTouch (View v, MotionEvent event) method, we can handle some touch events as follows:

 

public class MainActivity extends Activity {...// This example shows an Activity, but you would use the same approach if// you were subclassing a View.@Overridepublic boolean onTouchEvent(MotionEvent event){     int action = MotionEventCompat.getActionMasked(event);    switch(action) {        case (MotionEvent.ACTION_DOWN) :            Log.d(DEBUG_TAG,Action was 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 was 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 current screen element);            return true;              default :             return super.onTouchEvent(event);    }      }


 

The events provided by OnTouch are relatively simple. If you need to handle complicated gestures, using this interface will be very troublesome, because we need to determine what gestures are based on the user's touch track. The Android sdk provides us with the GestureDetector (Gesture: Gesture Detector: Recognition) class, through which we can recognize a lot of gestures.

 

Public class GestureDetector extends Object

Java. lang. Object

Android. view. GestureDetector

GestureDetector belongs to the android. view package. android also provides the android. gesture package to support more gesture operations. We will introduce it later. The official introduction uses GestureDetectorCompat to process Gesture Recognition. Why does GestureDetectorCompat replace GestureDetector? The official explanation is as follows:

 

GestureDetectorCompat instantiation has the following two methods:

 

The GestureDetector class provides two external interfaces: OnGestureListener, OnDoubleTapListener, and an internal class SimpleOnGestureListener. The SimpleOnGestureListener class is a class that GestureDetector provides us with a more convenient response to different gestures, it implements the above two interfaces. The class is static class, that is to say, it is actually an external class. We can inherit this class from the outside and override the gesture Processing Method in it. Therefore, there are two methods to implement Gesture Recognition: one is to implement the OnGestureListener interface, and the other is to use the SimpleOnGestureListener class.

OnGestureListener has the following actions:

Press (onDown): the moment when the finger touched the touch screen, it was the touch.

 

OnFling: the action of quickly moving and releasing fingers on the touch screen.

 

OnLongPress: the finger is pressed for a period of time and has not been released.

 

OnScroll: slide your finger on the touch screen.

 

OnShowPress: the finger is pressed on the touch screen, and its time range is effective after being pressed.

 

OnSingleTapUp: the moment when your finger leaves the touch screen.

 

To use the OnGestureListener interface, you need to reload All the methods of the OnGestureListener interface, which is suitable for listening to All Gestures, as mentioned in the official document "Detecing All Supported Gestures ".

 

public class MainActivity extends Activity implements         GestureDetector.OnGestureListener,        GestureDetector.OnDoubleTapListener{    private static final String DEBUG_TAG = Gestures;    private GestureDetectorCompat 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.OnGestureListener        mDetector = new GestureDetectorCompat(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 call 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,             float velocityX, float velocityY) {        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, float distanceX,            float distanceY) {        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;    }}


This will cause some gesture actions that we cannot use, but we need to reload them. The emergence of the SimpleOnGestureListener class solves this problem for us. If you want to "Detecting a Subset of Supported Gestures", SimpleOnGestureListener is the best choice.

 

 

public class MainActivity extends Activity {     private GestureDetectorCompat mDetector;     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mDetector = new GestureDetectorCompat(this, new MyGestureListener());    }    @Override     public boolean onTouchEvent(MotionEvent event){         this.mDetector.onTouchEvent(event);        return super.onTouchEvent(event);    }    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {        private static final String 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,                 float velocityX, float velocityY) {            Log.d(DEBUG_TAG, onFling:  + event1.toString()+event2.toString());            return true;        }    }}


 

Finally, we also explain two problems:

1. Why does onTouchEvent use MotionEventCompat instead of MotionEvent. Because MotionEventCompat makes more actions adapt to API 4.

2. How to use gestures in Android view:

 

 View myView = findViewById(R.id.my_view);         myView.setOnTouchListener(new OnTouchListener() {            public boolean onTouch(View v, MotionEvent event) {                // ... Respond to touch events                       this.mDetector.onTouchEvent(event);                return super.onTouchEvent(event);            }        });

 

 

 

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.