ImageView bitmap scale dimensions(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 位图缩放尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ImageView 位图缩放尺寸
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01