Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get measured size and position of bitmap on canvas after 2D transformation

I have a bunch of bitmaps that I draw on a Canvas applied with 2D transformation (android.graphics.Camera and android.graphics.Matrix). How do I get the sizes/positions of the bitmaps on Canvas? The size would be the bounds of the bitmap as rendered on Canvas and the position would be the coordinates of the top/left corner of the bounds. All the bitmaps are regular rectangles without an alpha channel.

like image 572
mobileTofu Avatar asked Oct 11 '22 03:10

mobileTofu


1 Answers

The matrix class has a mapRect function for this purpose:

mapRect(RectF dst, RectF src) Apply this matrix to the src rectangle, and write the transformed rectangle into dst.

Give the dimensions of the image as the source (and 0,0 for the x,y if you're using the whole image) and it will give you the transformed dimensions back as the dst rectangle.

like image 106
Simon Sarris Avatar answered Nov 03 '22 01:11

Simon Sarris