Android event processing mechanism is implemented based on Listener. For example, touch screen-related events are implemented through OnTouchListener; while gestures are implemented through OnGestureListener, so what is the relationship between the two?
OnTouchListener
The OnTouchListener interface contains an onTouch() method, and look at an example directly:
public class MainActivity extends Activity implements OnTouchListener { public void onCreate(Bundle outState) { (outState); setContentView(); TextView tv = (TextView) findViewById(); (this); } public boolean onTouch(View v, MotionEvent event) { (this, "Touch Touch", Toast.LENGTH_SHORT).show(); return false ; } }
We can use the getAction() method of MotionEvent to get the type of the Touch event, including ACTION_DOWN (press the touch screen), ACTION_MOVE (move the force point after pressing the touch screen), ACTION_UP (release the touch screen) and ACTION_CANCEL (not directly touched by the user). With the help of judgment of different users' operations, combined with methods such as getRawX(), getRawY(), getX() and getY() to obtain coordinates, we can implement functions such as dragging a certain button and dragging a scroll bar.
You can see that OnTouchListener can only listen to three types of touch events, namely, press, move, and release. If you want to listen to complex gesture operations such as double-click, slide, and long-press, you must use OnGestureListener.
OnGestureListener
Following the above example, this time adding gesture monitoring:
public class MainActivity extends Activity implements OnTouchListener, OnGestureListener { private GestureDetector mGestureDetector; public void onCreate(Bundle outState) { (outState); setContentView(); mGestureDetector = new GestureDetector(this); TextView tv = (TextView) findViewById(); (this); } public boolean onTouch(View v, MotionEvent event) { return (event); } // The user touches the touch screen, triggered by 1 MotionEvent ACTION_DOWN public boolean onDown(MotionEvent arg0) { ("MyGesture", "onDown"); (this, "onDown", Toast.LENGTH_SHORT).show(); return true; } // The user touches the touch screen and has not yet been released or dragged. It is triggered by a MotionEvent ACTION_DOWN. Pay attention to the difference between it and onDown(), which emphasizes the state of not being released or dragged. public void onShowPress(MotionEvent e) { ("MyGesture", "onShowPress"); (this, "onShowPress", Toast.LENGTH_SHORT).show(); } // The user (after touching the touch screen) releases, triggered by a MotionEvent ACTION_UP public boolean onSingleTapUp(MotionEvent e) { ("MyGesture", "onSingleTapUp"); (this, "onSingleTapUp", Toast.LENGTH_SHORT).show(); return true; } // The user presses the touch screen, moves quickly and releases it. It is triggered by 1 MotionEvent ACTION_DOWN, multiple ACTION_MOVE, and 1 ACTION_UP public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { ("MyGesture", "onFling"); (this, "onFling", Toast.LENGTH_LONG).show(); return true; } // The user presses the touch screen and drags it, triggered by 1 MotionEvent ACTION_DOWN, multiple ACTION_MOVE public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { ("MyGesture", "onScroll"); (this, "onScroll", Toast.LENGTH_LONG).show(); return true; } // The user presses and presses the touch screen for a long time, and is triggered by multiple MotionEvent ACTION_DOWN public void onLongPress(MotionEvent e) { ("MyGesture", "onLongPress"); (this, "onLongPress", Toast.LENGTH_LONG).show(); } }
The above article discusses the difference between OnTouchListener and OnGestureListener in detail is all the content I share with you. I hope you can give you a reference and I hope you can support me more.