How to handle various touch events in android

Source: Internet
Author: User
Tags gety

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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.