android - Multiple Colors drawn on canvas -


i trying draw multiple colors on canvas , initial color works when switched color line drawn previous color subsequent lines drawn correct color. ie once if switch green black next line draw green on black. appreciated.

public class drawingpanel extends view implements ontouchlistener {     private static final string tag = "drawview";     private static final float minp = 0.25f;     private static final float maxp = 0.75f;     private canvas  mcanvas;     private path    mpath;     private paint   mpaint;      private paint   bmpaint;     private paint   gmpaint;     private arraylist<path> paths = new arraylist<path>();     private map<path, integer> colorsmap = new hashmap<path,integer>();     private int selectedcolor;     sharedpreferences settings= getsharedpreferences("macaroni",0);     private string selcolr = settings.getstring("drawcolor", "");      public drawingpanel(context context) {         super(context);         setfocusable(true);         setfocusableintouchmode(true);          this.setontouchlistener(this);          mpaint = new paint();         mpaint.setantialias(true);         mpaint.setdither(true);         mpaint.setcolor(color.black);         mpaint.setstyle(paint.style.stroke);         mpaint.setstrokejoin(paint.join.round);         mpaint.setstrokecap(paint.cap.round);         mpaint.setstrokewidth(6);         mcanvas = new canvas();           mpath = new path();          paths.add(mpath);          colorsmap.put(mpath,getcolorsel());      }                        @override     protected void onsizechanged(int w, int h, int oldw, int oldh) {             super.onsizechanged(w, h, oldw, oldh);         }          @override         protected void ondraw(canvas canvas) {             (path p : paths){                  int grabcolorfromhash =colorsmap.get(p);                     mpaint.setcolor(grabcolorfromhash);                  canvas.drawpath(p, mpaint);         }         }      private float mx, my;         private static final float touch_tolerance = 4;         private void touch_start(float x, float y) {              mpath.reset();             mpath.moveto(x, y);             mx = x;             = y;             log.i("touch_start","touch_start");            }      private void touch_move(float x, float y) {             float dx = math.abs(x - mx);             float dy = math.abs(y - my);             log.i("touch_move","touch_move");              if (dx >= touch_tolerance || dy >= touch_tolerance) {                 mpath.quadto(mx, my, (x + mx)/2, (y + my)/2);                 mx = x;                 = y;             }         }          private void touch_up() {             mpath.lineto(mx, my);             mpath = new path();             paths.add(mpath);             colorsmap.put(mpath,getcolorsel());             mpaint.setcolor(getcolorsel());          }     public int getcolorsel()      {              selcolr = settings.getstring("drawcolor", "");           if (selcolr.equalsignorecase("black")){             mpaint.setcolor(color.black);             selectedcolor = mpaint.getcolor();            }             if (selcolr.equalsignorecase("red"))            {              mpaint.setcolor(color.red);             selectedcolor = mpaint.getcolor();            }             if (selcolr.equalsignorecase("green"))            {              mpaint.setcolor(color.green);             selectedcolor = mpaint.getcolor();            }          return selectedcolor;      }     @override     public boolean ontouch(view arg0, motionevent event) {           float x = event.getx();           float y = event.gety();            switch (event.getaction()) {               case motionevent.action_down:                   touch_start(x, y);                   invalidate();                   break;               case motionevent.action_move:                   touch_move(x, y);                   invalidate();                   break;               case motionevent.action_up:                   touch_up();                   invalidate();                   break;           }           return true;     } } 

in touch_start reset path don't add arraylist until touch_up don't see how it's getting drawn @ while in act of dragging finger around.

your ondraw drawing paths iterates through arraylist. in other words, add code ondraw draw mpath along saved in arraylist.

it's hard without being able run stuff in debugger think might solve problem.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -