Android to judge double-click event (refer to Android source, determine time interval and range)

Source: Internet
Author: User
Tags gety

For Android Double-click event Judgment, the official has given the solution, mainly using the following classes or interfaces: Gesturedetector,ongesturelistener,ondoubletaplistener, Gesturedetector.simpleongesturelistener

For their introduction and the use of a lot, it does not explain, you can refer to the following blog:

Http://blog.sina.com.cn/s/blog_77c6324101017hs8.html

Special instructions are ondoubletaplistener this interface,Gesturedetector has a function setondoubletaplistener to set the Ondoubletaplistener, Instead of through the way of constructors, but let the way through the constructor is not not possible, you can refer to the following blog:

Http://www.2cto.com/kf/201211/165457.html

Through the above study, I believe that we will be a few classes with the very skilled, but this is not the focus of our blog.

If you for some reason, or say, do not want to use the above method, do not want to use motionevent to judge the time of double-click, that also wood has Method! ~ This online also has a lot of blog to explain.

But their blog, no matter what the implementation, is only through the time to judge, and will not make a range of judgment, imagine, if you very quickly click on the top of the screen and the bottom two points, if you follow the network most of the time to judge the practice, it must be double-click time, but this is obviously unreasonable.

So how is the official source code judged? Let's take a look first, Bo main reference is ANDROID-17 version of the source code.

... case MotionEvent.ACTION_DOWN:if (Mdoubletaplistener! = null) {Boolean hadtapmessage = MHa                Ndler.hasmessages (TAP);                if (hadtapmessage) mhandler.removemessages (TAP);                        if (mcurrentdownevent! = null) && (mpreviousupevent! = null) && hadtapmessage &&                    Isconsidereddoubletap (mcurrentdownevent, mpreviousupevent, Ev)) {//This is a second tap                    Misdoubletapping = true; Give a callback with the first tap of the Double-tap handled |= Mdoubletaplistener.ondoubletap (mcurr                    Entdownevent); Give a callback with down event of the Double-tap handled |= mdoubletaplistener.ondoubletapevent (EV)                ; } else {//This is a first tap mhandler.sendemptymessagedelayed (tap, Double_tap_tim                Eout); }} MDOWNFOCUSX = Mlastfocusx = Focusx;            Mdownfocusy = Mlastfocusy = Focusy;            if (mcurrentdownevent! = null) {mcurrentdownevent.recycle ();            } mcurrentdownevent = Motionevent.obtain (EV);            Malwaysintapregion = true;            Malwaysinbiggertapregion = true;            Mstilldown = true;            Minlongpress = false; Mdeferconfirmsingletap = false;..
It is obvious that they are using the following method to determine whether a double-click event needs to be distributed:
Isconsidereddoubletap (mcurrentdownevent, mpreviousupevent, Ev)
The following code calls the listener interface to handle the double-click event, and gets the processing result, followed by the event distribution mechanism, which is no longer covered in this article.
Handled |= Mdoubletaplistener.ondoubletap (mcurrentdownevent);
So, how did those three parameters come about? Let's look at it one by one.

1.mCurrentDownEvent

<span style= "color: #333333;" >....case MotionEvent.ACTION_DOWN:if (Mdoubletaplistener! = null) {Boolean hadtapmessage =                Mhandler.hasmessages (TAP);                if (hadtapmessage) mhandler.removemessages (TAP);                        if (mcurrentdownevent! = null) && (mpreviousupevent! = null) && hadtapmessage &&                    Isconsidereddoubletap (mcurrentdownevent, mpreviousupevent, Ev)) {//This is a second tap                    Misdoubletapping = true; Give a callback with the first tap of the Double-tap handled |= Mdoubletaplistener.ondoubletap (mcurr                    Entdownevent); Give a callback with down event of the Double-tap handled |= mdoubletaplistener.ondoubletapevent (EV)                ; } else {//This is a first tap mhandler.sendemptymessagedelayed (tap, Double_tap_tim                Eout); }            } mdownfocusx = Mlastfocusx = Focusx;            Mdownfocusy = Mlastfocusy = Focusy; </span><span style= "color: #ff6666;"            >if (mcurrentdownevent! = null) {mcurrentdownevent.recycle (); } mcurrentdownevent = Motionevent.obtain (EV); </span><span style= "color: #333333;" >....</span>

The red font indicates that it was the last motionevent that triggered the down event.

2.mPriviousUpEvent

.... case MotionEvent.ACTION_UP:mStillDown = false; <span style= "color: #ff6666;"  >motionevent currentupevent = motionevent.obtain (EV);</span> if (misdoubletapping) {//            Finally, give the up event of the Double-tap handled |= mdoubletaplistener.ondoubletapevent (EV);                } else if (minlongpress) {mhandler.removemessages (TAP);            Minlongpress = false;                } else if (malwaysintapregion) {handled = Mlistener.onsingletapup (EV); if (Mdeferconfirmsingletap && mdoubletaplistener! = null) {MDOUBLETAPLISTENER.ONSINGLETAPCONFI                rmed (EV); }} else {//A fling must travel the minimum tap distance final Velocitytracker                Velocitytracker = Mvelocitytracker;                Final int pointerid = Ev.getpointerid (0); Velocitytracker.computecurrentvelocity (Mmaximumflingvelocity);                Final float velocityy = velocitytracker.getyvelocity (Pointerid);                Final float Velocityx = velocitytracker.getxvelocity (Pointerid); if ((Math.Abs (velocityy) > mminimumflingvelocity) | | (Math.Abs (Velocityx) > Mminimumflingvelocity))                {handled = mlistener.onfling (mcurrentdownevent, Ev, Velocityx, velocityy); }} <span style= "color: #ff6666;"            >if (mpreviousupevent! = null) {mpreviousupevent.recycle ();            }</span>//Hold the event we obtained above-listeners may have changed the original. <span style= "color: #ff6666;" >mpreviousupevent = Currentupevent;</span>

As you can see, the last time the up event was triggered is the motionevent.

3.ev

... public boolean ontouchevent (motionevent <span style= "color: #ff6666;" >ev</span>) {        if (minputeventconsistencyverifier! = null) {            Minputeventconsistencyverifier.ontouchevent (EV, 0);        }        Final int action = Ev.getaction ();
Is the Motionevent event that is currently occurring.

All of the above three parameters have been found, and the next step is to see what the Isconsidereddoubletap method does.

Private Boolean Isconsidereddoubletap (Motionevent firstdown, motionevent firstup,            motionevent seconddown) {        if (!malwaysinbiggertapregion) {            return false;        }        if (Seconddown.geteventtime ()-Firstup.geteventtime () > Double_tap_timeout) {            return false;        }        int deltax = (int) firstdown.getx ()-(int) seconddown.getx ();        int deltay = (int) firstdown.gety ()-(int) seconddown.gety ();        Return (DeltaX * deltax + deltay * DeltaY < Mdoubletapslopsquare);    }
The method is very simple, first judge the event, and then judge the scope. The following is a look at the relevant parameters of the acquisition, as follows:

Final Viewconfiguration Configuration = viewconfiguration.get (context);

private static final int double_tap_timeout = Viewconfiguration.getdoubletaptimeout ();

Doubletapslop = Configuration.getscaleddoubletapslop ();

Mdoubletapslopsquare = Doubletapslop * DOUBLETAPSLOP;

After the above observation, I believe you already know, Andorid himself is how to judge it? Accordingly, we can imitate to write a write.

Initialize the parameters First:

Viewconfiguration configuration = Viewconfiguration.get (this);

Doubletapslop = Configuration.getscaleddoubletapslop (); mdoubletapslopsquare = Doubletapslop * DOUBLETAPSLOP;

Then get three parameters:

@Overridepublic boolean OnTouch (View V, motionevent event) {switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:if (Isconsidereddoubletap (firstdown,firstup,event)) {Hideorshowtitlebar (menurl.getvisibility ()! = View.gone);} if (Firstdown! = null) {firstdown.recycle ();            } Firstdown = Motionevent.obtain (event); Hideorshowtitlebar (menurl.getvisibility ()! = View.gone); break;case MotionEvent.ACTION_UP:if (Firstdown! = null) {firstup.recycle ();            } Firstup = Motionevent.obtain (event); return false;}

The Final judgment:

/** * A method used to determine double-click time * @param firstdown * @param firstup * @param seconddown * @return */private boolean Isconsidereddoublet AP (motionevent firstdowns,motionevent firstups,motionevent seconddowns) {if (firstdowns = = NULL | | seconddowns = NULL) { return false;} System.out.println ("Seconddowns.geteventtime ():" +seconddowns.geteventtime ());//system.out.println ("        Firstups.geteventtime (): "+firstups.geteventtime ());        if (Seconddowns.geteventtime ()-Firstups.geteventtime () > Constans.double_tap_timeout) {return false;        } int deltax = (int) firstdowns.getx ()-(int) seconddowns.getx (); int deltay = (int) firstdowns.gety ()-(int) seconddowns.gety ();//System.out.println ("DeltaX:" +deltax);//S Ystem.out.println ("DeltaY:" +deltay);//System.out.println ("DeltaX * deltax + deltay * DeltaY:" +deltay);//Sy        Stem.out.println ("Mdoubletapslopsquare:" +mdoubletapslopsquare); Return (DeltaX * deltax + deltay * DeltaY < MdoubletapSlopsquare); }

OK, so it's done, isn't it better than before? ~!.~




Android to judge double-click event (refer to Android source, determine time interval and range)

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.