Android: How to Zoom, Pan(Scroll) Tic Tac Toe Board View -
so, have custom boardview class extends view. implemented drawing board, lines , drawing "o" drawable when user press on cell.
but, not implement following problems correctly:
1. zoom boardview when user doing pinch(multi touch).
2. scroll boardview left, right, top, bottom if boardview bigger boardview initial width or height.
3. find right cell coordinate when user pressed on cell after zooming or scrolling.
this first game project, please me if know how solve problem.
i tried did not work properly. boardview width equal width screen width , boardview height equal boardview width. square board view.
i give 200 bounty implementing problem.
here project, can download , edit: https://drive.google.com/file/d/0bxniutd_m1x8cuq2ngpsmdbuvve/view?usp=sharing
github: https://github.com/boyfox/gametictactoe
boardview code: http://pastie.org/10109253 or http://pastebin.com/tru8ybds
i found solution self, need improving code, can answer question solution!
could please move code github? easier download, edit , propose changes.
if you're looking generic implementation of 2 finger zoom/rotate, take @ game (https://github.com/zieiony/gravity). interesting part gamepanel view , dispatchtouchevent method:
private pointf prevpos = new pointf(), prevpos2 = new pointf(); float scale = 1; final float min_scale = 0.2f, max_scale = 2.0f; float rotation = 0; matrix matrix = new matrix(); private float prevdist; public boolean dispatchtouchevent(android.view.motionevent event) { if (event.getpointercount() == 2) { float d = dist(event.getx(0), event.gety(0), event.getx(1), event.gety(1)); float pivotx = (event.getx(0) + event.getx(1)) / 2; float pivoty = (event.gety(0) + event.gety(1)) / 2; float prevpivotx = (prevpos.x + prevpos2.x) / 2; float prevpivoty = (prevpos.y + prevpos2.y) / 2; if (event.getaction() == motionevent.action_move) { float newscale = scale * d / prevdist; newscale = math.max(min_scale, math.min(newscale, max_scale)); float scalefactor = newscale / scale; scale = newscale; matrix.postscale(scalefactor, scalefactor, pivotx, pivoty); float prevangle = (float) math.atan2( prevpos.x - prevpos2.x, prevpos.y - prevpos2.y); float angle = (float) math.atan2( event.getx(0) - event.getx(1), event.gety(0) - event.gety(1)); rotation += prevangle - angle; matrix.postrotate( (float) ((prevangle - angle) * 180.0f / math.pi), pivotx, pivoty); matrix.posttranslate(-prevpivotx + pivotx, -prevpivoty + pivoty); } prevpos.x = event.getx(0); prevpos.y = event.gety(0); prevpos2.x = event.getx(1); prevpos2.y = event.gety(1); prevdist = d; } return true; }
this method produces transformation matrix, should use drawing.
protected void dispatchdraw(canvas canvas) { canvas.save(); canvas.setmatrix(matrix); // drawing here canvas.restore(); }
Comments
Post a Comment