Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 997
Sunne Rotterz Avatar asked Jul 26 '11 19:07

Sunne Rotterz


3 Answers

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.

like image 186
Lumis Avatar answered Nov 19 '22 08:11

Lumis


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

like image 4
Felix Ruzzoli Avatar answered Nov 19 '22 10:11

Felix Ruzzoli


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
like image 2
aostiles Avatar answered Nov 19 '22 09:11

aostiles