java - How to clear drawing on a canvas in Android -
i have set canvas drawing. problem how clear drawing.
tried doing no avail.
public void clear() { circlepath.reset(); mpath.reset(); // calls ondraw() method //invalidate(); }
please take @ whole code here:
https://gist.github.com/akosijiji/a29cca90bead2e5e35ad
any appreciated.
on button click
b1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub dv.clear(); // call clear method using in custom view } });
define clear method in custom view
public void clear() { mbitmap = bitmap.createbitmap(width,height , bitmap.config.argb_8888); mcanvas = new canvas(mbitmap); mpath = new path(); mbitmappaint = new paint(paint.dither_flag); //added later.. mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(color.green); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(12); invalidate(); }
reset drawing tools , call invalidate refresh view.
complete working code
public class mainactivity extends activity implements colorpickerdialog.oncolorchangedlistener { drawingview dv ; relativelayout rl; private paint mpaint; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); dv = new drawingview(this); setcontentview(r.layout.activity_main); mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(color.green); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(12); rl = (relativelayout) findviewbyid(r.id.rl); rl.addview(dv); button b = (button) findviewbyid(r.id.button1); //b.settext(r.string.france); button b1 = (button) findviewbyid(r.id.button2); rl.setdrawingcacheenabled(true); b.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub // dv.clear(); new colorpickerdialog(mainactivity.this, mainactivity.this, mpaint.getcolor()).show(); } }); b1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub dv.clear(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); return true; } public class drawingview extends view { private int width; private int height; private bitmap mbitmap; private canvas mcanvas; private path mpath; private paint mbitmappaint; context context; private paint circlepaint; private path circlepath; public drawingview(context c) { super(c); context=c; mpath = new path(); mbitmappaint = new paint(paint.dither_flag); circlepaint = new paint(); circlepath = new path(); circlepaint.setantialias(true); circlepaint.setcolor(color.blue); circlepaint.setstyle(paint.style.stroke); circlepaint.setstrokejoin(paint.join.miter); circlepaint.setstrokewidth(4f); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); width =w; height =h; mbitmap = bitmap.createbitmap(w, h, bitmap.config.argb_8888); mcanvas = new canvas(mbitmap); } public void clear() { mbitmap = bitmap.createbitmap(width,height , bitmap.config.argb_8888); //log.d("bitmap","restoring..."); //mbitmap=bitmapfactory.decodebytearray(bytes, 0, bytes.length); mcanvas = new canvas(mbitmap); mpath = new path(); mbitmappaint = new paint(paint.dither_flag); //added later.. mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(color.green); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(12); invalidate(); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint); canvas.drawpath(mpath, mpaint); canvas.drawpath(circlepath, circlepaint); } 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; } private void touch_move(float x, float y) { float dx = math.abs(x - mx); float dy = math.abs(y - my); if (dx >= touch_tolerance || dy >= touch_tolerance) { mpath.quadto(mx, my, (x + mx)/2, (y + my)/2); mx = x; = y; circlepath.reset(); circlepath.addcircle(mx, my, 30, path.direction.cw); // invalidate(); } } private void touch_up() { mpath.lineto(mx, my); circlepath.reset(); // commit path our offscreen mcanvas.drawpath(mpath, mpaint); // kill don't double draw mpath.reset(); // mpaint.setxfermode(new porterduffxfermode(porterduff.mode.screen));//.mode.screen)); mpaint.setmaskfilter(null); mcanvas.drawcolor(color.transparent, porterduff.mode.screen); } @override public boolean ontouchevent(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; } } @override public void colorchanged(int color) { // todo auto-generated method stub mpaint.setcolor(color); } }
the above works not design coz everytime need clear creating objects paint object.
you can create singleton class , have tools reset , call invalidate
public class drawingmanager { private static drawingmanager minstance=null; public drawingtools mdrawingutilities=null; public int mthemeindex; public canvas mcanvas; public path mpath; public paint mbitmappaint; public bitmap mbitmap; public paint mpaint; private drawingmanager() { resetdrawingtools(); } public static drawingmanager getinstance() { if(minstance==null) { minstance=new drawingmanager(); } return minstance; } public void resetdrawingtools() { mbitmap = bitmap.createbitmap(screenwidth,screenheight , bitmap.config.argb_8888) mcanvas = new canvas(mbitmap); path = new path(); mbitmappaint = new paint(paint.dither_flag); //added later.. mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(color.green); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(12); } /** * clears drawing board. */ public static void cleardrawingboard() { minstance=null; } public void resetbitmapcanvasandpath() { // todo auto-generated method stub mbitmap = bitmap.createbitmap(screenwidth,screenheight , bitmap.config.argb_8888); //log.d("bitmap","restoring..."); //mbitmap=bitmapfactory.decodebytearray(bytes, 0, bytes.length); mcanvas = new canvas(.mbitmap); mpath = new path(); } }
in painting class
private drawingmanager mdrawingmanager=null; mdrawingmanager=drawingmanager.getinstance();// initialize drawing tools once
then on button click clear draw
mdrawingmanager.resetbitmapcanvasandpath(); invalidate();
Comments
Post a Comment