Android gesture listening (GestureDetector class)
Public class GestureTest extends Activity implements OnClickListener, OnGestureListener {// defines the GestureDetector detector of the gesture detector instance; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity10_layout); // create a gesture detector = new GestureDetector (this, this);} // send the touch event of the Activity to GestureDetector for processing @ Overridepublic boolean onTouchEvent (MotionEvent me) {return detector. onTouchEvent (me) ;}@ Overridepublic boolean onDown (MotionEvent arg0) {Toast. makeText (this, "pressing", Toast. LENGTH_LONG ). show (); return false ;}@ Overridepublic boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {Toast. makeText (this, "throwing", Toast. LENGTH_LONG ). show (); return false ;}@ Overridepublic void onLongPress (MotionEvent e) {Toast. makeText (this, "Long-pressing", Toast. LENGTH_LONG ). show () ;}@ Overridepublic boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {Toast. makeText (this, "sliding", Toast. LENGTH_LONG ). show (); return false ;}@ Overridepublic void onShowPress (MotionEvent e) {Toast. makeText (this, "holding down", Toast. LENGTH_LONG ). show () ;}@ Overridepublic boolean onSingleTapUp (MotionEvent e) {Toast. makeText (this, "being lifted", Toast. LENGTH_LONG ). show (); return false ;}@ Overridepublic void onClick (View arg0 ){}}
The onFling () parameter has the following meanings:
E1: A movement event of the starting point of a gesture
E2: movement event of the current gesture point
VelocityX: pixels that move in the x-axis direction per second
VelocityY: The pixel that moves in the Y axis every second.
The onScroll () parameter has the following meanings:
DistanceX: equals to the X value of e1 minus the X value of e2. The calculation result is a plus or minus sign.
DistanceY: equal to the Y value of e1 minus the Y value of e2. The calculation result is a plus or minus sign.