高效加载大型位图

注意:如需加载界面图片,请使用图片加载库(例如 Coil 或 Glide),而不是手动进行位图解码。如需有关位图内存架构、自适应缓存大小调整和生命周期逐出的端到端指导,请参阅优化位图内存。

图片有各种形状和大小。在很多情况下,它们的大小超过了典型应用界面的要求。例如,系统“图库”应用会显示使用 Android 设备的相机拍摄的照片,这些照片的分辨率通常远高于设备的屏幕密度。

鉴于您使用的内存有限,理想情况下您只希望在内存中加载较低分辨率的版本。分辨率较低的版本应与显示该版本的界面组件的大小相匹配。分辨率更高的图片不会带来任何明显的好处,但仍会占用宝贵的内存,并且会因为额外的动态缩放而产生额外的性能开销。

本指南介绍了如何手动解码经过缩放的位图。它涵盖了 Android 9(API 级别 28)及更高版本上的 ImageDecoder、适用于大型可缩放图片的 BitmapRegionDecoder,以及 Android 8.1(API 级别 27)及更低版本上的 BitmapFactory。

使用 ImageDecoder 解码缩放后的硬件位图

在 Android 9(API 级别 28)及更高版本中,请使用 ImageDecoder,而不是双通道 BitmapFactory 解码。传递 OnHeaderDecodedListener 以读取编码的图片大小,并在目标边界内缩放输出。设置 ALLOCATOR_HARDWARE,将不可变的界面位图放置在显存中。硬件位图仍会计入 Android Vitals 位图内存指标,因此请将其大小调整为容器边界:

val source = ImageDecoder.createSource(resources, R.drawable.myimage)
val bitmap = ImageDecoder.decodeBitmap(source) { decoder, info, _ ->
    decoder.allocator = ImageDecoder.ALLOCATOR_HARDWARE
    val scale = minOf(
        reqWidth.toFloat() / info.size.width,
        reqHeight.toFloat() / info.size.height,
        1f,
    )
    if (scale < 1f) {
        decoder.setTargetSize(
            (info.size.width * scale).roundToInt().coerceAtLeast(1),
            (info.size.height * scale).roundToInt().coerceAtLeast(1),
        )
    }
}

使用 BitmapRegionDecoder 解码可见图块

对于可缩放的大型图片(例如地图、楼层平面图或商品预览),请使用 BitmapRegionDecoder。调用 decodeRegion(在缩小状态下使用 BitmapFactory.Options.inSampleSize),以便仅解码可见视口 Rect,而不是 RAM 中的全分辨率图片。

读取位图尺寸和类型 (BitmapFactory)

BitmapFactory 类提供了几种用于从各种来源创建 Bitmap 的解码方法(decodeByteArray()、decodeFile()、decodeResource() 等)。根据您的图片数据源选择最合适的解码方法。这些方法尝试为构造的位图分配内存,因此很容易导致 OutOfMemory 异常。每种类型的解码方法都有额外的签名,允许您通过 BitmapFactory.Options 类指定解码选项。在解码时将 inJustDecodeBounds 属性设置为 true 可避免内存分配,为位图对象返回 null,但设置 outWidth、outHeight 和 outMimeType。此方法可让您在构造位图并为其分配内存之前读取图片数据的尺寸和类型。

Kotlin

val options = BitmapFactory.Options().apply {
    inJustDecodeBounds = true
}
BitmapFactory.decodeResource(resources, R.id.myimage, options)
val imageHeight: Int = options.outHeight
val imageWidth: Int = options.outWidth
val imageType: String = options.outMimeType

Java

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

为避免出现 java.lang.OutOfMemory 异常,请先检查位图的尺寸,然后再对其进行解码,除非您绝对信任该来源可为您提供大小可预测的图片数据,以轻松适应可用的内存。

将按比例缩小的版本加载到内存中

既然图片尺寸已知,便可用于确定应将完整图片加载到内存中,还是应改为加载下采样版本。以下是需要考虑的一些因素:

  • 在内存中加载完整图片的估计内存使用量。
  • 根据应用的任何其他内存要求,您愿意分配用于加载此图片的内存量。
  • 图片要载入到的目标 ImageView 或界面组件的尺寸。
  • 当前设备的屏幕大小和密度。

例如,如果 1024x768 像素的图片最终会在 ImageView 中显示为 128x96 像素缩略图,则不值得将其加载到内存中。

如需对图片进行下采样,将其转换为较小的位图,请在 BitmapFactory.Options 对象中将 inSampleSize 设置为 2 的幂。例如,分辨率为 2048x1536 且以 4 作为 inSampleSize 进行解码的图片会生成大约 512x384 的位图。将此图片加载到内存中需使用 0.75MB,而不是完整图片所需的 12MB(假设位图配置为 ARGB_8888)。下面的方法用于计算样本大小值,即基于目标宽度和高度的 2 的幂:

Kotlin

fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
    // Raw height and width of image
    val (height: Int, width: Int) = options.run { outHeight to outWidth }
    var inSampleSize = 1

    if (height > reqHeight || width > reqWidth) {

        val halfHeight: Int = height / 2
        val halfWidth: Int = width / 2

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) {
            inSampleSize *= 2
        }
    }

    return inSampleSize
}

Java

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) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) >= reqHeight
                && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

注意:根据 inSampleSize 文档,计算 2 的幂的原因是解码器使用的最终值将向下舍入为最接近的 2 的幂。

如需使用此方法,请先将 inJustDecodeBounds 设为 true 进行解码,传递选项,然后使用新的 inSampleSize 值并将 inJustDecodeBounds 设为 false 再次进行解码:

Kotlin

fun decodeSampledBitmapFromResource(
        res: Resources,
        resId: Int,
        reqWidth: Int,
        reqHeight: Int
): Bitmap {
    // First decode with inJustDecodeBounds=true to check dimensions
    return BitmapFactory.Options().run {
        inJustDecodeBounds = true
        BitmapFactory.decodeResource(res, resId, this)

        // Calculate inSampleSize
        inSampleSize = calculateInSampleSize(this, reqWidth, reqHeight)

        // Decode bitmap with inSampleSize set
        inJustDecodeBounds = false

        BitmapFactory.decodeResource(res, resId, this)
    }
}

Java

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);
}

采用此方法,您可以轻松地将任意大尺寸的位图加载到显示 100x100 像素缩略图的 ImageView 中,如以下示例代码所示:

Kotlin

imageView.setImageBitmap(
        decodeSampledBitmapFromResource(resources, R.id.myimage, 100, 100)
)

Java

imageView.setImageBitmap(
    decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));

您可以按照类似的流程来解码其他来源的位图,只需根据需要替换相应的 BitmapFactory.decode* 方法即可。