android - Gesture Detector with onDraw() called multiple times -


i using gesturedetector identify touch.i have 2 relative layoutsof same layout size.i trying draw image on 1 layout , move image b(created using ondraw) on layout.while move image b using gesturedetector identify touch.when move image b, image a(view) ondraw() method called multiple times. please me avoid calling ondraw() multiple times.here code

public class touchexampleactivity extends activity {  relativelayout r1; relativelayout r2; context context;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);      context = touchexampleactivity.this;     r1 = (relativelayout) findviewbyid(r.id.r1);     r2 = (relativelayout) findviewbyid(r.id.r2);      touchexampleview view = new touchexampleview(this);      drawcircle drawcircle = new drawcircle(context);      r1.addview(view);     r2.addview(drawcircle);  }  class drawcircle extends view {      paint p;      public drawcircle(context context) {         super(context);         // todo auto-generated constructor stub         p = new paint();     }      @override     protected void ondraw(canvas canvas) {         // todo auto-generated method stub         super.ondraw(canvas);          log.v("touch", "called draw circle");         p.setcolor(color.blue);         canvas.drawcircle(60, 50, 50, p);      } }  } 

touchexampleview.java

public class touchexampleview extends view { private drawable micon; private float mposx; private float mposy; static boolean fstartdrag = false; static boolean fdrag = false; private float ypos = 40; private float xpos = 30; public static float sx, sy;  private versionedgesturedetector mdetector; private float mscalefactor = 1.f;  public touchexampleview(context context) {     this(context, null, 0); }  public touchexampleview(context context, attributeset attrs) {     this(context, attrs, 0); }  public touchexampleview(context context, attributeset attrs, int defstyle) {     super(context, attrs, defstyle);     micon = context.getresources().getdrawable(r.drawable.icon);     micon.setbounds(0, 0, micon.getintrinsicwidth(), micon.getintrinsicheight());      mdetector = versionedgesturedetector.newinstance(context, new gesturecallback()); }  @override public boolean ontouchevent(motionevent ev) {     mdetector.ontouchevent(ev);     return true; }  @override public void ondraw(canvas canvas) {     super.ondraw(canvas);      paint paint = new paint();     paint.setcolor(color.red);     paint.setstrokewidth(5);      canvas.save();     canvas.translate(mposx, 0);     canvas.scale(mscalefactor, mscalefactor);      canvas.drawrect(0, 0, 30, 30, paint);     canvas.restore(); }  private class gesturecallback implements versionedgesturedetector.ongesturelistener {     public void ondrag(float dx, float dy) {            if ((dx == -1) && (dy == -1)) {              fstartdrag = true;             fdrag = false;         }          else if (fstartdrag) {              if (dy >= mposy && dy <= ypos + mposy && dx >= mposx && dx <= mposx + xpos) {                  fdrag = true;                 fstartdrag = false;             }         } else if (fdrag) {             system.out.println("fdrag");             if (mposx < 3)                 mposx = 3;             else if (mposx > 400)                 mposx = 400;             else                  mposx = dx;              // mposy = dy;              postinvalidate();         }     }      public void onscale(float scalefactor) {         mscalefactor *= scalefactor;           mscalefactor = math.max(0.1f, math.min(mscalefactor, 5.0f));           postinvalidate();     } }  } 

my sample xml

<relativelayout     android1:id="@+id/r1"     android1:layout_width="620dip"     android1:layout_height="320dip"     android1:layout_alignparentbottom="true"     android1:layout_alignparentleft="true"     android1:layout_marginbottom="94dp" > </relativelayout>  <relativelayout     android1:id="@+id/r2"     android1:layout_width="620dip"     android1:layout_height="320dip"     android1:layout_alignparentbottom="true"     android1:layout_alignparentleft="true"     android1:layout_marginbottom="94dp" > </relativelayout> 

if use 2 relative layouts of different size , position works fine, ondraw() not called multiple times

after trying found answer question.setdrawingcacheenabled(true) prevents calling ondraw() multiple times.its working me.i hope going right.

   view circleview;    r2 = (relativelayout) findviewbyid(r.id.r2);    circleview= r2;             circleview.setdrawingcacheenabled(true); 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -