use*_*389 10 android bitmapfactory
我正在'developer.android.com'上缩小我的位图文件,我发现了一件我不理解的事情.所以我感谢你给我一点帮助.
这是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;
}
Run Code Online (Sandbox Code Playgroud)
在if语句中,当"if(width> height)"为什么会计算"(float)height /(float)reqHeight"?
例如,width = 600,height = 800,reqWidth = 100,reqHeight = 100.
在这种情况下,inSampleSize将为6,并且计算的尺寸为width = 100,height = 133.身高仍然高于reqHeight ..
那么,有人可以解释一下这个吗?抱歉复杂的解释,但我希望有人给我一个想法.:)
我只能说,他们的逻辑看起来是错误的:(无论如何,这个方法相当简单,所以你用正确的条件重新实现它应该不是什么大问题!我的意思是,当你看一下decodeSampledBitmapFromResource时,它只是想缩小位图以使其适合所需的边界,所以这一定是一个错误。
编辑:: 这看起来更糟糕,因为它在某些情况下不起作用。假设您的宽度 = 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;
}
Run Code Online (Sandbox Code Playgroud)
但这看起来他们的代码有太多问题,让我相信我正确理解了它的要点!
| 归档时间: |
|
| 查看次数: |
5191 次 |
| 最近记录: |