java - Android conflict between, OnTouchListener SimpleOnGestureListener and setOnClickListener -


i trying create animation:

use case : 1. user swipes/ drags item right ( horizontally ), item gets added basket. if again swipes adds 1 more of same item basket. 2. user swipes/ drags item left ( horizontally ), item gets removes basket, if added before, if there no item in basket leave is.

drag effect : when user drags, item gets moved along finger. leave it, items gets position.

zoom effect : when user clicks on item, item gets zoomed, letting user know item has been added basket. ( replicated method of right dragged ).

i tried use onswipetouchlistener class specially purpose :

import android.content.context; import android.view.gesturedetector; import android.view.gesturedetector.simpleongesturelistener; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener;  import pointf;  public class onswipetouchlistener implements ontouchlistener {      private final gesturedetector gesturedetector;     pointf downpt = null;     pointf startpt = null;     float startx;     float starty;      public onswipetouchlistener(context context , float x, float y) {         gesturedetector = new gesturedetector(context, new gesturelistener());         downpt = new pointf();         startpt = new pointf();         this.startx = x;         this.starty = y;     }      public void onswipeleft() {     }      public void onswiperight() {     }      public boolean ontouch(view v, motionevent event) {          int eid = event.getaction();          switch (eid) {             case motionevent.action_move:                  pointf mv = new pointf(event.getx() - downpt.x, event.gety() - downpt.y);                  v.setx((int) (startpt.x + mv.x));                 v.sety(this.starty);                 startpt = new pointf(v.getx(), v.gety());                 break;             case motionevent.action_down:                 downpt.x = event.getx();                 downpt.y = event.gety();                 startpt = new pointf(v.getx(), v.gety());                  break;             case motionevent.action_up:                 // move image original position.                 v.setx(this.startx);                 v.sety(this.starty);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              break;             default:                 v.setx(this.startx);                 v.sety(this.starty);                  break;         }          return gesturedetector.ontouchevent(event);     }      private final class gesturelistener extends simpleongesturelistener {          private static final int swipe_distance_threshold = 100;         private static final int swipe_velocity_threshold = 100;          @override         public boolean ondown(motionevent e) {             return true;         }          @override         public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) {             float distancex = e2.getx() - e1.getx();             float distancey = e2.gety() - e1.gety();             if (math.abs(distancex) > math.abs(distancey) && math.abs(distancex) > swipe_distance_threshold && math.abs(velocityx) > swipe_velocity_threshold) {                 if (distancex > 0)                     onswiperight();                 else                     onswipeleft();                 return true;             }             return false;         }     } } 

when method ontouch below, onswiperight() , onswipeleft() gets triggered when have implemented ontouch above in code, these 2 methods does not triggered.

public boolean ontouch(view v, motionevent event) {   return gesturedetector.ontouchevent(event); } 

regarding zoom effect on click event.

in fragment zooming image this.

offerimg1.setonclicklistener(new view.onclicklistener() {

        @override         public void onclick(view v) {             // todo auto-generated method stub             zoomanimation(v, offerimg1);         }     }); 

and zoomanimation method :

private void zoomanimation(view view,imageview image) {         animation animation = animationutils.loadanimation(getactivity().getapplicationcontext(), r.anim.zoom);         image.startanimation(animation);     } 

my zoom working unless have not implemented in fragmented :

 offerimg1.setontouchlistener(new onswipetouchlistener(getactivity().getapplicationcontext(),offerimg1.getx(),offerimg1.gety()) {             @override             public void onswipeleft() {                 log.d("ontouch "," swipe left");             }              @override             public void onswiperight() {                 log.d("ontouch "," swipe right");             }          }); 

i not sure collision between these events. need achieve above use case, every events should work on each image. images in scroll view in fragment.

if want can share more code here. please let me know if not clarify problem.

i appreciate help.

regards, shashank pratap

finally have solved problem,

found problem above code :

onswipeleft() , onswiperight().. since image moving along finger, reason distancex turing out zero, less static swipe_distance_threshold. , in code saying if distancex negative swipeleft() else swiperight()

solution : needed someway trap way user moving his/her finger, therefore decided within motionevent.action_move: event, find if mv.x negative or mv.x positive. once found it, easy make decision, , on basis can decide method run within gesturelistener.

here onswipetouchlistener, still not sure if best solution case, found working on avd on android device.

i still open find nice , clear method ( best practice ) of doing :

import android.content.context; import android.content.res.resources; import android.util.log; import android.view.gesturedetector; import android.view.gesturedetector.simpleongesturelistener; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener; import android.view.viewconfiguration; import android.view.animation.animation; import android.view.animation.animationutils; import android.widget.imageview; import android.widget.toast;  import r;  import startingpoints; import pointf;  public class onswipetouchlistener implements ontouchlistener {      private static final string app = onswipetouchlistener.class.getname() ;     private final gesturedetector gesturedetector;     pointf downpt = null;     pointf startpt = null;      context _context;     static  boolean isleftmoved = false;     static  boolean isrightmoved = false;      /**      * max allowed duration "click", in milliseconds.      */     private static final int max_click_duration = 1000;      /**      * max allowed distance move during "click", in dp.      */     private static final int max_click_distance = 15;     private static final float pixels_per_second = 5;      private long pressstarttime;     private float pressedx;     private float pressedy;     private boolean stayedwithinclickdistance;     resources resources;     private float startx = 0f;     private float starty = 0f;     private boolean isnewimage = true;         public onswipetouchlistener(context context, resources resources) {         this._context = context;         gesturedetector = new gesturedetector(context, new gesturelistener());         downpt = new pointf();         startpt = new pointf();         this.resources = resources;      }      public void onswipeleft() {     }      public void onswiperight() {     }      public boolean ontouch(view v, motionevent e) {         if(isnewimage){             isnewimage = false;             startx = v.getx();             starty = v.gety();         }         switch (e.getaction()) {             case motionevent.action_down:                 //animation code                 downpt.x = e.getx();                 downpt.y = e.gety();                 startpt = new pointf(v.getx(), v.gety());                   //calculation code                 pressstarttime = system.currenttimemillis();                 pressedx = e.getx();                 pressedy = e.gety();                 stayedwithinclickdistance = true;                 break;              case motionevent.action_move:                 // animation code                 pointf mv = new pointf(e.getx() - downpt.x, e.gety() - downpt.y);                 v.setx((int) (startpt.x + mv.x));                 v.sety(starty);                  startpt = new pointf(v.getx(), v.gety());                 if(mv.x < 0 ){                     isleftmoved = true;                 }                 if(mv.x > 0 ){                     isrightmoved = true;                 }                  //calculation code                 if (stayedwithinclickdistance && distance(pressedx, pressedy, e.getx(), e.gety()) > max_click_distance) {                     stayedwithinclickdistance = false;                 }                  log.d("moved ","item moved");                 break;             case motionevent.action_up:                 if(!stayedwithinclickdistance){                     v.setx(startx);                     v.sety(starty);                     isnewimage = true;                 }                   long pressduration = system.currenttimemillis() - pressstarttime;                 if (pressduration < max_click_duration && stayedwithinclickdistance) {                     // click event has occurred                     log.d("stayed"," click event");                     zoomanimation(v);                     isnewimage = true;                 }                   break;             default:                 // move image original position, default                 log.d("default", "this default ");                 v.setx(startx);                 v.sety(starty);                 isnewimage = true;                 break;          }          return gesturedetector.ontouchevent(e);     }       private  float distance(float x1, float y1, float x2, float y2) {         float dx = x1 - x2;         float dy = y1 - y2;         float distanceinpx = (float) math.sqrt(dx * dx + dy * dy);         return pxtodp(distanceinpx);     }      private  float pxtodp(float px) {         return px / resources.getdisplaymetrics().density;     }      private void zoomanimation(view view) {         animation animation = animationutils.loadanimation(_context, r.anim.zoom);         view.startanimation(animation);     }          private final class gesturelistener extends simpleongesturelistener {          private static final int swipe_distance_threshold = 20;         private static final int swipe_velocity_threshold = 50;            @override         public boolean ondown(motionevent e) {             return true;         }          @override         public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) {              float maxflingvelocity    = viewconfiguration.get(_context).getscaledmaximumflingvelocity();             float velocitypercentx    = velocityx / maxflingvelocity;          // percent value in range of (0, 1]             float normalizedvelocityx = velocitypercentx * pixels_per_second;  // pixels_per_second device-independent measurement              float distancex = e2.getx() - e1.getx();             float distancey = e2.gety() - e1.gety();              if (isleftmoved || isrightmoved) {                 if(isrightmoved) {                     isleftmoved = false;                     isrightmoved = false;                     onswiperight();                 }                 else {                     isleftmoved = false;                     isrightmoved = false;                     onswipeleft();                 }             }              return false;         }          @override         public boolean onsingletapconfirmed(motionevent e) {             return true;         }     } } 

best regards, shashank pratap


Comments