ImageView的位图级尺寸 [英] ImageView bitmap scale dimensions

查看:135
本文介绍了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的的高度和宽度都是硬codeD在335 xml文件。

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的宽度,以位图的宽度乘以位图的高度的比例。司做最后因为整数除法首先将导致为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宽度与位图的宽度的比例与原来的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天全站免登陆