How to get Drawable ImageView rectangle on Android?

I want to get a rectangle object that will wrap a Drawable ImageView instead of a rectangle that will wrap an ImageView. I will use this rectangle to draw some fancy rectangle around Drawable. How can I get this rectangle?

+5
source share
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(); 

You now have a rectangle.

+4
source

I have not tried this, it just occurred to me when I read your question. What about this one.

Say ImageView does not have a gasket, and gravity is set to the center. The problem is that you cannot just use Drawable width and height to calculate the position of the rectangle, as it can be resized to fit the ImageView. Therefore, you should use ImageView sizes and the ratio of ImageView and Drawable.

When the ratio of Drawable and ImageView is equal, then Drawable completely fills the ImageView, and then the rectangle corresponds to the size of the ImageView. Rectangle coordinates (from the upper left corner, counter): (0/0), (ImageView width / 0), (ImageView width, ImageView height), (0 / ImageView height),

When the coefficients do not match and, for example, you can say that Drawable is wider than ImageView, then Drawable will fill the full width of the ImageView, but not fill the height. But as the Drawable is centered, we can calculate the y coordinate of the upper left corner of the Drawables and therefore for the rectangle. First, calculate the current Drawable height in the ImageView. This must be done because Drawable doesn’t have their original sizes, as they were 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 

Thus, the coordinates of the rectangle (from the upper left corner, counter): (0 / y), (ImageView / y width), (ImageView / y width + height Drawable height), (0 / y + current height Raised)

I hope you get this idea. If Drawable is higher than ImageView, then you can calculate it in the same way, but you must exchange width and height values.

0
source

Depending on the type of scale that applies to the image, it must be reverse engineered on the "configureBounds ()" method 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, when I need the position of the image for the type of center of the scale (I think it is simpler than others, because the image does not scale the image) I do the inverse:

 (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 can create a rectangle, I know this is a little complicated, but it works for me.

0
source

You can use getImageMatrix () See: Android, how to get image borders using matrix

 Rect bounds = imageView.getDrawable().getBounds(); RectF boundsF = new RectF(bounds); imageView.getImageMatrix().mapRect(boundsF); boundsF.round(bounds); 
0
source

I know this is an old question, but if anyone else needs it.

as I understand it, you are trying to get a blocking rectangle something like this: blocking rectangle

If you are trying to get a Rect after the ImageView is drawn in the layout, you can call the View.getHitRect (Rect) method , which will give you a blocking Rect Drawable inside the image view.

 val rect = Rect() view.getHitRect(rect) 
0
source

just convert the selection to a raster object:

 Bitmap bmp = ((BitmapDrawable)dr).getBitmap(); int w = bmp.getWidth(); int h = bmp.getHeight(); 
-2
source

All Articles