ImageView 位图比例尺寸
我有一个比我放入的 ImageView 更大的位图。我将 ScaleType 设置为 center_inside。如何获得缩小图像的尺寸?
I have a Bitmap that is larger than the ImageView that I'm putting it in. I have the ScaleType set to center_inside. How do I get the dimensions of the scaled down image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的。我可能应该更清楚。在将缩放位图绘制到屏幕上之前,我需要缩放位图的高度和宽度,以便我可以在正确的位置绘制一些叠加层。我知道原始位图中叠加层的位置,但不知道缩放后的位置。我想出了一些简单的公式来计算它们应该在缩放位图上的位置。
我会解释一下我做了什么,以防有一天其他人可能需要这个。
我得到了位图的原始宽度和高度。 ImageView 的高度和宽度在 xml 文件中硬编码为 335。
我确定了哪一个更大,以便我可以正确地找出计算的基础。对于我当前的示例,宽度较大。由于宽度缩小到 ImageView 的宽度,我必须找到缩小后的高度。我只是将 ImageView 的宽度与位图的宽度之比乘以位图的高度。除法是最后完成的,因为首先进行整数除法会得到 0 的结果。
通过缩放的高度,我可以通过使用以下方法确定缩放位图两侧的空白空间量:
确定叠加位置的 x 和 y 坐标应该在缩放位图上我必须使用原始坐标和这些计算出的数字。本例中的 x 坐标很简单。由于缩放后的宽度是 ImageView 的宽度,因此我只需将 ImageView 的宽度与 Bitmap 的宽度之比乘以原始 x 坐标即可。
y 坐标有点棘手。取位图缩放后的高度与位图原始高度的比率。将该值与原始 y 坐标相乘。然后添加空白区域缓冲区。
这适合我的需要。如果高度大于宽度,只需反转宽度和高度变量即可。
Ok. I probably should have been clearer. I needed the height and width of the scaled Bitmap before it's ever drawn to the screen so that I could draw some overlays in the correct position. I knew the position of the overlays in the original Bitmap but not the scaled. I figured out some simple formulas to calculate where they should go on the scaled Bitmap.
I'll explain what I did in case someone else may one day need this.
I got the original width and height of the Bitmap. The ImageView's height and width are hard-coded in the xml file at 335.
I determined which one was larger so that I could correctly figure out which one to base the calculations off of. For my current example, width is larger. Since the width was scaled down to the the width of the ImageView, I have to find the scaled down height. I just multiplied the ratio of the ImageView's width to the Bitmap's width times the Bitmap's height. Division is done last because Integer division first would have resulted in an answer of 0.
With the scaled height I can determine the amount of blank space on either side of the scaled Bitmap by using:
To determine the x and y coordinates of where the overlay should go on the scaled Bitmap I have to use the original coordinates and these calculated numbers. The x coordinate in this example is easy. Since the scaled width is the width of the ImageView, I just have to multiply the ratio of the ImageView's width to the Bitmap's width with the original x coordinate.
The y coordinate is a bit trickier. Take the ratio of the Bitmap's scaled height to the Bitmap's original height. Multiply that value with the original y coordinate. Then add the blank area buffer.
This works for what I need. If the height is greater than the width, just reverse the width and height variables.
我是这样做的:
如果您想获取原始大小,可以使用 Drawable 的 getIntrinsicWidth() 或 height 方法。
编辑:好的,如果您尝试在 onCreate 运行时访问这些边界,则必须等待并检索它们,直到布局传递之后。虽然我不知道这是最好的方法,但这就是我能够实现的方法。基本上,向 ImageView 添加一个侦听器,并在将其绘制到屏幕之前获取尺寸。如果您需要对检索到的尺寸进行任何布局更改,您应该在 onPreDraw() 中进行。
Here's how I do it:
You can use Drawable's
getIntrinsicWidth()
or height method if you want to get the original size.EDIT: Okay, if you're trying to access these bounds at the time onCreate runs, you'll have to wait and retrieve them till after the layout pass. While I don't know that this is the best way, this is how I've been able to accomplish it. Basically, add a listener to the ImageView, and get your dimensions just before it's drawn to the screen. If you need to make any layout changes from the dimensions you retrieve, you should do it within onPreDraw().
尝试此代码:
否则您可以通过将此视图设为自定义视图来使用
onSizeChange()
。Try this code:
Otherwise you can use
onSizeChange()
by making this view a custom one.