android - Draw dash line on a Canvas -
how can draw dash line on canvas. tried this:
paint dashpaint = new paint(); dashpaint.setargb(255, 0, 0, 0); dashpaint.setstyle(paint.style.stroke); dashpaint.setpatheffect(new dashpatheffect(new float[]{5, 10, 15, 20}, 0)); canvas.drawline(0, canvas.getheight() / 2, canvas.getwidth(), canvas.getheight() / 2, dashpaint);
and gave me not dash line simple one.
you drawing line
canvas.drawline(0, canvas.getheight() / 2, canvas.getwidth(), canvas.getheight() / 2, dashpaint)
this draw line
solution
private path mpath; mpath = new path(); mpath.moveto(0, h / 2); mpath.quadto(w/2, h/2, w, h/2); h , w height , width of screen paint mpaint = new paint(); mpaint.setargb(255, 0, 0, 0); mpaint.setstyle(paint.style.stroke); mpaint.setpatheffect(new dashpatheffect(new float[]{5, 10, 15, 20}, 0));
in ondraw()
canvas.drawpath(mpath, mpaint);
snap shot
i have background , dashed line drew on it.
example
public class fingerpaintactivity extends activity { myview mv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mv = new myview(this); setcontentview(mv); mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(0xffff0000); mpaint.setargb(255, 0, 0, 0); mpaint.setstyle(paint.style.stroke); mpaint.setpatheffect(new dashpatheffect(new float[]{10, 40,}, 0)); mpaint.setstrokewidth(12); } private paint mpaint; public class myview extends view { 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); mpath.moveto(0, h / 2); mpath.quadto(w / 2, h / 2, w, h / 2); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint); canvas.drawpath(mpath, mpaint); } } }
modify above according needs.
Comments
Post a Comment