Android上如何获取ImageView的Drawable的矩形?

发布于 2024-12-28 19:10:57 字数 105 浏览 0 评论 0原文

我想要获取将包裹 ImageView 的 Drawable 的矩形对象,而不是包裹 ImageView 的矩形。我将使用该矩形在 Drawable 周围绘制一些奇特的矩形。我怎样才能得到那个矩形?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

妄想挽回 2025-01-04 19:10:57
    Rect rect = new Rect();
    ImageView iV = new ImageView();

    rect.left = iV.getLeft();
    rect.top = iV.getTop();
    rect.bottom = iV.getBottom();
    rect.right = iV.getRight();

现在你有了你的矩形。

    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.

随波逐流 2025-01-04 19:10:57

我知道这是一个老问题,但如果有人仍然需要这个。

据我了解,您正在尝试像这样获取阻塞矩形:
阻塞矩形

如果您在布局中绘制 ImageView 之后尝试获取 Rect,那么您可以调用 View.getHitRect(Rect) 方法这将为您提供图像视图内可绘制对象的阻塞矩形。

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

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:
blocking rectangle

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)
别把无礼当个性 2025-01-04 19:10:57

您可以使用 getImageMatrix()
看:
Android 如何使用矩阵获取 imageview 的边界

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

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); 
不甘平庸 2025-01-04 19:10:57

我没有尝试过这个,只是在阅读你的问题时想到的。这又怎样呢。

假设 ImageView 没有填充并且重力设置为居中。问题是您不能简单地使用 Drawable 的宽度和高度来计算矩形的位置,因为它可能会调整大小以适合 ImageView。所以你必须使用ImageView的尺寸以及ImageView和Drawable的比例。

当Drawable和ImageView的比例相等时,Drawable完全填充ImageView,然后矩形与ImageView的尺寸匹配。矩形的坐标为(从左上角开始,逆时针):(0 / 0), (ImageView 宽度 / 0), (ImageView 宽度, ImageView 高度), (0 / ImageView 高度),

当比例不同时匹配,例如,Drawable 比 ImageView 宽,那么 Drawable 将填充 ImageView 的完整宽度,但不会填充高度。但当 Drawable 居中时,我们可以计算 Drawable 左上角的 y 坐标,从而计算矩形的 y 坐标。首先计算ImageView中Drawable的当前高度。这需要完成,因为 Drawable 没有
t 具有其原始尺寸,因为它已调整大小以适合 ImageView。

current height Drawable = (Drawable height / Drawable width) * ImageView width

左上角坐标的y值:

y = (ImageView height - Drawable current height) / 2 

所以矩形的坐标为(从左上角开始,逆向):(0 / y), (width ImageView / y), (width ImageView / y + 当前高度Drawable), (0 / y + Drawable 当前高度)

我希望你明白了。如果Drawable比ImageView高,那么你可以用同样的方式计算它,但必须交换宽度和高度值。

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.

吲‖鸣 2025-01-04 19:10:57

这取决于图像视图应用于可绘制对象的比例类型,您必须这样做
对 ImageView 类的方法“configureBounds()”的逆向工程 (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)。

例如,当我需要中心比例类型的图像位置时(我认为这比其他方法更容易,因为图像视图不会缩放图像),我会执行相反的操作:

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

使用这些数据,您应该能够创建矩形,我知道这有点棘手,但它对我有用。

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.

若有似无的小暗淡 2025-01-04 19:10:57

只需将可绘制对象转换为位图对象:

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

simply convert the drawable into a bitmap object:

Bitmap bmp = ((BitmapDrawable)dr).getBitmap();
int w = bmp.getWidth();
int h = bmp.getHeight();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文