Usage of the GestureDetector class

The GestureDetector class defines a number of touch events . Including 
   1.boolean onDoubleTap(MotionEvent e) explanation: triggered when the second double-click touch down 
   2.boolean onDoubleTapEvent(MotionEvent e) explanation: the second double-click touch down and up will be triggered, which can be distinguished by e.getAction(). 
   3.boolean onDown(MotionEvent e) Explanation: Triggered when Touch Down 
   4.boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) Explanation: After touching a sliding distance, it is triggered when up. 
   5.void onLongPress(MotionEvent e) Explanation: Triggered when the touch does not move and has been touching down 
   6.boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) Explanation: Triggered when the sliding is touched. 
   7.void onShowPress(MotionEvent e) Explanation: Triggered when touch has not yet swiped (compared with onDown, onLongPress), onDown must be triggered immediately as long as it is touched down. And touchdown after a while without sliding trigger onShowPress and then onLongPress. Therefore, it has not been swiped after Touchdown, and the sequence of onDown->onShowPress->onLongPress is triggered. 
   8.boolean onSingleTapConfirmed(MotionEvent e) 
   9.boolean onSingleTapUp(MotionEvent e) Explanation: The above two functions are both triggered after touch down and no sliding (onScroll), no long pressing (onLongPress), and then touchup. 
   Click a very fast (no sliding) Touchup: onDown->onSingleTapUp->onSingleTapConfirmed 
   Click a slightly slower (no sliding) Touchup: onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed

 

 

GestureDetector detects various operation gestures of the current user,  and obtains the currently triggered operation gestures (Single Tap Up, Show Press, Long Press, Scroll, Down, Fling) through the GestureDetector.OnGestureListener callback.

 

Instructions:


01.private GestureDetector mGestureDetector; 
02.@Override
03.public void onCreate(Bundle savedInstanceState) { 
04.    super.onCreate(savedInstanceState); 
05.    mGestureDetector = new GestureDetector(this, new LearnGestureListener()); 
06.} 
07.@Override
08.public boolean onTouchEvent(MotionEvent event) { 
09.    if (mGestureDetector.onTouchEvent(event)) 
10.        return true; 
11.    else
12.        return false; 
13.} 
14.class LearnGestureListener extends GestureDetector.SimpleOnGestureListener{ 
15.    @Override
16.    public boolean onSingleTapUp(MotionEvent ev) { 
17.        Log.d("onSingleTapUp",ev.toString()); 
18.        return true; 
19.    } 
20.    @Override
21.    public void onShowPress(MotionEvent ev) { 
22.        Log.d("onShowPress",ev.toString()); 
23.    } 
24.    @Override
25.    public void onLongPress(MotionEvent ev) { 
26.        Log.d("onLongPress",ev.toString()); 
27.    } 
28.    @Override
29.    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
30.        Log.d("onScroll",e1.toString()); 
31.        return true; 
32.    } 
33.    @Override
34.    public boolean onDown(MotionEvent ev) { 
35.        Log.d("onDownd",ev.toString()); 
36.        return true; 
37. } 
38. @Override
39. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
40. Log.d("d",e1.toString()); 
41. Log.d(" e2",e2.toString()); 
42. return true; 
43. } 
44.}
Description:
Create a GestureDetector instance in the current class.
1.private GestureDetector mGestureDetector;
Create a Listener to monitor the current panel operation gestures in real time.
1.class LearnGestureListener extends GestureDetector.SimpleOnGestureListener
When initializing, associate the Listener instance with the current GestureDetector instance.
1.mGestureDetector = new GestureDetector(this, new LearnGestureListener()); Use the onTouchEvent method as the entrance detection, and listen for the operation gesture
by passing the MotionEvent parameter.
1.mGestureDetector.onTouchEvent(event)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326645354&siteId=291194637