Android 大图显示策略优化显示(一)

在 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

根据你指定的目标尺寸(例如 reqWidthreqHeight)和原始尺寸,计算出一个合适的 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

💡‌注意事项

  • 获取到的 inSampleSizeint 类型,代表的是宽高的缩放因子。例如,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
            )
        }
相关推荐
CoovallyAIHub18 小时前
中文语音识别该用谁?6 个开源模型 + 2 个配套工具,一文理清
深度学习·算法·计算机视觉
会编程的土豆18 小时前
【数据结构与算法】 二叉树做题
开发语言·数据结构·c++·算法
木禾ali0th18 小时前
告别大模型“裸奔”:开源项目 ClawVault 架构与核心能力解析
算法·安全
Storynone18 小时前
【Day28】LeetCode:509. 斐波那契数,70. 爬楼梯,746. 使用最小花费爬楼梯
python·算法·leetcode
峥嵘life19 小时前
Android16 EDLA【CTS】CtsConnectivityMultiDevicesTestCases存在fail项
android·学习
大傻^19 小时前
SpringAI2.0 Null Safety 实战:JSpecify 注解体系与 Kotlin 互操作
android·开发语言·人工智能·kotlin·springai
游戏开发爱好者819 小时前
React Native iOS 代码如何加密,JS 打包 和 IPA 混淆
android·javascript·react native·ios·小程序·uni-app·iphone
DeepModel19 小时前
【统计检验】方差分析(ANOVA)
算法
sz-lcw19 小时前
HOG特征向量计算方法
人工智能·python·算法
闻缺陷则喜何志丹19 小时前
【博弈论】P8144 [JRKSJ R4] BBWWBB|普及+
c++·算法·洛谷·博弈论