Android Bitmap/Canvas offset after scale

2019-01-18 00:49发布

问题:

If I have a canvas, on which I draw a Bitmap like this:

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 before scale was (0, 0), after scale there is a offset and I need that offset.. how can I get it?

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

回答1:

Ok lets try to work out 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); 
}

The same for objectNewY. The new width and height of the bitmap would of course be the multiple of the old size and scale.



回答2:

I believe the cleanest Solution would be to use the underlying transformation Matrix of the Canvas you are manipulating.

In Android there is the canvas.getMatrix(Matrix cmt) method available which will yield it. The transformation matrix will transform any point in world space you throw at into screen coordinates. Just use the matrix.mapPoints(float[] points) and you will be fine.

FYI, you can easily do it the other way around too. If you want to know what screen coordinate maps to which point in world space, e.g. for tapping; the inverse matrix can be used for that. It can be obtained via the matrix.invert(Matrix out) method. Use its mapPoints() for the coordinate mapping then.

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



回答3:

If you'd like know the corners of your screen relative to your original canvas, you can use canvas.getClipBounds(). This returns a Rect with edge coordinates relative to your original canvas. For instance, if you start off with a canvas size of 320 x 480 and call

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

and then

canvas.getClipBounds();

you will have a Rect (call this rect) where

rect.top == 120
rect.bottom == 360
rect.left == 80
rect.right == 240