Android has two classes
Android. view. GestureDetector
Android. view. GestureDetector. SimpleOnGestureListener
(In addition, android. widget. Gallery seems to be a better OnGestureListener)
1)
Create a class that inherits the SimpleOnGestureListener and HahaGestureDetectorListener
You can implement the following event Events.
Boolean onDoubleTap (MotionEvent e)
Explanation: triggered when the second Touch is down under double-click
Boolean onDoubleTapEvent (MotionEvent e)
Explanation: Double-click triggers both Touch down and up, which can be distinguished by e. getAction.
Boolean onDown (MotionEvent e)
Explanation: triggered when Touch is down
Boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
Explanation: Touch is triggered when it is up after sliding a little distance.
Void onLongPress (MotionEvent e)
Explanation: triggered when Touch is down without moving
Boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
Explanation: It is triggered when sliding is reached.
Void onShowPress (MotionEvent e)
Explanation: triggered when Touch is not sliding
(Compared with onDown and onLongPress
OnDown must be triggered immediately as long as Touch down.
After Touchdown, onShowPress is triggered before onLongPress after a while without sliding.
Therefore, it does not slide after Touchdown. onDown-> onShowPress-> onLongPress is triggered in this order.
)
Boolean onSingleTapConfirmed (MotionEvent e)
Boolean onSingleTapUp (MotionEvent e)
Explanation: the above two functions are triggered when the touch is down without moving (onScroll) or long-pressed (onLongPress.
Click very fast (do not slide) Touchup:
OnDown-> onSingleTapUp-> onSingleTapConfirmed
Click a slightly slower (do not slide) Touchup:
OnDown-> onShowPress-> onSingleTapUp-> onSingleTapConfirmed
2) create a GestureDetector object in view.
Constructor
GestureDetector = new GestureDetector (new HahaGestureDetectorListener ());
Use the following code in onTouchEvent of View to write your own code in the event 1 just now.
Copy codeThe Code is as follows: @ Override
Public boolean onTouchEvent (MotionEvent event ){
GestureDetector. onTouchEvent (event );
}
MTouchListener = new OnTouchListener (){
@ Override
Public boolean onTouch (View v, MotionEvent event ){
// TODO Auto-generated method stub
Float x = event. getXPrecision () * event. getX () + event. getX ();
Float y = event. getYPrecision () * event. getY () + event. getY ();
Switch (event. getAction ()){
Case MotionEvent. ACTION_DOWN:
Break;
Case MotionEvent. ACTION_MOVE:
MTouchTimes ++;
If (mTouchTimes> TOUCH_TIMES ){
// Calculate the Angle Based on the Direction
If (mCurrentOrientation = DeviceOrientation. Landscape ){
MAngle = Math. toDegrees (Math. atan2 (y-480/2, x) + 90;
} Else {
MAngle =-Math. toDegrees (Math. atan2 (y-480/2, 320-x) + 90;
}
Log. w ("angle", "mangle:" + mAngle );
}
Break;
Case MotionEvent. ACTION_UP:
If (mTouchTimes> TOUCH_TIMES ){
} Else {
}
MTouchTimes = 0;
Break;
Default:
Break;
}
Return true;
}
};
MView. setOnTouchListener (mTouchListener );