gpt4 book ai didi

android - calculateInSampleSize,为什么 Math.round 在 width > height 时处理 height(height/reqHeight)?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:04:29 28 4
gpt4 key购买 nike

我在“developer.android.com”上查看以缩小我的位图文件,但我发现了一件事我不明白。所以我很感激你能给我一点帮助。

这是一个 snippet来自 developer.android.com

public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}

在 if 语句中,当“if(width > height)”时,为什么要计算“(float)height/(float)reqHeight”?

例如,宽度=600,高度=800,reqWidth=100,reqHeight=100。

在这种情况下,inSampleSize 将为 6,计算出的尺寸为 width=100,height=133。高度仍然高于 reqHeight..

所以,有人可以解释一下吗?抱歉复杂的解释但是我希望有人给我一个想法。 :)

最佳答案

我只能说他们的逻辑看起来是错误的 :( 不管怎样,这个方法相当简单,所以用正确的条件重新实现它应该不是什么大问题!我的意思是当你看一下decodeSampledBitmapFromResource,它只是想减少 Bitmap 以使其适合所需的边界,所以这一定是一个错误。

编辑::对我来说,这看起来更糟,因为它在某些情况下不起作用。假设您有宽度 = 200 和高度 = 600。您将最大边界设置为宽度 = 100 和高度 = 500。您的高度 > 宽度,但如果您希望它们都适合,则返回结果 inSampleSize 必须为 200/100而不是 600/500。所以基本上如果你重新实现这个方法,我会这样做:

public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;

int stretch_width = Math.round((float)width / (float)reqWidth);
int stretch_height = Math.round((float)height / (float)reqHeight);

if (stretch_width <= stretch_height)
return stretch_height;
else
return stretch_width;
}

但是他们的代码看起来有太多问题,我无法相信我正确理解了它的意义!

关于android - calculateInSampleSize,为什么 Math.round 在 width > height 时处理 height(height/reqHeight)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13696166/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com