Skip to content Skip to sidebar Skip to footer

Android Bitmap/canvas Offset After Scale

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 w

Solution 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.

Solution 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()

Solution 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 == 120rect.bottom == 360rect.left == 80rect.right == 240

Post a Comment for "Android Bitmap/canvas Offset After Scale"