I want to get the rectangle object that will wrap the Drawable of ImageView instead of the rectangle that will wrap the ImageView. I will use that rectangle to draw some fancy rectangle around Drawable. How can I get that rectangle?
6 Answers
Rect rect = new Rect();
ImageView iV = new ImageView();
rect.left = iV.getLeft();
rect.top = iV.getTop();
rect.bottom = iV.getBottom();
rect.right = iV.getRight();
Now you have your rectangle.

- 864
- 3
- 11
- 27
-
12this gives the rectangle surrounding ImageView, but I want the rectangle surrounding the Drawable in ImageView. – ipman Jan 24 '12 at 13:42
I know its an old question but if someone still needs this.
as I understand you are trying to get the blocking rectangle like so:
If you are trying to get the Rect after the ImageView is drawn in the layout then you can call the View.getHitRect(Rect) method which will give you the blocking Rect of the Drawable inside the image view.
val rect = Rect()
view.getHitRect(rect)

- 1,161
- 15
- 18
You can use getImageMatrix() See: Android how to get bounds of imageview with matrix
Rect bounds = imageView.getDrawable().getBounds();
RectF boundsF = new RectF(bounds);
imageView.getImageMatrix().mapRect(boundsF);
boundsF.round(bounds);

- 47
- 2
It depends on the scale type that the image view applies to the drawable, you have to do inverse engineering over the method "configureBounds()" of the ImageView class (http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/android/widget/ImageView.java#ImageView.configureBounds%28%29).
for example I when I need the image position for a center scale type (I think it is easier than the others because the image view does not scale the image) I do the inverse of:
(mDrawMatrix.setTranslate((int) ((vwidth - dwidth) * 0.5f + 0.5f),
(int) ((vheight - dheight) * 0.5f + 0.5f));)
ImageView iv (Your ImageView);
Drawable dw = iv.getDrawable();
int dwidth = dw.getIntrinsicWidth();
int dheight = dw.getIntrinsicHeight();
int vwidth = iv.getWidth() - iv.getPaddingLeft() - iv.getPaddingRight();
int vheight = iv.getHeight() - iv.getPaddingTop() - iv.getPaddingBottom();
int realImageWidth = (int)((vwidth - dwidth) * 0.25f);
// (I do not know why I had to put 0.25f instead of 0.5f,
// but I think this issue is a consequence of the screen density)
int realImageHeight = (int)((vheight - dheight) * 0.25f);
And with this data you should be able to create the rectangle, I know this is a bit tricky but it works for me.
-
Did you understand the part of the 0.25f ? Why do you think it has to do with density, if all things here are in pixels ? – android developer Jun 08 '15 at 07:39
I haven't tried this, it just came to my mind when reading your question. What about this.
Let's say the ImageView has no padding and gravity is set to centered. The problem is that you cannot simple use the width and height of the Drawable to calculate the rectangle's position as it might get resized to fit the ImageView. So you got to use the ImageView's dimensions and the ratio of the ImageView and Drawable.
When the ratio of the Drawable and the ImageView is equal then the Drawable fills the ImageView completely and then the rectangle matches the dimensions of the ImageView. Coordinates of the rectangle are (from the upper left corner, counter wise): (0 / 0), (width ImageView / 0), (width ImageView, height ImageView), (0 / height ImageView),
When the ratios don't match and let's say for example the Drawable is wider than the ImageView then the Drawable will fill the complete width of the ImageView but will not fill the height. But as the Drawable get centered we can calculate the y coordinate of the Drawables upper left corner and therefore for the rectangle. First calculate the current height of the Drawable in the ImageView. This needs to be done as the Drawable doesn' t have its original dimensions as it was resized to fit the ImageView.
current height Drawable = (Drawable height / Drawable width) * ImageView width
y value of the coordinate of the upper left corner:
y = (ImageView height - Drawable current height) / 2
So the coordinates of the rectangle are (from the upper left corner, counter wise): (0 / y), (width ImageView / y), (width ImageView / y + current height Drawable), (0 / y + current height Drawable)
I hope you get the idea. If the Drawable is higher than the ImageView then you can calculate it the same way but have to exchange the width and height values.

- 27,355
- 15
- 87
- 125
simply convert the drawable into a bitmap object:
Bitmap bmp = ((BitmapDrawable)dr).getBitmap();
int w = bmp.getWidth();
int h = bmp.getHeight();

- 1,617
- 15
- 18
-
3this only gives the width and height of the Drawable, but I want the rectangle that surrounds the Drawable. – ipman Jan 24 '12 at 13:43
-
can you some better explain what you mean? Do you mean the layout out from the ImageView? – Andreas Jan 24 '12 at 13:50
-
No I mean, sometimes ImageView has some space beetween its bounds and Drawable in the ImageView. I do not want to include this spacing in my rectangle. – ipman Jan 24 '12 at 13:55
-
A Imageview is only a View (extended) that draws a Bitmap, i think you cant get other sizes as from ImageView/Drawable/Bitmap as in known size functions. Only a ImageView can have a padding (its inside the view and can reduce the "imagesize" itself, so you can ask for padding in the ImageView and compare it with the rect(width/height) of the real drawable/bitmap. – Andreas Jan 24 '12 at 13:58