Android上如何获取ImageView的Drawable的矩形?
我想要获取将包裹 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
现在你有了你的矩形。
Now you have your rectangle.
我知道这是一个老问题,但如果有人仍然需要这个。
据我了解,您正在尝试像这样获取阻塞矩形:
如果您在布局中绘制 ImageView 之后尝试获取 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:
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.
您可以使用 getImageMatrix()
看:
Android 如何使用矩阵获取 imageview 的边界
You can use getImageMatrix()
See:
Android how to get bounds of imageview with matrix
我没有尝试过这个,只是在阅读你的问题时想到的。这又怎样呢。
假设 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。
左上角坐标的y值:
所以矩形的坐标为(从左上角开始,逆向):(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.
y value of the coordinate of the upper left corner:
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.
这取决于图像视图应用于可绘制对象的比例类型,您必须这样做
对 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)。
例如,当我需要中心比例类型的图像位置时(我认为这比其他方法更容易,因为图像视图不会缩放图像),我会执行相反的操作:
使用这些数据,您应该能够创建矩形,我知道这有点棘手,但它对我有用。
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:
And with this data you should be able to create the rectangle, I know this is a bit tricky but it works for me.
只需将可绘制对象转换为位图对象:
simply convert the drawable into a bitmap object: