Android bitmap / canvas reduction after scaling

If I have a canvas on which I draw Bitmap as follows:

canvas.drawBitmap(bmLargeImage, srcRect, destRect, paint); 

and I scale the bitmap:

 canvas.scale(1.5f, 1.5f, 450, 250); 

I want to get the position of the bitmap after the scale. If the position in front of the scale was (0, 0), after scaling there is an offset, and I need this offset .. how can I get it?

Thanks and sorry for the simple question, newbie here ...

+4
source share
3 answers

Ok lets try to find the best formula for this

 canvas.scale(scaleX, scaleY, pivotX, pivotY); if (scaleX >= 1){ objectNewX = objectOldX + (objectOldX - pivotX)*(scaleX - 1); }else{ objectNewX = objectOldX - (objectOldX - pivotX)*(1 - scaleX); } 

Same thing for objectNewY. The new width and height of the bitmap, of course, would be brief in size and scale.

+13
source

I believe that the purest solution would be to use the basic Canvas transform matrix that you manipulate.

Android has an available canvas.getMatrix(Matrix cmt) method that will give it. The transformation matrix transforms any point in the world space that you throw into the coordinates of the screen. Just use matrix.mapPoints(float[] points) and everything will be fine.

FYI, you can easily do this and vice versa. If you want to know what screen coordinates will be mapped to a point in world space, for example. for tapping; an inverse matrix can be used for this. It can be obtained using the matrix.invert(Matrix out) method. Use its mapPoints() to display the coordinates then.

Here are the official docs: mapPoints () , invert () , getMatrix ()

+3
source

If you want to know the angles of the screen relative to the original canvas, you can use canvas.getClipBounds() . This returns a Rect with boundary coordinates relative to your original canvas. For example, if you start with a 320 x 480 canvas and call

 canvas.scale(2, 2, getWidth()/2, getHeight()/2); 

and then

 canvas.getClipBounds(); 

you will have a Rect (call it Rect ) where

 rect.top == 120 rect.bottom == 360 rect.left == 80 rect.right == 240 
+1
source

All Articles