erase functionality in android finger paint -
i doing finger paint on image contains background.after drawing lines or arcs on image need erase functionality erase paint using finger touch.
i googled did not find proper solution it.i developing minimum android version 2.2
here code did far.
public class drawpanel extends view { private paint p; private arraylist<point> points; private arraylist<arraylist<point>> strokes; private arraylist<paint> mvisiblepaints = new arraylist<paint>(); int currentcolor; boolean isdraw = true; public drawpanel(context context) { super(context); setfocusable(true); setfocusableintouchmode(true); points = new arraylist<point>(); strokes = new arraylist<arraylist<point>>(); currentcolor = color.red; } @override public void ondraw(canvas c) { super.ondraw(c); int = 0; (arraylist<point> obj : strokes) { paint paint = mvisiblepaints.get(i); drawstroke( obj, c, paint); i++; } if (p != null) drawstroke(points, c, p); } @override public boolean ontouchevent(motionevent event) { if (event.getactionmasked() == motionevent.action_down) { p = createpaint(currentcolor, 10); invalidate(); } if (event.getactionmasked() == motionevent.action_move) { if (isdraw) { points.add(new point((int) event.getx(), (int) event.gety())); } else erasescreen(new point((int) event.getx(), (int) event.gety())); invalidate(); } if (event.getactionmasked() == motionevent.action_up) { this.strokes.add(points); points = new arraylist<point>(); mvisiblepaints.add(p); invalidate(); } return true; } private void drawstroke(arraylist<point> stroke, canvas c, paint paint) { if (stroke.size() > 0) { point p0 = (point) stroke.get(0); (int = 1; < stroke.size(); i++) { point p1 = (point) stroke.get(i); c.drawline(p0.x, p0.y, p1.x, p1.y, paint); p0 = p1; } } } private paint createpaint(int color, float width) { paint temp = new paint(); temp.setstyle(paint.style.stroke); temp.setdither(true); temp.setantialias(true); temp.setcolor(color); temp.setstrokewidth(width); temp.setstrokecap(cap.round); temp.setstrokejoin(paint.join.round); return temp; } /** * sets current drawing color. * * @param color */ public void setcolor(int color) { currentcolor = color; } public void setisdraw(boolean isdraw) { this.isdraw = isdraw; } public boolean getisdraw() { return isdraw; } public void erasescreen(point p) { for(int i=0;i<strokes.size();i++) { arraylist<point> listpoints=strokes.get(i); //arraylist<point> drawpoints=new arraylist<point>(); if(listpoints.size()>0) { for(int j=0;j<listpoints.size();j++) { point pp=listpoints.get(j); float dx = math.abs(p.x - pp.x); float dy = math.abs(p.y - pp.y); if(dx<=5 && dy<=5) { listpoints.remove(j); } } strokes.add(i, listpoints); } } } }
please suggest solution this, achieve.
as op asking alternative
activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/ll1" android:gravity="bottom" android:orientation="vertical" android:weightsum="1.0" > <linearlayout android:id="@+id/view_drawing_pad" android:layout_width="fill_parent" android:layout_height="fill_parent" > </linearlayout> </linearlayout> </relativelayout>
mainactivity.java
public class fingerpaintactivity extends activity implements colorpickerdialog.oncolorchangedlistener { myview mv; alertdialog dialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mv= new myview(this); mv.setdrawingcacheenabled(true); mv.setbackgroundresource(r.drawable.afor);//set background here setcontentview(mv); mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(0xffff0000); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(20); memboss = new embossmaskfilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f); mblur = new blurmaskfilter(8, blurmaskfilter.blur.normal); } private paint mpaint; private maskfilter memboss; private maskfilter mblur; public void colorchanged(int color) { mpaint.setcolor(color); } public class myview extends view { private static final float minp = 0.25f; private static final float maxp = 0.75f; private bitmap mbitmap; private canvas mcanvas; private path mpath; private paint mbitmappaint; context context; public myview(context c) { super(c); context=c; mpath = new path(); mbitmappaint = new paint(paint.dither_flag); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); mbitmap = bitmap.createbitmap(w, h, bitmap.config.argb_8888); mcanvas = new canvas(mbitmap); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint); canvas.drawpath(mpath, mpaint); } private float mx, my; private static final float touch_tolerance = 4; private void touch_start(float x, float y) { //showdialog(); 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; } } private void touch_up() { mpath.lineto(mx, my); // commit path our offscreen mcanvas.drawpath(mpath, mpaint); // kill don't double draw mpath.reset(); mpaint.setxfermode(new porterduffxfermode(porterduff.mode.screen)); //mpaint.setmaskfilter(null); } @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; } } private static final int color_menu_id = menu.first; private static final int emboss_menu_id = menu.first + 1; private static final int blur_menu_id = menu.first + 2; private static final int erase_menu_id = menu.first + 3; private static final int srcatop_menu_id = menu.first + 4; private static final int save = menu.first + 5; @override public boolean oncreateoptionsmenu(menu menu) { super.oncreateoptionsmenu(menu); menu.add(0, color_menu_id, 0, "color").setshortcut('3', 'c'); menu.add(0, emboss_menu_id, 0, "emboss").setshortcut('4', 's'); menu.add(0, blur_menu_id, 0, "blur").setshortcut('5', 'z'); menu.add(0, erase_menu_id, 0, "erase").setshortcut('5', 'z'); menu.add(0, srcatop_menu_id, 0, "srcatop").setshortcut('5', 'z'); menu.add(0, save, 0, "save").setshortcut('5', 'z'); return true; } @override public boolean onprepareoptionsmenu(menu menu) { super.onprepareoptionsmenu(menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { mpaint.setxfermode(null); mpaint.setalpha(0xff); switch (item.getitemid()) { case color_menu_id: new colorpickerdialog(this, this, mpaint.getcolor()).show(); return true; case emboss_menu_id: if (mpaint.getmaskfilter() != memboss) { mpaint.setmaskfilter(memboss); } else { mpaint.setmaskfilter(null); } return true; case blur_menu_id: if (mpaint.getmaskfilter() != mblur) { mpaint.setmaskfilter(mblur); } else { mpaint.setmaskfilter(null); } return true; case erase_menu_id: mpaint.setxfermode(new porterduffxfermode( porterduff.mode.clear)); return true; case srcatop_menu_id: mpaint.setxfermode(new porterduffxfermode( porterduff.mode.src_atop)); mpaint.setalpha(0x80); return true; case save: alertdialog.builder editalert = new alertdialog.builder(fingerpaintactivity.this); editalert.settitle("please enter name want save"); final edittext input = new edittext(fingerpaintactivity.this); linearlayout.layoutparams lp = new linearlayout.layoutparams( linearlayout.layoutparams.fill_parent, linearlayout.layoutparams.fill_parent); input.setlayoutparams(lp); editalert.setview(input); editalert.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { string name= input.gettext().tostring(); bitmap bitmap = mv.getdrawingcache(); string path = environment.getexternalstoragedirectory().getabsolutepath(); file file = new file("/sdcard/"+name+".png"); try { if(!file.exists()) { file.createnewfile(); } fileoutputstream ostream = new fileoutputstream(file); bitmap.compress(compressformat.png, 10, ostream); ostream.close(); mv.invalidate(); } catch (exception e) { e.printstacktrace(); }finally { mv.setdrawingcacheenabled(false); } } }); editalert.show(); return true; } return super.onoptionsitemselected(item); } }
colorpicker
public class colorpickerdialog extends dialog { public interface oncolorchangedlistener { void colorchanged(int color); } private oncolorchangedlistener mlistener; private int minitialcolor; private static class colorpickerview extends view { private paint mpaint; private paint mcenterpaint; private final int[] mcolors; private oncolorchangedlistener mlistener; colorpickerview(context c, oncolorchangedlistener l, int color) { super(c); mlistener = l; mcolors = new int[] { 0xffff0000, 0xffff00ff, 0xff0000ff, 0xff00ffff, 0xff00ff00, 0xffffff00, 0xffff0000 }; shader s = new sweepgradient(0, 0, mcolors, null); mpaint = new paint(paint.anti_alias_flag); mpaint.setshader(s); mpaint.setstyle(paint.style.stroke); mpaint.setstrokewidth(32); mcenterpaint = new paint(paint.anti_alias_flag); mcenterpaint.setcolor(color); mcenterpaint.setstrokewidth(5); } private boolean mtrackingcenter; private boolean mhighlightcenter; @override protected void ondraw(canvas canvas) { float r = center_x - mpaint.getstrokewidth()*0.5f; canvas.translate(center_x, center_x); canvas.drawoval(new rectf(-r, -r, r, r), mpaint); canvas.drawcircle(0, 0, center_radius, mcenterpaint); if (mtrackingcenter) { int c = mcenterpaint.getcolor(); mcenterpaint.setstyle(paint.style.stroke); if (mhighlightcenter) { mcenterpaint.setalpha(0xff); } else { mcenterpaint.setalpha(0x80); } canvas.drawcircle(0, 0, center_radius + mcenterpaint.getstrokewidth(), mcenterpaint); mcenterpaint.setstyle(paint.style.fill); mcenterpaint.setcolor(c); } } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { setmeasureddimension(center_x*2, center_y*2); } private static final int center_x = 100; private static final int center_y = 100; private static final int center_radius = 32; private int floattobyte(float x) { int n = java.lang.math.round(x); return n; } private int pintobyte(int n) { if (n < 0) { n = 0; } else if (n > 255) { n = 255; } return n; } private int ave(int s, int d, float p) { return s + java.lang.math.round(p * (d - s)); } private int interpcolor(int colors[], float unit) { if (unit <= 0) { return colors[0]; } if (unit >= 1) { return colors[colors.length - 1]; } float p = unit * (colors.length - 1); int = (int)p; p -= i; // p fractional part [0...1) , index int c0 = colors[i]; int c1 = colors[i+1]; int = ave(color.alpha(c0), color.alpha(c1), p); int r = ave(color.red(c0), color.red(c1), p); int g = ave(color.green(c0), color.green(c1), p); int b = ave(color.blue(c0), color.blue(c1), p); return color.argb(a, r, g, b); } private int rotatecolor(int color, float rad) { float deg = rad * 180 / 3.1415927f; int r = color.red(color); int g = color.green(color); int b = color.blue(color); colormatrix cm = new colormatrix(); colormatrix tmp = new colormatrix(); cm.setrgb2yuv(); tmp.setrotate(0, deg); cm.postconcat(tmp); tmp.setyuv2rgb(); cm.postconcat(tmp); final float[] = cm.getarray(); int ir = floattobyte(a[0] * r + a[1] * g + a[2] * b); int ig = floattobyte(a[5] * r + a[6] * g + a[7] * b); int ib = floattobyte(a[10] * r + a[11] * g + a[12] * b); return color.argb(color.alpha(color), pintobyte(ir), pintobyte(ig), pintobyte(ib)); } private static final float pi = 3.1415926f; @override public boolean ontouchevent(motionevent event) { float x = event.getx() - center_x; float y = event.gety() - center_y; boolean incenter = java.lang.math.sqrt(x*x + y*y) <= center_radius; switch (event.getaction()) { case motionevent.action_down: mtrackingcenter = incenter; if (incenter) { mhighlightcenter = true; invalidate(); break; } case motionevent.action_move: if (mtrackingcenter) { if (mhighlightcenter != incenter) { mhighlightcenter = incenter; invalidate(); } } else { float angle = (float)java.lang.math.atan2(y, x); // need turn angle [-pi ... pi] unit [0....1] float unit = angle/(2*pi); if (unit < 0) { unit += 1; } mcenterpaint.setcolor(interpcolor(mcolors, unit)); invalidate(); } break; case motionevent.action_up: if (mtrackingcenter) { if (incenter) { mlistener.colorchanged(mcenterpaint.getcolor()); } mtrackingcenter = false; // draw w/o halo invalidate(); } break; } return true; } } public colorpickerdialog(context context, oncolorchangedlistener listener, int initialcolor) { super(context); mlistener = listener; minitialcolor = initialcolor; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); oncolorchangedlistener l = new oncolorchangedlistener() { public void colorchanged(int color) { mlistener.colorchanged(color); dismiss(); } }; setcontentview(new colorpickerview(getcontext(), l, minitialcolor)); settitle("pick color"); } }
you can choose erase menu option erase. see part of draw in erased
Comments
Post a Comment