Click and double tap in Android based on touch events

Source: Internet
Author: User
Tags gety

private static final int max_interval_for_click = 250;    private static final int max_distance_for_click = 100;     private static final int max_double_click_interval = 500;    int mdownx = 0;    int Mdow NY = 0;    int mtempx = 0;    int mtempy = 0;    Boolean miswaitupevent = false;  &nbsp ; Boolean Miswaitdoubleclick = false;    Runnable mtimerforupevent = new Runnable () {        p  ublic void Run () {            if (miswaitupevent) {                LOG.D (log_tag,                        "The MTim Erforupevent have executed, so set the Miswaitupevent as false ");                MI Swaitupevent = false;           } else {              &N Bsp LOG.D (LOG_TAG,&NBSp                       "The mtimerforupevent has executed, miswaitup Event is false,so does nothing ");           }       }   };&NB Sp   @Override     public boolean ontouchevent (Motionevent event) {        if (!miswaitupev ENT && event.getaction ()! = Motionevent.action_down) {            return Super.ontou Chevent (event);       }        switch (event.getaction ()) {      & nbsp Case motionevent.action_down:            MDOWNX = (int) event.getx ();      &N Bsp     MDOWNY = (int) event.gety ();            miswaitupevent = true;    &N Bsp       postdelayed (mtimerforupevent, Max_interval_for_click);            Break;        case motionevent.action_move:            MTEMPX = (int) event.g EtX ();            mtempy = (int) event.gety ();            if (  Math.Abs (MTEMPX-MDOWNX) > max_distance_for_click                    || Math.Abs (Mtempy-mdowny) > Max_distance_for_click) {                Miswaitupe Vent = false;                removecallbacks (mtimerforupevent);    &NBSP ;           LOG.D (Log_tag, "The move distance too far:cancel the click");      &NBSP ;    }            break;        Case Motionevent.action_up:&nbs P           MTEMPX = (int) event.getx ();            mtempy = (int) EV Ent.gety (); &nBsp           if (Math.Abs (MTEMPX-MDOWNX) > max_distance_for_click        &N Bsp           | | Math.Abs (Mtempy-mdowny) > Max_distance_for_click) {                Miswaitupe Vent = false;                removecallbacks (mtimerforupevent);    &NBSP ;           LOG.D (log_tag,                    &NB Sp   "The touch down and up distance too far:cancel the click");                break;           } else {                Miswaitupe Vent = false;                removecallbacks (mtimerforupevent);    &NBSP ;           Onsingleclick ();          &NBSP      return Super.ontouchevent (event);           }        case motionevent.action_cancel:            miswaitupevent = false;        &N Bsp   Removecallbacks (mtimerforupevent);            LOG.D (Log_tag, "the Touch Cancel state : Cancel the Click ");            break;        default:    &NBS P       LOG.D (Log_tag, "irrelevant motionevent State:" + event.getaction ());       }&NBS P       return super.ontouchevent (event);   }    Runnable Mtimerforsecondclick = new Run Nable () {        @Override         public void run () {        & nbsp    if (Miswaitdoubleclick) {                LOG.D (log_tag,      &nbsP                 "The Mtimerforsecondclick has Executed,so as a Singleclick"); &nbsp ;               Miswaitdoubleclick = false;            &NBS P  /At this can do something for singleclick!!            } else {                LOG.D (LOG_TAG,&N Bsp                       "The Mtimerforsecondclick has executed, the D Oubleclick have executed, so do thing ");           }       }  &nbs P };    public void Onsingleclick () {        if (Miswaitdoubleclick) {      &NB Sp     OnDoubleClick ();            Miswaitdoubleclick = false;      &N Bsp     Removecallbacks (Mtimerforsecondclick);    &NBSp   Else {            Miswaitdoubleclick = true;          &NBS P Postdelayed (Mtimerforsecondclick, Max_double_click_interval);       }   }    public void OnDoubleClick () {        LOG.D (Log_tag, "We can do sth for double click here");  &nbsp ; }

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.