android - singletap touch detect in Ontouch method of the view -


i needed singletap touch detect in custom view's ontouch method. tried getting x , y values in both action-down , action-up , in action-up gave condition if values of x , y in actiondown , action-up equal take single tap.

my code follows

@override public boolean ontouchevent(motionevent ev) {    if (!msupportszoom && !msupportspan) return false;      mscaledetector.ontouchevent(ev);      final int action = ev.getaction();     switch (action & motionevent.action_mask) {     case motionevent.action_down: {         final float x = ev.getx();         final float y = ev.gety();          mlasttouchx = x;  //here x , y values in action down         mlasttouchy = y;         mactivepointerid = ev.getpointerid(0);          break;     }      case motionevent.action_move: {         final int pointerindex = ev.findpointerindex(mactivepointerid);         final float x = ev.getx(pointerindex);         final float y = ev.gety(pointerindex);          if (msupportspan && !mscaledetector.isinprogress()) {             final float dx = x - mlasttouchx;             final float dy = y - mlasttouchy;              mposx += dx;             mposy += dy;             //mfocusx = mposx;             //mfocusy = mposy;              invalidate();         }          mlasttouchx = x;         mlasttouchy = y;          break;     }      case motionevent.action_up: {          final float x = ev.getx();         final float y = ev.gety();          touchupx=x;   //here x , y values @ action         touchupy=y;           if(mlasttouchx == touchupx && mlasttouchy == touchupy){  //my condition if both x , y values same .              pinchzoompanactivity2.tapped1(this.getcontext(), 100); //my method if singletap detected          }         else{          }          mactivepointerid = invalid_pointer_id;          break;     }      case motionevent.action_cancel: {         mactivepointerid = invalid_pointer_id;         break;     }      case motionevent.action_pointer_up: {         final int pointerindex = (ev.getaction() & motionevent.action_pointer_index_mask)                  >> motionevent.action_pointer_index_shift;         final int pointerid = ev.getpointerid(pointerindex);         if (pointerid == mactivepointerid) {              final int newpointerindex = pointerindex == 0 ? 1 : 0;             mlasttouchx = ev.getx(newpointerindex);             mlasttouchy = ev.gety(newpointerindex);             mactivepointerid = ev.getpointerid(newpointerindex);         }         break;     }     }      return true; } 

but cant done. mean @ every action method called. when x , y values of both actionup , actiondown not same. , think need put range singletap touch our finger on screen. can suggest me ways?

i ran same problem , ended having implement debounce working. it's not ideal, it's pretty reliable until can find better.

view.onclicklistener more reliable me, unfortunately need motionevent ontouchlistener.

edit: removed excess code cause fail here

class customview extends view {      private static long mdebounce = 0;      static ontouchlistener listenermotionevent = new ontouchlistener() {         @override         public boolean ontouch(view view, motionevent motionevent) {             if ( math.abs(mdebounce - motionevent.geteventtime()) < 250) {                 //ignore if it's been less 250ms since                 //the item last clicked                 return true;             }              int intcurrenty = math.round(motionevent.gety());             int intcurrentx = math.round(motionevent.getx());             int intstarty = motionevent.gethistorysize() > 0 ? math.round(motionevent.gethistoricaly(0)) : intcurrenty;             int intstartx = motionevent.gethistorysize() > 0 ? math.round(motionevent.gethistoricalx(0)) : intcurrentx;              if ( (motionevent.getaction() == motionevent.action_up) && (math.abs(intcurrentx - intstartx) < 3) && (math.abs(intcurrenty - intstarty) < 3) ) {                 if ( mdebounce > motionevent.getdowntime() ) {                     //still got occasional duplicates without                     return true;                 }                  //handle click                  mdebounce = motionevent.geteventtime();                 return true;             }             return false;         }     }; } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -