java - Drag/Scroll canvas not smooth after zoom [android coloring book app] -
i'm trying implement zoom feature in coloring book app, zoom working have 3 main problems in canvas after zoom:
- drag/scroll canvas not smooth , difficult after zoom.
- i can't detect pinch motion in order avoid coloring during zoom process.
- after zoom not filling exact tapped point keep filling tapped point.
here code:
// flood fill public class myview extends view { point p1 = new point(); bitmap mbitmap, scaledbitmap; progressdialog pd; canvas canvas; private path path; //test 20/8/2017// private scalegesturedetector scaledetector; private float scalefactor = 1.f; ////////////////// public myview(context context) { super(context); //test 20/8/2017// init(context); ////////////////// this.path = new path(); paint = new paint(); paint.setantialias(true); pd = new progressdialog(context); paint.setstyle(paint.style.stroke); paint.setstrokejoin(paint.join.round); paint.setstrokewidth(5f); int id = getresources().getidentifier("gp" + code + "_" + position, "drawable", getpackagename()); mbitmap = bitmapfactory.decoderesource(getresources(), id).copy(bitmap.config.argb_8888, true); // screen dimension displaymetrics displaymetrics = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(displaymetrics); display getorient = getwindowmanager().getdefaultdisplay(); int orientation = configuration.orientation_undefined; if (getorient.getwidth() == getorient.getheight()) { orientation = configuration.orientation_square; } else { if (getorient.getwidth() < getorient.getheight()) { orientation = configuration.orientation_portrait; } else { orientation = configuration.orientation_landscape; } } if (orientation == configuration.orientation_portrait) { h = displaymetrics.heightpixels / 2 + 100; w = displaymetrics.widthpixels; } if (orientation == configuration.orientation_landscape) { //for mdpi screen 760×1024 tablet display display = getwindowmanager().getdefaultdisplay(); float density = context.getresources().getdisplaymetrics().density; if (density >= 1.0 && display.getwidth() == 1024) { h = displaymetrics.heightpixels - 200; w = displaymetrics.widthpixels - 100; } else { h = displaymetrics.heightpixels - 500; w = displaymetrics.widthpixels - 700; } } scaledbitmap = bitmap.createscaledbitmap(mbitmap, w, h, false); } //test 20/8/2017// public myview(context context, attributeset attrs) { super(context, attrs); init(context); } public myview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(context); } private void init(context ctx) { scaledetector = new scalegesturedetector(ctx, new scalelistener()); } ///////////////// @override protected void ondraw(canvas canvas) { this.canvas = canvas; super.ondraw(canvas); displaymetrics metrics = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(metrics); //test 20/8/2017// canvas.scale(this.scalefactor, this.scalefactor, this.scaledetector.getpreviousspanx(),this.scaledetector.getpreviousspany()); ////////////////// canvas.drawbitmap(scaledbitmap, 0, 0, paint); (path p : paths) { canvas.drawpath(p, paint); canvas.save(); } } @override public boolean ontouchevent(motionevent event) { //test 20/8/2017// // scaledetector.ontouchevent(event); ////////////////// float x = event.getx(); float y = event.gety(); switch (event.getaction()) { case motionevent.action_up: try { p1 = new point(); p1.x = (int) x; p1.y = (int) y; drawnpoints.add(p1); final int sourcecolor = scaledbitmap.getpixel((int) x, (int) y); final int targetcolor = paint.getcolor(); new thetask(scaledbitmap, p1, sourcecolor, targetcolor).execute(); paths.add(path); invalidate(); } catch (exception e) { e.printstacktrace(); } break; case motionevent.action_move: scaledetector.ontouchevent(event); break; } return true; } public void clear() { path.reset(); invalidate(); } public int getcurrentpaintcolor() { return paint.getcolor(); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); w = widthmeasurespec - measurespec.exactly; h = heightmeasurespec - measurespec.exactly; log.d("dim", integer.tostring(w) + " | " + integer.tostring(h)); } class thetask extends asynctask<void, integer, void> { bitmap bmp; point pt; int replacementcolor, targetcolor; public thetask(bitmap bm, point p, int sc, int tc) { this.bmp = bm; this.pt = p; this.replacementcolor = tc; this.targetcolor = sc; pd.show(); } @override protected void onpreexecute() { pd.show(); } @override protected void onprogressupdate(integer... values) { } @override protected void doinbackground(void... params) { floodfill f = new floodfill(); f.floodfill(bmp, pt, targetcolor, replacementcolor); return null; } @override protected void onpostexecute(void result) { pd.dismiss(); invalidate(); } } private class scalelistener extends scalegesturedetector.simpleonscalegesturelistener { @override public boolean onscale(scalegesturedetector detector) { scalefactor *= detector.getscalefactor(); scalefactor = math.max(0.1f, math.min(scalefactor, 10.0f)); invalidate(); return true; } } } public class floodfill { public void floodfill(bitmap image, point node, int targetcolor, int replacementcolor) { int width = image.getwidth(); int height = image.getheight(); int target = targetcolor; int replacement = replacementcolor; if (target != replacement) { queue<point> queue = new linkedlist<point>(); { int x = node.x; int y = node.y; while (x > 0 && image.getpixel(x - 1, y) == target) { x--; } boolean spanup = false; boolean spandown = false; while (x < width && image.getpixel(x, y) == target) { image.setpixel(x, y, replacement); if (!spanup && y > 0 && image.getpixel(x, y - 1) == target) { queue.add(new point(x, y - 1)); spanup = true; } else if (spanup && y > 0 && image.getpixel(x, y - 1) != target) { spanup = false; } if (!spandown && y < height - 1 && image.getpixel(x, y + 1) == target) { queue.add(new point(x, y + 1)); spandown = true; } else if (spandown && y < height - 1 && image.getpixel(x, y + 1) != target) { spandown = false; } x++; } } while ((node = queue.poll()) != null); } } } }
so please need resolve above problems.
kindly let me know in case need more details.
thanks in advance.
best regards,
leenah.
Comments
Post a Comment