ImageView 位图比例尺寸

发布于 2024-10-17 22:41:01 字数 76 浏览 1 评论 0原文

我有一个比我放入的 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 技术交流群。

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

发布评论

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

评论(3

他不在意 2024-10-24 22:41:01

好的。我可能应该更清楚。在将缩放位图绘制到屏幕上之前,我需要缩放位图的高度和宽度,以便我可以在正确的位置绘制一些叠加层。我知道原始位图中叠加层的位置,但不知道缩放后的位置。我想出了一些简单的公式来计算它们应该在缩放位图上的位置。

我会解释一下我做了什么,以防有一天其他人可能需要这个。

我得到了位图的原始宽度和高度。 ImageView 的高度和宽度在 xml 文件中硬编码为 335。

int bitmap_width = bmp.getWidth();
int bitmap_height = bmp.getHeight();

我确定了哪一个更大,以便我可以正确地找出计算的基础。对于我当前的示例,宽度较大。由于宽度缩小到 ImageView 的宽度,我必须找到缩小后的高度。我只是将 ImageView 的宽度与位图的宽度之比乘以位图的高度。除法是最后完成的,因为首先进行整数除法会得到 0 的结果。

int scaled_height = image_view_width * bitmap_height / bitmap_width;

通过缩放的高度,我可以通过使用以下方法确定缩放位图两侧的空白空间量:

int blank_space_buffer = (image_view_height - scaled_height) / 2;

确定叠加位置的 x 和 y 坐标应该在缩放位图上我必须使用原始坐标和这些计算出的数字。本例中的 x 坐标很简单。由于缩放后的宽度是 ImageView 的宽度,因此我只需将 ImageView 的宽度与 Bitmap 的宽度之比乘以原始 x 坐标即可。

int new_x_coord = image_view_width * start_x / bitmap_width;

y 坐标有点棘手。取位图缩放后的高度与位图原始高度的比率。将该值与原始 y 坐标相乘。然后添加空白区域缓冲区。

int new_y_coord = scaled_height * start_y / bitmap_height + blank_space_buffer;

这适合我的需要。如果高度大于宽度,只需反转宽度和高度变量即可。

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.

int bitmap_width = bmp.getWidth();
int bitmap_height = bmp.getHeight();

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.

int scaled_height = image_view_width * bitmap_height / bitmap_width;

With the scaled height I can determine the amount of blank space on either side of the scaled Bitmap by using:

int blank_space_buffer = (image_view_height - scaled_height) / 2;

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.

int new_x_coord = image_view_width * start_x / bitmap_width;

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.

int new_y_coord = scaled_height * start_y / bitmap_height + blank_space_buffer;

This works for what I need. If the height is greater than the width, just reverse the width and height variables.

熟人话多 2024-10-24 22:41:01

我是这样做的:

ImageView iv = (ImageView)findViewById(R.id.imageview);
Rect bounds = iv.getDrawable().getBounds();
int scaledHeight = bounds.height();
int scaledWidth = bounds.width();

如果您想获取原始大小,可以使用 Drawable 的 getIntrinsicWidth() 或 height 方法。

编辑:好的,如果您尝试在 onCreate 运行时访问这些边界,则必须等待并检索它们,直到布局传递之后。虽然我不知道这是最好的方法,但这就是我能够实现的方法。基本上,向 ImageView 添加一个侦听器,并在将其绘制到屏幕之前获取尺寸。如果您需要对检索到的尺寸进行任何布局更改,您应该在 onPreDraw() 中进行。

ImageView iv = (ImageView)findViewById(R.id.imageview);
int scaledHeight, scaledWidth;
iv.getViewTreeObserver().addOnPreDrawListener(
    new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
        Rect rect = iv.getDrawable().getBounds();
        scaledHeight = rect.height();
        scaledWidth = rect.width();
        iv.getViewTreeObserver().removeOnPreDrawListener(this);
        return true;
    }
});

Here's how I do it:

ImageView iv = (ImageView)findViewById(R.id.imageview);
Rect bounds = iv.getDrawable().getBounds();
int scaledHeight = bounds.height();
int scaledWidth = bounds.width();

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

ImageView iv = (ImageView)findViewById(R.id.imageview);
int scaledHeight, scaledWidth;
iv.getViewTreeObserver().addOnPreDrawListener(
    new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
        Rect rect = iv.getDrawable().getBounds();
        scaledHeight = rect.height();
        scaledWidth = rect.width();
        iv.getViewTreeObserver().removeOnPreDrawListener(this);
        return true;
    }
});
零崎曲识 2024-10-24 22:41:01

尝试此代码:

final ImageView iv = (ImageView) findViewById(R.id.myImageView); 
ViewTreeObserver vto = iv.getViewTreeObserver();  
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
    @Override  
    public void onGlobalLayout() {  
        Toast.makeText(MyActivity.this, iv.getWidth() + " x " + iv.getHeight(), Toast.LENGTH_LONG).show(); 
        iv.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
    }  
});  

否则您可以通过将此视图设为自定义视图来使用 onSizeChange()

Try this code:

final ImageView iv = (ImageView) findViewById(R.id.myImageView); 
ViewTreeObserver vto = iv.getViewTreeObserver();  
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
    @Override  
    public void onGlobalLayout() {  
        Toast.makeText(MyActivity.this, iv.getWidth() + " x " + iv.getHeight(), Toast.LENGTH_LONG).show(); 
        iv.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
    }  
});  

Otherwise you can use onSizeChange() by making this view a custom one.

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