ImageView 位图缩放尺寸 [英] ImageView bitmap scale dimensions

查看:20
本文介绍了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?

推荐答案

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

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.

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

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

我确定了哪一个更大,这样我就可以正确地确定计算的基础是哪一个.对于我当前的示例,宽度更大.由于宽度被缩小到 ImageView 的宽度,我必须找到缩小的高度.我只是将 ImageView 的宽度与 Bitmap 的宽度之比乘以 Bitmap 的高度.除法是最后完成的,因为先进行整数除法会导致答案为 0.

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;

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

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;

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

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.

这篇关于ImageView 位图缩放尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆