How Do I Make The Coordinates Of Motionevent Match The Ones Of A Scaled Canvas?
Solution 1:
After looking through StackOverflow and Google for the past 2 days looking for something that comes close to a solution, I came over this: Get Canvas coordinates after scaling up/down or dragging in android Which solved the problem. It was really hard to find because the title was slightly misleading(of the other question)
floatpx= e.getX() / mScaleFactorX;
floatpy= e.getY() / mScaleFactorY;
intipy= (int) py;
intipx= (int) px;
Rectr=newRect(ipx, ipy, ipx+2, ipy+2);
I added this as an answer and accepting it so it no longer will be an unanswered question as it is solved. The code above converts the coordinates to integers so they can be used for checking collision between the finger and the object I'm checking with
Solution 2:
Don't scale the canvas directly. Make a Matrix object, scale that once. Then concat that to the canvas. Then you can make an inverted matrix for your touch events.
And just make the invert matrix whenever you change the view matrix:
viewMatrix = newMatrix();
viewMatrix.scale(scalefactor,scalefactor);
invertMatrix = newMatrix(viewMatrix);
invertMatrix.invert(invertMatrix);
Then apply these two matrices to the relevant events.
@OverridepublicbooleanonTouchEvent(MotionEvent event) {
event.transform(invertMatrix);
And then on the draw events, concat the matrix.
@OverrideprotectedvoidonDraw(Canvas canvas) {
canvas.concat(viewMatrix);
And you're done. everything is taken care of for you. Whatever modifications you do to the view matrix will change your viewbox and your touch events will be translated into that same scene too.
If you want to add panning or rotation, or even skew the view, it's all taken care of. Just apply that to the matrix, get the inverted matrix, and the view will look that way and the touch events will respond as you expect.
Post a Comment for "How Do I Make The Coordinates Of Motionevent Match The Ones Of A Scaled Canvas?"