解码bitmapfactory两次有什么用?
What is the use of decoding bitmapfactory twice?
我在开发者网站上的高效加载大型位图教程中看到了这个。
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
**BitmapFactory.decodeResource(res, resId, options);**
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
我的问题是第一次解码资源有什么意义,你可以设置inSampleSize然后解码它。
如果你在第一次解码时注意到他设置了
options.inJustDecodeBounds = true;
来自 api 文档 "If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels."
使用此选项进行解码时,不会加载位图。
这是下载图片时的常用策略。
由于您几乎不想下载比显示分辨率更高的图像,而且 Android 在内存管理方面相当困难,因此该系统允许您首先评估图像的大小图像将是,当你实际下载时,你可以控制你想要多少下采样。
下采样,简单地说,就是要跳过多少像素。例如,1 的下采样不会减少。然而,2 的下采样将在水平和垂直方向上每隔一个像素跳过一次,从而导致位图的宽度和高度分别为一半和四分之一的内存。
如果您查看这段代码:
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
**BitmapFactory.decodeResource(res, resId, options);**
这里发生的事情是在调用“decodeResource
”时,您正在传递一个带有 inJustDecodeBounds = true
的 Options
对象。这是告诉 BitmapFactory
不要实际加载图像像素,而只是解码图像的边界——这是一种更便宜的操作。当您这样做时,BitmapFactory
的结果是 null
,但是 Options
(outWidth, outHeight
) 的参数将具有描述图像宽度/高度的有效值。有了这个,您可以计算出您想要的样本大小,并最终下载实际图像,但具有最适合您的应用程序的大小。
我在开发者网站上的高效加载大型位图教程中看到了这个。
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
**BitmapFactory.decodeResource(res, resId, options);**
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
我的问题是第一次解码资源有什么意义,你可以设置inSampleSize然后解码它。
如果你在第一次解码时注意到他设置了
options.inJustDecodeBounds = true;
来自 api 文档 "If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels."
使用此选项进行解码时,不会加载位图。
这是下载图片时的常用策略。
由于您几乎不想下载比显示分辨率更高的图像,而且 Android 在内存管理方面相当困难,因此该系统允许您首先评估图像的大小图像将是,当你实际下载时,你可以控制你想要多少下采样。
下采样,简单地说,就是要跳过多少像素。例如,1 的下采样不会减少。然而,2 的下采样将在水平和垂直方向上每隔一个像素跳过一次,从而导致位图的宽度和高度分别为一半和四分之一的内存。
如果您查看这段代码:
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
**BitmapFactory.decodeResource(res, resId, options);**
这里发生的事情是在调用“decodeResource
”时,您正在传递一个带有 inJustDecodeBounds = true
的 Options
对象。这是告诉 BitmapFactory
不要实际加载图像像素,而只是解码图像的边界——这是一种更便宜的操作。当您这样做时,BitmapFactory
的结果是 null
,但是 Options
(outWidth, outHeight
) 的参数将具有描述图像宽度/高度的有效值。有了这个,您可以计算出您想要的样本大小,并最终下载实际图像,但具有最适合您的应用程序的大小。