canvas - Android drawing a path ON TOP of another path -
so facing weird problem in drawing app.
i drew these lines 1 8 top bottom , left right.
while i'm drawing line, shows it's drawing behind other lines. whenever let go off screen, sometimes pops front, seems random.
what overlooking draw on top of else @ times?
my drawview.java:
public class drawview extends view implements ontouchlistener { private path path = new path(); private paint paint = new paint(); private map<path, paint> pathmap = new hashmap<path, paint>(); private boolean isscreencleared = false; public drawview(context context) { super(context); this.setontouchlistener(this); paint.setcolor(color.black); paint.setantialias(true); paint.setstrokewidth(5); paint.setstyle(paint.style.stroke); paint.setstrokejoin(paint.join.round); paint.setstrokecap(paint.cap.round); } @override public void ondraw(canvas canvas) { if (isscreencleared) { pathmap.clear(); canvas.drawcolor(color.white); isscreencleared = false; } else { //current line canvas.drawpath(path, paint); //all other lines (map.entry<path, paint> p : pathmap.entryset()) { canvas.drawpath(p.getkey(), p.getvalue()); } } } public boolean ontouch(view view, motionevent event) { float eventx = event.getx(); float eventy = event.gety(); switch (event.getaction()) { case motionevent.action_down: path = new path(); path.reset(); path.moveto(eventx, eventy); return true; case motionevent.action_move: path.lineto(eventx, eventy); break; case motionevent.action_up: paint newpaint = new paint(); newpaint.set(paint); pathmap.put(path, newpaint); break; default: return false; } invalidate(); return true; } public float getradius() { return paint.getstrokewidth(); } public void setradius(float radius) { paint.setstrokewidth(radius); } public void setcolor(int color) { paint.setcolor(color); system.out.println("color set to: " + color); } public void clearscreen() { isscreencleared = true; invalidate(); } }
i instantiating drawview in mainactivity this:
relative layout = (relativelayout) findviewbyid(r.id.drawscreen); drawview dv = new drawview(layout.getcontext());
don't use hashmap store paths. use vector , add new paths end. draw them , iterate through vector draw them in right order recent on top.
from hashmap docs:
note iteration order hashmap non-deterministic.
Comments
Post a Comment