误区:设置了 Bitmap.Config.RGB_565 图片就会减少占用内存

如果图片原本是RGB_565格式,确实比ARGB_8888格式少一半的内存 例如:

val options = BitmapFactory.Options()
        //设置图片格式为RGB_565
options.inPreferredConfig = Bitmap.Config.RGB_565
        //获取图片流
val ios = assets.open("anitque.png")
val optionsBitmap = BitmapFactory.decodeStream(ios, null, options)

相信大家都认为optionsBitmap色彩格式为RGB_565

val inputStream = assets.open("anitque.png")
val bitmap1 = BitmapFactory.decodeStream(inputStream)
Log.i("BITMAP", "默认格式 " + bitmap1.config.name)
val options = BitmapFactory.Options()
options.inPreferredConfig = Bitmap.Config.RGB_565
val ios = assets.open("anitque.png")
val optionsBitmap = BitmapFactory.decodeStream(ios, null, options)
Log.i("BITMAP", "转换后格式 " + optionsBitmap.config.name)

结果:

 默认格式 ARGB_8888
转换后格式 ARGB_8888

使用Glide

@GlideModule
class ConfGlideModule : AppGlideModule() {

    override fun applyOptions(context: Context, builder: GlideBuilder) {
        super.applyOptions(context, builder)
        //设置一个全局的配置
builder.setDefaultRequestOptions(RequestOptions().format(DecodeFormat.PREFER_RGB_565))
    }

    override fun isManifestParsingEnabled(): Boolean {
        return false
    }
}

Glide加载图片

   GlideApp.with(default_iv).asBitmap().load(R.drawable.emerald)
            .into(object : SimpleTarget<Bitmap>() {
                override fun onResourceReady(
                    resource: Bitmap,
                    transition: Transition<in Bitmap>?
                ) {

                    Log.i("BITMAP", "Glide格式 ${resource.config.name}")
                }

            })

结果:

Glide格式 ARGB_8888

 

通过查看源码得知原因:

 /**
         * If this is non-null, the decoder will try to decode into this
         * internal configuration. If it is null, or the request cannot be met,
         * the decoder will try to pick the best matching config based on the
         * system's screen depth, and characteristics of the original image such
         * as if it has per-pixel alpha (requiring a config that also does).
         * 
         * Image are loaded with the {@link Bitmap.Config#ARGB_8888} config by
         * default.
         */
        public Bitmap.Config inPreferredConfig = Bitmap.Config.ARGB_8888;

翻译:如果非零,则解码器将尝试解码为该内部配置。 如果为null,或无法满足请求,则解码器将尝试根据系统的屏幕深度和原始图像的特征(例如,具有每像素的alpha值)选择最佳匹配的配置(要求配置也这样做)。
默认情况下,图像会使用{@link Bitmap.Config#ARGB_8888}配置加载。

由此可见,改配置并不是总能起到效果

解决方案:

    public static Bitmap convert(Bitmap bitmap, Bitmap.Config config) {
        Bitmap convertedBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), config);
//创建一个画布
        Canvas canvas = new Canvas(convertedBitmap);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        bitmap.recycle();
        return convertedBitmap;
    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值