android - Draw bitmap over already drawn background on SurfaceView -
in surfaceview when click on screen creates black dots after connect them , has filled color. after ttrying draw other bitmap , want current state background , when touch screen want draw bitmap on possition , when trying draw bitmap on touch location canvas goes previous state draws.
here code:
package com.inveitix.android.clue.ui.views; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.bitmapshader; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.graphics.shader; import android.util.attributeset; import android.util.log; import android.view.motionevent; import android.view.surfaceholder; import android.view.surfaceview; import com.inveitix.android.clue.r; import com.inveitix.android.clue.cmn.door; import com.inveitix.android.clue.cmn.mappoint; import java.util.arraylist; import java.util.list; public class drawingview extends surfaceview { private static final string tag = "drawingview"; private static final float door_size = 30; private paint paint; private canvas canvas; private int maxheight; private int maxwidth; private surfaceholder surfaceholder; private list<mappoint> shape; private list<door> doors; private float ratio; private boolean isfloorfinished; private boolean isdoorselected; path path; public drawingview(context context) { super(context); init(); } public drawingview(context context, attributeset attrs) { super(context, attrs); init(); } public drawingview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(); } public void setisdoorselected(boolean isdoorselected) { this.isdoorselected = isdoorselected; } public void setisfloorfinished(boolean isfloorfinished) { this.isfloorfinished = isfloorfinished; } private void init() { paint = new paint(paint.anti_alias_flag); isfloorfinished = false; surfaceholder = this.getholder(); preparecanvas(); paint.setcolor(color.black); paint.setstyle(paint.style.fill); shape = new arraylist<>(); doors = new arraylist<>(); } private void preparecanvas() { surfaceholder.addcallback(new surfaceholder.callback() { public void surfacedestroyed(surfaceholder holder) { } public void surfacecreated(surfaceholder holder) { canvas = holder.lockcanvas(); canvas.drawcolor(color.white); holder.unlockcanvasandpost(canvas); } public void surfacechanged(surfaceholder holder, int format, int width, int height) { } }); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { int minw = getpaddingleft() + getpaddingright() + getsuggestedminimumwidth(); int minh = getpaddingbottom() + getpaddingtop() + getsuggestedminimumheight(); this.maxwidth = resolvesizeandstate(minw, widthmeasurespec, 1); this.maxheight = resolvesizeandstate(minh, heightmeasurespec, 1); if (ratio != 0) { maxheight = (int) (maxwidth / ratio); } log.i(tag, "onmeasure width:" + maxwidth); log.i(tag, "onmeasure height:" + maxheight); setmeasureddimension(maxwidth, maxheight); } @override public boolean ontouchevent(motionevent event) { if (event.getaction() == motionevent.action_down && !isfloorfinished) { if (surfaceholder.getsurface().isvalid()) { shape.add(new mappoint(event.getx(), event.gety())); canvas = surfaceholder.lockcanvas(); canvas.drawcolor(color.white); (mappoint point : shape) { canvas.drawcircle(point.getx(), point.gety(), 10, paint); } surfaceholder.unlockcanvasandpost(canvas); } } else if (event.getaction() == motionevent.action_down && isdoorselected) { if (surfaceholder.getsurface().isvalid()) { door door = new door(); door.setconnectedto("door1"); //this test door.setx(event.getx()); door.sety(event.gety()); doors.add(door); canvas = surfaceholder.lockcanvas(); drawdoors(canvas); surfaceholder.unlockcanvasandpost(canvas); } } return false; } public void drawfloor() { bitmap bmpfloorpattern = bitmapfactory.decoderesource(getresources(), r.drawable.floor_pattern6); bitmapshader patternbmpshader = new bitmapshader(bmpfloorpattern, shader.tilemode.repeat, shader.tilemode.repeat); canvas = surfaceholder.lockcanvas(); path = new path(); path.reset(); if (shape != null) { path.moveto(shape.get(0).getx(), shape.get(0).gety()); alignpoints(path); canvas.drawcolor(color.white); (int = 0; < shape.size(); i++) { path.lineto(shape.get(i).getx(), shape.get(i).gety()); } } paint.setshader(patternbmpshader); path.close(); canvas.drawpath(path, paint); paint.setshader(null); surfaceholder.unlockcanvasandpost(canvas); } public void drawdoors(canvas canvas) { bitmap bmpdoor = bitmapfactory.decoderesource(getresources(), r.drawable.door32); paint.setfilterbitmap(true); if (doors != null && doors.size() > 0) { (door door : doors) { canvas.drawbitmap(bmpdoor, door.getx() - door_size, door.gety() - door_size, null); } } } private void alignpoints(path path) { mappoint previouspoint = null; (int = 0; < shape.size(); i++) { float x = shape.get(i).getx(); float y = shape.get(i).gety(); if (previouspoint != null) { float deltax = math.abs(x - previouspoint.getx()); float deltay = math.abs(y - previouspoint.gety()); if (math.max(deltax, deltay) == deltax) { x = previouspoint.getx(); } else { y = previouspoint.gety(); } } path.lineto(x, y); previouspoint = shape.get(i); } } public void setwidthtoheightratio(float ratio) { this.ratio = ratio; maxheight = (int) (maxwidth / ratio); invalidate(); requestlayout(); } }
any ideas how draw on drawn background ?
i found have draw doors in same time floor did changes:
@override public boolean ontouchevent(motionevent event) { if (event.getaction() == motionevent.action_down && !isfloorfinished) { if (surfaceholder.getsurface().isvalid()) { shape.add(new mappoint(event.getx(), event.gety())); canvas = surfaceholder.lockcanvas(); canvas.drawcolor(color.white); (mappoint point : shape) { canvas.drawcircle(point.getx(), point.gety(), 10, paint); } surfaceholder.unlockcanvasandpost(canvas); } } else if (event.getaction() == motionevent.action_down && isdoorselected) { if (surfaceholder.getsurface().isvalid()) { door door = new door(); door.setconnectedto("door1"); //this test door.setx(event.getx()); door.sety(event.gety()); doors.add(door); drawfloor(); } } return false; } public void drawfloor() { bitmap bmpfloorpattern = bitmapfactory.decoderesource(getresources(), r.drawable.floor_pattern6); bitmapshader patternbmpshader = new bitmapshader(bmpfloorpattern, shader.tilemode.repeat, shader.tilemode.repeat); canvas = surfaceholder.lockcanvas(); path = new path(); path.reset(); if (shape != null) { path.moveto(shape.get(0).getx(), shape.get(0).gety()); alignpoints(path); canvas.drawcolor(color.white); (int = 0; < shape.size(); i++) { path.lineto(shape.get(i).getx(), shape.get(i).gety()); } } paint.setshader(patternbmpshader); path.close(); canvas.drawpath(path, paint); paint.setshader(null); if(doors.size() > 0) { drawdoors(); } surfaceholder.unlockcanvasandpost(canvas); }
Comments
Post a Comment