canvas - android path shifted once finger is removed -
i working on drawing app , allows users import image farther draw on it. images bigger drawing area scaled downwards such meet max screenwidth or screenheight.
the imported image placed @ center of drawingview using canvas.drawbitmap(bitmap, x_adjustment, y_adjustment, paintscreen);
in way there blank spaces on left, right or top, downwards of imported image. adjustment counting (0,0)
x_adjustment
, y_adjustment
ondraw
@override protected void ondraw(canvas canvas) { canvas.drawbitmap(bitmap, x_adjustment, y_adjustment, paintscreen); (integer key : pathmap.keyset()) canvas.drawpath(pathmap.get(key), paintline); // draw line }
touchstarted:
private void touchstarted(float x, float y, int lineid) { path path; // used store path given touch id point point; // used store lastly point in path path = new path(); // create new path pathmap.put(lineid, path); // add together path map point = new point(); previouspointmap.put(lineid, point); path.moveto(x, y); point.x = (int) x; point.y = (int) y; }
touchmoved:
// called when user drags along screen private void touchmoved(motionevent event) { // each of pointers in given motionevent (int = 0; < event.getpointercount(); i++) { // pointer id , pointer index int pointerid = event.getpointerid(i); int pointerindex = event.findpointerindex(pointerid); if (pathmap.containskey(pointerid)) { // new coordinates pointer float newx = event.getx(pointerindex); float newy = event.gety(pointerindex); // path , previous point associated pointer path path = pathmap.get(pointerid); point point = previouspointmap.get(pointerid); float deltax = math.abs(newx - point.x); float deltay = math.abs(newy - point.y); if (deltax >= touch_tolerance || deltay >= touch_tolerance) { path.quadto(point.x, point.y, ((newx + point.x)/2),((newy + point.y)/2)); // store new coordinates point.x = (int) newx ; point.y = (int) newy ; } } } }
touchended:
private void touchended(int lineid) { path path = pathmap.get(lineid); bitmapcanvas.drawpath(path, paintline); path.reset(); }
question: since imported image placed @ center not (0,0), every line drawn, though when showing when drawing , screen-touching, when user removes finger, i.e. touch ended, finalized line shifted x_adjustment , y_adjustment.
e.g. if scaled image width < screenwidth, there blank space on left , right, when drawing line showing correctly, yet when finger removed, line wrongly shift right x_adjustment
;
i know because of adjustments create error. know save path's coordinates x,y shift. dont know how modify codes , have tried add together adjustment paths still fails. kindly help give me guides? many thanks!
android canvas path bitmap
No comments:
Post a Comment