在 Android 开发中,使用 BitmapFactory.Options 分两步操作是高效加载和缩放大图的常用方法。首先获取图片的实际宽高,然后根据目标尺寸计算出合适的采样率 inSampleSize,最终只将缩放后的图片加载到内存中以避免内存溢出。
📌实现步骤与核心方法
1. 预读取图片边界信息
通过设置 options.inJustDecodeBounds = true,可以解析出图片的原始宽高,而无需将整个图片加载到内存中。
val options = BitmapFactory.Options().apply {
inJustDecodeBounds = true // 开启边界模式
}
// 解码资源,现在只会填充 options.outWidth 和 options.outHeight
BitmapFactory.decodeResource(resources, R.drawable.your_image, options)
val imageWidth = options.outWidth
val imageHeight = options.outHeight
2. 计算采样率 inSampleSize
根据你指定的目标尺寸(例如 reqWidth 和 reqHeight)和原始尺寸,计算出一个合适的 inSampleSize。采样率必须是 2 的幂(如 1, 2, 4, 8...),且数值越大,最终图片越小。
fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
// 图片的实际高度和宽度
val (height: Int, width: Int) = options.run { outHeight to outWidth }
var inSampleSize = 1
// 当实际尺寸大于目标尺寸时,通过乘以2来尝试更大的采样率
if (height > reqHeight || width > reqWidth) {
val halfHeight: Int = height / 2
val halfWidth: Int = width / 2
while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2
}
}
return inSampleSize
}
val reqWidth = // ... 你的目标宽度(像素)
val reqHeight = // ... 你的目标高度(像素)
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight)
3. 加载最终位图
将 inJustDecodeBounds 设置回 false 并应用计算出的 inSampleSize,再次解码资源以获取缩放后的位图。
options.inJustDecodeBounds = false // 关闭边界模式,开始解码图片
val bitmap: Bitmap? = BitmapFactory.decodeResource(resources, R.drawable.your_image, options)
// 将 bitmap 设置给 ImageView
💡注意事项
- 获取到的
inSampleSize是int类型,代表的是宽高的缩放因子。例如,inSampleSize为 2 时,生成的位图宽度为原始宽度的 1/2,高度为原始高度的 1/2,总像素数为原来的 1/4。 - 这种采样缩放是向下采样 ,即只能缩小不能放大。如果需要放大、精确裁剪或有其他复杂需求,需要在获取位图后使用
Bitmap.createScaledBitmap等方法进行进一步处理

以上是加载大图片压缩图片的步骤。这样实现的话,图片并不是最佳的显示效果。毕竟 要是原图的 1倍,2倍,4倍,不是最佳的显示效果。
只要知道,控件如ImageView 的最大显示宽高。把控件计算成符合原图片宽高比例的效果即可。再把图片赋值到ImageView上即可。并且scaleType 要是 fitXY的属性。
<ImageView
android:id="@+id/igvTarget"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/windmill"></ImageView>
具体实现代码可以采用以下方式:
val options = BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, R.drawable.windmill, options)
val maxHeight = binding.layoutLine.layoutParams.height
val maxWidth = binding.layoutLine.layoutParams.width
val scaleResult =
ImageUtils.calculateScaleParameters(
maxWidth,
maxHeight,
options.outWidth,
options.outHeight
)
binding.igvTarget.layoutParams.height = scaleResult.newHeight
binding.igvTarget.layoutParams.width = scaleResult.newWidth
// binding.igvTarget.setImageResource(R.drawable.windmill)
Glide.with(this@PhotoGuidanceActivity).load(R.drawable.windmill).into(binding.igvTarget)
fun calculateScaleParameters(
maxWidth: Int,
maxHeight: Int,
bitmapWidth: Int,
bitmapHeight: Int
): ScaleResult {
// 如果图片高度低于要求的高度,直接展示图片
if (bitmapHeight <= maxHeight && bitmapWidth <= maxWidth) {
return ScaleResult(
newWidth = bitmapWidth,
newHeight = bitmapHeight,
scaleX = 1.0f,
scaleY = 1.0f,
shouldScale = false
)
}
// 计算缩放比例
val scaleX = maxWidth.toFloat() / bitmapWidth
val scaleY = maxHeight.toFloat() / bitmapHeight
// 选择较小的缩放比例以保持图片比例
val scale = minOf(scaleX, scaleY)
// 计算缩放后的尺寸
val newWidth = (bitmapWidth * scale).toInt()
val newHeight = (bitmapHeight * scale).toInt()
return ScaleResult(
newWidth = newWidth,
newHeight = newHeight,
scaleX = scale,
scaleY = scale,
shouldScale = true
)
}