android - Drawing a repeated image (tile) along a path -
i trying repeat image resources along path, done shapes in patheffect
. below 2 images of have vs. i'm trying achieve.
- what achieved using
pathdashpatheffect
triangle shapes: screenshot - my desired outcome should this: desired
here code:
import android.annotation.suppresslint; import android.content.context; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.graphics.pathdashpatheffect; import android.graphics.patheffect; import android.util.attributeset; import android.view.motionevent; import android.view.view; public class imagelineview extends view { private paint mpaint; private bitmap mdrawingbitmap; private canvas mdrawingcanvas; private path mpath; private paint mbitmappaint; private static final int background_color = color.white; private static final int center_stroke_width = 10; public imagelineview(context context) { super(context); initialize(); } public imagelineview(context context, attributeset attrs) { super(context, attrs); initialize(); } public imagelineview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); initialize(); } @suppresslint("newapi") public imagelineview(context context, attributeset attrs, int defstyleattr, int defstyleres) { super(context, attrs, defstyleattr, defstyleres); initialize(); } private void initialize() { mpath = new path(); mbitmappaint = new paint(paint.dither_flag); mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setfilterbitmap(true); mpaint.setcolor(color.red); mpaint.setstyle(paint.style.fill_and_stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokewidth(10); mpaint.setstrokecap(paint.cap.round); mpaint.setpatheffect(gettrianglepatheffect(center_stroke_width)); setlayertype(layer_type_software, null); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); // foreground bitmap if (mdrawingbitmap == null) { mdrawingbitmap = bitmap.createbitmap(w, h, bitmap.config.argb_8888); } else { mdrawingbitmap = bitmap.createscaledbitmap(mdrawingbitmap, w, h, false); } mdrawingcanvas = new canvas(mdrawingbitmap); drawinitialview(); } @override protected void ondraw(canvas canvas) { canvas.drawbitmap(mdrawingbitmap, 0, 0, mbitmappaint); } private void drawinitialview() { mdrawingcanvas.drawcolor(background_color); } private void touchdraw(float x, float y) { mdrawingcanvas.drawcolor(background_color); mpath.reset(); mpath.moveto(x, y); int canvaswidth = mdrawingcanvas.getwidth(); int canvasheight = mdrawingcanvas.getheight(); mpath.moveto(x,y); mpath.lineto(0,0); mpath.moveto(x,y); mpath.lineto(0,canvasheight); mpath.moveto(x,y); mpath.lineto(canvaswidth,0); mpath.moveto(x,y); mpath.lineto(canvaswidth,canvasheight); mdrawingcanvas.drawpath(mpath, mpaint); mpath.reset(); } private patheffect gettrianglepatheffect(int strokewidth) { return new pathdashpatheffect( gettriangle(strokewidth), strokewidth, 0.0f, pathdashpatheffect.style.rotate); } private path gettriangle(float size) { path path = new path(); float half = size / 2; path.moveto(-half, -half); path.lineto(half, -half); path.lineto(0, half); path.close(); return path; } private void touchdown(float x, float y) { touchdraw(x, y); } private void touchmove(float x, float y) { touchdraw(x, y); } private void touchup() { resetcanvas(); } private void resetcanvas() { drawinitialview(); } @suppresslint("clickableviewaccessibility") @override public boolean ontouchevent(motionevent event) { float x = event.getx(); float y = event.gety(); int motionaction = event.getaction(); switch (motionaction) { case motionevent.action_down: touchdown(x, y); invalidate(); break; case motionevent.action_move: touchmove(x, y); invalidate(); break; case motionevent.action_up: touchup(); invalidate(); break; default: break; } return true; } }
try following piece of code
import android.content.context; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.paint.style; import android.graphics.rect; import android.graphics.shader.tilemode; import android.graphics.drawable.bitmapdrawable; import android.view.view; public class tiling extends view { rect rect; bitmap mbitmap; bitmapdrawable mdrawable; public tiling(context mcontext) { super(mcontext); rect = new rect(0, 0, 100, 100); mbitmap = loadbitmap(); mdrawable = new bitmapdrawable(mcontext.getresources(), mbitmap); mdrawable.settilemodexy(tilemode.repeat, tilemode.repeat); mdrawable.setbounds(rect); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); mdrawable.draw(canvas); } public bitmap loadbitmap() { paint paint = new paint(); paint.setcolor(color.black); bitmap bm = bitmap.createbitmap(10, 10, bitmap.config.argb_8888); canvas canvas = new canvas(bm); canvas.drawrect(0, 0, 10, 10, paint); paint.setstyle(style.stroke); paint.setcolor(color.white); canvas.drawrect(0, 0, 9, 9, paint); return bm; }
}
Comments
Post a Comment