问题
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?
回答1:
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.
回答2:
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);
回答3:
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)
回答4:
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.
回答5:
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.
回答6:
simply convert the drawable into a bitmap object:
Bitmap bmp = ((BitmapDrawable)dr).getBitmap();
int w = bmp.getWidth();
int h = bmp.getHeight();
来源:https://stackoverflow.com/questions/8987580/how-to-get-the-rectangle-of-the-drawable-of-imageview-on-android