How to handle various touch events in Android talking about _android

Source: Internet
Author: User

There are two classes in Android
Android.view.GestureDetector
Android.view.GestureDetector.SimpleOnGestureListener
(in addition, Android.widget.Gallery seems to be more ongesturelistener of the Ox x)
1)
Create a new class to inherit Simpleongesturelistener,hahagesturedetectorlistener
The following event events can be implemented.
Boolean Ondoubletap (Motionevent e)
Explanation: Double click on the second touch down when the trigger
Boolean ondoubletapevent (Motionevent e)
Explanation: Double click down and up will trigger, and e.getaction () can be used to distinguish between the two.
Boolean Ondown (Motionevent e)
Explanation: Trigger when touching down
Boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy)
Explanation: Touch it by sliding a little distance after the up is triggered.
void Onlongpress (Motionevent e)
Explanation: The touch is not moving, always touching down.
Boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey)
Explanation: Touch the trigger when sliding.
void Onshowpress (Motionevent e)
Explanation: Touch has not yet slipped when triggered
(Compared with ondown,onlongpress
Ondown as soon as touch down must trigger immediately.
and touchdown after a while not sliding first trigger onshowpress again is onlongpress.
So touchdown does not slide, ondown->onshowpress->onlongpress this sequence trigger.

Boolean onsingletapconfirmed (Motionevent e)
Boolean onsingletapup (Motionevent e)
Explanation: The above two functions are not sliding (onscroll) after touching down, without a long press (onlongpress), and then triggering when touchup.
Click on the very fast (do not slide) Touchup:
Ondown->onsingletapup->onsingletapconfirmed
Click a little slower (do not slide) Touchup:
Ondown->onshowpress->onsingletapup->onsingletapconfirmed
2 A new Gesturedetector object in view.
In the constructor function
Gesturedetector = new Gesturedetector (new Hahagesturedetectorlistener ());
Then you can write your own code in the ontouchevent of the view by using the following in the event you just made in 1.

Copy Code code 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) {
According to the direction of calculation angle
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);

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.