How can I implement Pencil Shades like hb, 2b, 4b, etc. to draw anything in Canvas of my Android Application? -
i need implement pencil shades 9b 9h list showing shades on pencil button click() , after clicking list item, pencil should change new shade. need eraser implementation on erase button click erase path drawn pencil.
this code drawing fixed size pen on canvas without button click. , clearcanvas() method called on button click clears whole canvas.
any appreciated.
public class mainactivity extends appcompatactivity { private canvasview customcanvas; private toolbar toolbar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); customcanvas = (canvasview) findviewbyid(r.id.custom_canvas); toolbar = (toolbar) findviewbyid(r.id.actionbar); setsupportactionbar(toolbar); assert getsupportactionbar()!=null; getsupportactionbar().sethomebuttonenabled(true); getsupportactionbar().setdisplayhomeasupenabled(true); } public void onclick(view view){ customcanvas.clearcanvas(); } } public class canvasview extends view{ public int width; public int height; private bitmap mbitmap; private canvas mcanvas; private path mpath; context context; private paint mpaint, merase; private float mx, my; private static final float tolerance = 5; private boolean erase=false; public canvasview(context c, attributeset attrs) { super(c, attrs); // set new path mpath = new path(); // , set new paint desired attributes mpaint = new paint(); mpaint.setantialias(true); mpaint.setcolor(color.black); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(4f); } // override onsizechanged @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); // canvas draw onto defined bitmap mbitmap = bitmap.createbitmap(w, h, bitmap.config.argb_8888); mcanvas = new canvas(mbitmap); } // override ondraw @override protected void ondraw(canvas canvas) { super.ondraw(canvas); // draw mpath mpaint on canvas when ondraw canvas.drawpath(mpath, mpaint); } // when action_down start touch according x,y values private void starttouch(float x, float y) { mpath.moveto(x, y); mx = x; = y; } // when action_move move touch according x,y values private void movetouch(float x, float y) { float dx = math.abs(x - mx); float dy = math.abs(y - my); if (dx >= tolerance || dy >= tolerance) { mpath.quadto(mx, my, (x + mx) / 2, (y + my) / 2); mx = x; = y; } } // clear canvas public void clearcanvas(){ mpath.reset(); invalidate(); } // when action_up stop touch private void uptouch() { mpath.lineto(mx, my); } //override ontouchevent @override public boolean ontouchevent(motionevent event) { float x = event.getx(); float y = event.gety(); switch (event.getaction()) { case motionevent.action_down: starttouch(x, y); invalidate(); break; case motionevent.action_move: movetouch(x, y); invalidate(); break; case motionevent.action_up: uptouch(); invalidate(); break; } return true; } }
xml file:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include android:id="@+id/actionbar" layout="@layout/action_bar" /> <view android:layout_below="@+id/actionbar" android:layout_width="match_parent" android:layout_height="1dp" android:background="#2084af" /> <com.admin.mainactivity.canvasview android:layout_below="@+id/actionbar" android:id="@+id/custom_canvas" android:layout_width="match_parent" android:layout_height="match_parent" /> </relativelayout>
Comments
Post a Comment