Centered magnification in user view - calculation of canvas coordinates

I am working on my first "real" Android application, a graphical workflow editor. The drawing is executed in a custom class, which is a subclass of View. At the moment when my elements are rectangles that are drawn on the canvas. To detect actions on elements, I compare the coordinates and check the elements at the point of contact.

To implement the zoom gesture, I tried http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html

Using the canvas.scale (...) function with 4 arguments, centered scaling works well, but I lose the ability to calculate the coordinates of the canvas using the offset from mPosX and mPosY to determine if the item has a touch after scaling.

I tried changing the example in the blogpost above to center the canvas on the zoom gesture with:

canvas.save(); canvas.translate(mPosX, mPosY); canvas.scale(mScaleFactor, mScaleFactor, mScalePivotX, mScalePivotY); //drawing .... canvas.restore(); 

I did not find examples of how this could be done without losing the offset of the link to calculate the coordinates. Is there an easy way? I tried to calculate the offset with the center of gestures and the zoom factor, but failed: /

I have seen that other examples that use ImageView often use Matrix to transform the image. Can this be done with a custom view and canvas? If so, how can I get the x and y offset to check the coordinates?

Also, if my ideas are completely wrong, I would be very happy to see some examples of how this is done correctly.

thanks!;)

+6
android view zoom
source share
1 answer

Perhaps the following code will help you calculate the coordinates with the center of gestures and the zoom factor. I use this method in my class representing opengl-sprite.

 void zoom(float scale, PointF midPoint) { if (zoomFactor == MAX_ZOOM_FACTOR && scale > 1) return; if (zoomFactor == MIN_ZOOM_FACTOR && scale < 1) return; zoomFactor *= scale; x = (x - midPoint.x) * scale + midPoint.x; y = (y - height + midPoint.y) * scale + height - midPoint.y; if (zoomFactor >= MAX_ZOOM_FACTOR) { zoomFactor = MAX_ZOOM_FACTOR; } else if (zoomFactor < MIN_ZOOM_FACTOR) { zoomFactor = MIN_ZOOM_FACTOR; x = 0; y = 0; } } 

The X and Y coordinates are handled differently, due to the difference between the directions of the opengl coordinate system (right and up) and the MidPoint coordinate system (right and down). midPoint is taken from MotionEvent coordinates.

All other operations are clear, I think.

Hope this helps you.

+3
source share

All Articles