The login
registered
Write an article
Home page
Download the APP
Android GestureDetector: The GestureDetector App You Must Learn
Carson_Ho
Android GestureDetector: The GestureDetector App You Must Learn
preface
- Gesture recognition in
Android
The applications developed are very common - today
carson
I will explain it to you in detailAndroid
Gesture recognition class:GestureDetector
Class usage. (including examples)
directory
Introduction to the
Below, I will introduce the usage interface & usage class of GestureDetector in detail with examples.
Interface 1: OnGestureListener
1. The role
Detect the following user operations on the screen: press momentarily, press, long press, tap, quickly slide the screen, and drag
2. Procedure
// Step 1: Create gesture detector instance & pass in OnGestureListener interface (need to duplicate corresponding method) // there are 3 constructors, Commonly used is the second / / 1. GestureDetector GestureDetector = new GestureDetector (GestureDetector. OnGestureListener listener); // 2. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener); // 3. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener); GestureDetector mGestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() { // 1. Public Boolean onDown(MotionEvent e) {log. I ("MyGesture", "onDown"); return false; } // 2. The user touches the touch screen lightly and does not release or drag // the difference with onDown() : no release/drag // that is: When the user clicks, onDown () is executed, Public void onShowPress(MotionEvent e) {log. I ("MyGesture", "onShowPress"); public void onShowPress(MotionEvent e) {log. I ("MyGesture", "onShowPress"); Public void onLongPress(MotionEvent e) {log. I ("MyGesture", "onLongPress"); } public Boolean onSingleTapUp(MotionEvent e) {log. I ("MyGesture", "onSingleTapUp");} public Boolean onSingleTapUp(MotionEvent e) {log. I ("MyGesture", "onSingleTapUp"); return true; } public Boolean onScroll(MotionEvent E1, MotionEvent e2, float distanceX, float distanceY) { Log.i("MyGesture", "onScroll:"); return true; } // 6. User press the touch screen, move quickly and release // parameters: // e1: first ACTION_DOWN MotionEvent // e2: last ACTION_MOVE MotionEvent // velocityX: Speed of movement on the X axis, pixels per second // velocityY: The velocity of movement on the Y axis, Public Boolean onFling(MotionEvent E1, MotionEvent e2, float velocityX, float velocityY) {log. I ("MyGesture", "onFling"); return true; }}); // Step 2-1: Make a View detect gestures - rewrite the View's onTouch function and hand the View's touch event to the GestureDetector. View.setontouchlistener (new view.onTouchListener () {@override public Boolean onTouch(View v, MotionEvent event) { mGestureDetector.onTouchEvent(event); return true; // Return true to receive touch events completely}}); // Step 2-2: Make an Activity detect gestures: Rewrite the Activity's dispatchTouchEvent function to send the touchscreen event to the GestureDetector to handle, To respond to the user gestures @ Override public Boolean dispatchTouchEvent (MotionEvent ev) {mGestureDetector. OnTouchEvent (ev); // Make GestureDetector respond to touch events super.dispatchTouchEvent(EV); // Make the Activity respond to the touch event return false; }Copy the code
3. Examples
Now perform gesture detection on a TextView activity_main.xml
<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="600dp" android:text="carson_ho Test" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>Copy the code
MainActivity.java
public class MainActivity extends AppCompatActivity { TextView mTextView; GestureDetector mGestureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Step 1: MGestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() { // 1. Public Boolean onDown(MotionEvent e) {log. I ("MyGesture", "onDown"); return false; } // 2. The user touches the touch screen lightly and does not release or drag // the difference with onDown() : no release/drag // that is: When the user clicks, onDown () is executed, Public void onShowPress(MotionEvent e) {log. I ("MyGesture", "onShowPress"); public void onShowPress(MotionEvent e) {log. I ("MyGesture", "onShowPress"); Public void onLongPress(MotionEvent e) {log. I ("MyGesture", "onLongPress"); } public Boolean onSingleTapUp(MotionEvent e) {log. I ("MyGesture", "onSingleTapUp");} public Boolean onSingleTapUp(MotionEvent e) {log. I ("MyGesture", "onSingleTapUp"); return true; } public Boolean onScroll(MotionEvent E1, MotionEvent e2, float distanceX, float distanceY) { Log.i("MyGesture", "onScroll:"); return true; } // 6. User press the touch screen, move quickly and release // parameters: // e1: first ACTION_DOWN MotionEvent // e2: last ACTION_MOVE MotionEvent // velocityX: Speed of movement on the X axis, pixels per second // velocityY: The velocity of movement on the Y axis, Public Boolean onFling(MotionEvent E1, MotionEvent e2, float velocityX, float velocityY) {log. I ("MyGesture", "onFling"); return true; }}); // Step 2: Let TextView detect gestures: MTextView = (TextView) findViewById(R.id.extView); mTextView = (TextView) findViewById(R.ID.extView); mTextView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { mGestureDetector.onTouchEvent(event); return false; }}); }}Copy the code
4. The schematic
I tested it by making a series of gestures on the screen
Interface 2: OnDoubleTapListener
1. The role
Detect that the user clicks or double-clicks the screen
2. Procedure
// Step 1: Create gesture detector instance If the OnDoubleTapListener interface is used, the GestureDetector interface needs to be used. If the OnDoubleTapListener interface is used, the GestureDetector interface must be passed to the OnGestureListener interface. You must also implement the OnGestureListener interface // there are three constructors, Commonly used is the second / / 1. GestureDetector GestureDetector = new GestureDetector (GestureDetector. OnGestureListener listener); // 2. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener); // 3. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener); GestureDetector mGestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() { // 1. Public Boolean onDown(MotionEvent e) {log. I ("MyGesture", "onDown"); return false; } // 2. The user touches the touch screen lightly and does not release or drag // the difference with onDown() : no release/drag // that is: When the user clicks, onDown () is executed, Public void onShowPress(MotionEvent e) {log. I ("MyGesture", "onShowPress"); public void onShowPress(MotionEvent e) {log. I ("MyGesture", "onShowPress"); Public void onLongPress(MotionEvent e) {log. I ("MyGesture", "onLongPress"); } public Boolean onSingleTapUp(MotionEvent e) {log. I ("MyGesture", "onSingleTapUp");} public Boolean onSingleTapUp(MotionEvent e) {log. I ("MyGesture", "onSingleTapUp"); return true; } public Boolean onScroll(MotionEvent E1, MotionEvent e2, float distanceX, float distanceY) { Log.i("MyGesture", "onScroll:"); return true; } // 6. User press the touch screen, move quickly and release // parameters: // e1: first ACTION_DOWN MotionEvent // e2: last ACTION_MOVE MotionEvent // velocityX: Speed of movement on the X axis, pixels per second // velocityY: The velocity of movement on the Y axis, Public Boolean onFling(MotionEvent E1, MotionEvent e2, float velocityX, float velocityY) {log. I ("MyGesture", "onFling"); return true; }}); // Step 2: Create & set OnDoubleTapListener interface implementation class mGestureDetector. SetOnDoubleTapListener (new GestureDetector. OnDoubleTapListener () {/ / 1. Click event / / about OnDoubleTapListener onSingleTapConfirmed () and OnGestureListener onSingleTapUp () / / onSingleTapConfirmed: the difference between // onSingleTapUp: // onSingleTapUp: Public Boolean onSingleTapConfirmed(MotionEvent e) {log. I ("MyGesture", "onSingleTapConfirmed"); return false; Public Boolean onDoubleTap(MotionEvent e) {log. I ("MyGesture", "onDoubleTap"); return false; } // other actions that occur between double clicks after the onDoubleTap is triggered, including down, up, and Move events; public boolean onDoubleTapEvent(MotionEvent e) { Log.i("MyGesture", "onDoubleTapEvent"); return false; }}); // Step 3-1: Make a View detect gestures - rewrite the View's onTouch function and hand the View's touch event to the GestureDetector. View.setontouchlistener (new view.onTouchListener () {@override public Boolean onTouch(View v, MotionEvent event) { mGestureDetector.onTouchEvent(event); return true; // Return true to receive touch events completely}}); // Step 3-2: Make an Activity detect gestures: Rewrite the Activity's dispatchTouchEvent function to send the touchscreen event to the GestureDetector to handle, To respond to the user gestures @ Override public Boolean dispatchTouchEvent (MotionEvent ev) {mGestureDetector. OnTouchEvent (ev); // Make GestureDetector respond to touch events super.dispatchTouchEvent(EV); // Make the Activity respond to the touch event return false; }Copy the code
3. Examples
Now perform gesture detection on a TextView activity_main.xml
<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="600dp" android:text="carson_ho Test" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>Copy the code
MainActivity.java
public class MainActivity extends AppCompatActivity { TextView mTextView; GestureDetector mGestureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Step 1: MGestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() { // 1. Public Boolean onDown(MotionEvent e) {log. I ("MyGesture1", "onDown"); return false; } // 2. The user touches the touch screen lightly and does not release or drag // the difference with onDown() : no release/drag // that is: When the user clicks, onDown () is executed, Public void onShowPress(MotionEvent e) {log. I ("MyGesture", "onShowPress"); public void onShowPress(MotionEvent e) {log. I ("MyGesture", "onShowPress"); Public void onLongPress(MotionEvent e) {log. I ("MyGesture", "onLongPress"); } public Boolean onSingleTapUp(MotionEvent e) {log. I ("MyGesture", "onSingleTapUp");} public Boolean onSingleTapUp(MotionEvent e) {log. I ("MyGesture", "onSingleTapUp"); return true; } public Boolean onScroll(MotionEvent E1, MotionEvent e2, float distanceX, float distanceY) { Log.i("MyGesture", "onScroll:"); return true; } // 6. User press the touch screen, move quickly and release // parameters: // e1: first ACTION_DOWN MotionEvent // e2: last ACTION_MOVE MotionEvent // velocityX: Speed of movement on the X axis, pixels per second // velocityY: The velocity of movement on the Y axis, Public Boolean onFling(MotionEvent E1, MotionEvent e2, float velocityX, float velocityY) {log. I ("MyGesture", "onFling"); return true; }}); // Step 2: Create & set OnDoubleTapListener interface implementation class mGestureDetector. SetOnDoubleTapListener (new GestureDetector. OnDoubleTapListener () {/ / 1. Click event / / about OnDoubleTapListener onSingleTapConfirmed () and OnGestureListener onSingleTapUp () / / onSingleTapConfirmed: the difference between // onSingleTapUp: // onSingleTapUp: Public Boolean onSingleTapConfirmed(MotionEvent e) {log. I ("MyGesture", "onSingleTapConfirmed"); return false; Public Boolean onDoubleTap(MotionEvent e) {log. I ("MyGesture", "onDoubleTap"); return false; } // other actions that occur between double clicks after the onDoubleTap is triggered, including down, up, and Move events; public boolean onDoubleTapEvent(MotionEvent e) { Log.i("MyGesture", "onDoubleTapEvent"); return false; }}); MTextView = (TextView) findViewById(R.id.extView); mTextView = (TextView) findViewById(R.id.extView); mTextView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { mGestureDetector.onTouchEvent(event); return true; }}); }}Copy the code
4. Test the effect
The log effect is as follows
Use the class: SimpleOnGestureListener
1. The role
The gesture detection function of the two interfaces is integrated
2. Differences between the two interfaces
- Functions in the OnGestureListener and OnDoubleTapListener interfaces are mandatory overridden
- The SimpleOnGestureListener class, on the other hand, can be optionally overwritten, because the SimpleOnGestureListener class already implements all of the interface functions, except that they are empty
3. Procedure
// Step 1: Create gesture detector instance // there are three constructors, Here is the third / / 1. GestureDetector GestureDetector = new GestureDetector (GestureDetector. OnGestureListener listener); // 2. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener); // 3. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener); GestureDetector mGestureDetector = new GestureDetector(this, New GestureDetector. SimpleOnGestureListener () {/ / / 1 / OnGestureListener interface function. Public Boolean onDown(MotionEvent e) {log. I ("MyGesture1", "onDown"); return false; } // 2. The user touches the touch screen lightly and does not release or drag // the difference with onDown() : no release/drag // that is: When the user clicks, onDown () is executed, Public void onShowPress(MotionEvent e) {log. I ("MyGesture", "onShowPress"); public void onShowPress(MotionEvent e) {log. I ("MyGesture", "onShowPress"); Public void onLongPress(MotionEvent e) {log. I ("MyGesture", "onLongPress"); } public Boolean onSingleTapUp(MotionEvent e) {log. I ("MyGesture", "onSingleTapUp");} public Boolean onSingleTapUp(MotionEvent e) {log. I ("MyGesture", "onSingleTapUp"); return true; } public Boolean onScroll(MotionEvent E1, MotionEvent e2, float distanceX, float distanceY) { Log.i("MyGesture", "onScroll:"); return true; } // 6. User press the touch screen, move quickly and release // parameters: // e1: first ACTION_DOWN MotionEvent // e2: last ACTION_MOVE MotionEvent // velocityX: Speed of movement on the X axis, pixels per second // velocityY: The velocity of movement on the Y axis, Public Boolean onFling(MotionEvent E1, MotionEvent e2, float velocityX, float velocityY) {log. I ("MyGesture", "onFling"); return true; } // OnDoubleTapListener function // 1. Click event / / about OnDoubleTapListener onSingleTapConfirmed () and OnGestureListener onSingleTapUp () / / onSingleTapConfirmed: the difference between // onSingleTapUp: // onSingleTapUp: Public Boolean onSingleTapConfirmed(MotionEvent e) {log. I ("MyGesture", "onSingleTapConfirmed"); return false; Public Boolean onDoubleTap(MotionEvent e) {log. I ("MyGesture", "onDoubleTap"); return false; } // other actions that occur between double clicks after the onDoubleTap is triggered, including down, up, and Move events; public boolean onDoubleTapEvent(MotionEvent e) { Log.i("MyGesture", "onDoubleTapEvent"); return false; }}); // Step 2-1: Make a View detect gestures - rewrite the View's onTouch function and hand the View's touch event to the GestureDetector. View.setontouchlistener (new view.onTouchListener () {@override public Boolean onTouch(View v, MotionEvent event) { mGestureDetector.onTouchEvent(event); return true; // Return true to receive touch events completely}}); // Step 2-2: Make an Activity detect gestures: Rewrite the Activity's dispatchTouchEvent function to send the touchscreen event to the GestureDetector to handle, To respond to the user gestures @ Override public Boolean dispatchTouchEvent (MotionEvent ev) {mGestureDetector. OnTouchEvent (ev); // Make GestureDetector respond to touch events super.dispatchTouchEvent(EV); // Make the Activity respond to the touch event return false; }Copy the code
4. Examples
Now perform gesture detection on a TextView activity_main.xml
<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="600dp" android:text="carson_ho Test" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>Copy the code
MainActivity.java
public class MainActivity extends AppCompatActivity { TextView mTextView; GestureDetector mGestureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Step 1: MGestureDetector = new GestureDetector(this, New GestureDetector. SimpleOnGestureListener () {/ / / 1 / OnGestureListener interface function. Public Boolean onDown(MotionEvent e) {log. I ("MyGesture1", "onDown"); return false; } // 2. The user touches the touch screen lightly and does not release or drag // the difference with onDown() : no release/drag // that is: When the user clicks, onDown () is executed, Public void onShowPress(MotionEvent e) {log. I ("MyGesture", "onShowPress"); public void onShowPress(MotionEvent e) {log. I ("MyGesture", "onShowPress"); Public void onLongPress(MotionEvent e) {log. I ("MyGesture", "onLongPress"); } public Boolean onSingleTapUp(MotionEvent e) {log. I ("MyGesture", "onSingleTapUp");} public Boolean onSingleTapUp(MotionEvent e) {log. I ("MyGesture", "onSingleTapUp"); return true; } public Boolean onScroll(MotionEvent E1, MotionEvent e2, float distanceX, float distanceY) { Log.i("MyGesture", "onScroll:"); return true; } // 6. User press the touch screen, move quickly and release // parameters: // e1: first ACTION_DOWN MotionEvent // e2: last ACTION_MOVE MotionEvent // velocityX: Speed of movement on the X axis, pixels per second // velocityY: The velocity of movement on the Y axis, Public Boolean onFling(MotionEvent E1, MotionEvent e2, float velocityX, float velocityY) {log. I ("MyGesture", "onFling"); return true; } // OnDoubleTapListener function // 1. Click event / / about OnDoubleTapListener onSingleTapConfirmed () and OnGestureListener onSingleTapUp () / / onSingleTapConfirmed: the difference between // onSingleTapUp: // onSingleTapUp: Public Boolean onSingleTapConfirmed(MotionEvent e) {log. I ("MyGesture", "onSingleTapConfirmed"); return false; Public Boolean onDoubleTap(MotionEvent e) {log. I ("MyGesture", "onDoubleTap"); return false; } // other actions that occur between double clicks after the onDoubleTap is triggered, including down, up, and Move events; public boolean onDoubleTapEvent(MotionEvent e) { Log.i("MyGesture", "onDoubleTapEvent"); return false; }}); MTextView = (TextView) findViewById(R.id.extView); mTextView = (TextView) findViewById(R.id.extView); mTextView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { mGestureDetector.onTouchEvent(event); return true; }}); }}Copy the code
5. Test the effect
The log effect is as follows
At this point, the usage of the Android gesture recognition class GestureDetector class is explained.
conclusion
- This paper mainly focuses on
Android
Gesture recognition class:GestureDetector
The use of classes is fully explained - And I’m going to continue with that
Android
Development of relevant knowledge, interested students can continue to pay attention to my blogDevelopment notes for Carson_Ho
Thumb up, please! Because your encouragement is the biggest power that I write!
The Android event distribution mechanism is the most comprehensive and easy to understand solution for Android screen adaptation. It is the most comprehensive and easy to understand solution for Android screen adaptation. Android development: JSON introduction and the most comprehensive analysis method! BroadcastReceiver Is the most comprehensive version of Android’s BroadcastReceiver
Welcome to attentionCarson_HoJane books!
Share the dry things about Android development from time to time, the pursuit of short, flat, fast, but there is no lack of depth.
Recommended readingMore highlights
- Android Retrofit is one of the hottest libraries in the Android Web request library. Android Retrofit is one of the hottest libraries in the Android Web request library. Carson_Ho
- Android development (9) | Android gestures to development Similar to Java IO (input/output) programming, Android offers file IO openFileOutput and openFil… Ye Chen _
- Android gesture detection — GestureDetector comprehensive analysis Reference: inflammation of the armoured email: [email protected] blog: http://blog.csdn.net/to… Inflammation of the armoured
- Android gesture detection, mainly is related to the content of the GestureDetector usage and precautions, this article still belongs to the event processing this… The duke lu
- After reading this article can not GestureDetector gesture detection, I kneel to rub the clothes board! Introduction In the process of Android development, we often need to monitor some gestures, such as: click, double click, long press, slide, zoom, etc. At this time also… _yuanhao