android – “Bitmap.createScaledBitmap”将32位图像转换为24位吗?

在我的应用程序中,我以这种方式将图像加载为32位(ARGB_8888):
Bitmap.Config mBitmapConfig;
 mBitmapConfig = Bitmap.Config.ARGB_8888; 
 BitmapFactory.Options options = new BitmapFactory.Options();
 options.inPreferredConfig = mBitmapConfig;
 mBitmap = BitmapFactory.decodeFile(SourceFileName,options);

然后缩放:

mBitmap = Bitmap.createScaledBitmap(mBitmap,iW,iH,true);

如果我用于缩放原始位图的相同宽度和高度,则它是以兆字节为单位的1/2的大小(我正在观察堆大小).
将值“ARGB_8888”更改为“RGB_565”(24位)可在缩放后提供相同的大小(兆字节).

有人可以解释这个现象,可能会给我一个建议,如何在32位色彩空间中缩放位图?
谢谢!

解决方法

我在Bitmap类的源代码中查找了createScaledBitmap方法( Link):
public static Bitmap createScaledBitmap(Bitmap src,int dstWidth,int dstHeight,boolean filter) {
    Matrix m;
    synchronized (Bitmap.class) {
        // small pool of just 1 matrix
        m = sScaleMatrix;
        sScaleMatrix = null;
    }

    if (m == null) {
        m = new Matrix();
    }

    final int width = src.getWidth();
    final int height = src.getHeight();
    final float sx = dstWidth  / (float)width;
    final float sy = dstHeight / (float)height;
    m.setScale(sx,sy);
    Bitmap b = Bitmap.createBitmap(src,width,height,m,filter);

    synchronized (Bitmap.class) {
        // do we need to check for null? why not just assign everytime?
        if (sScaleMatrix == null) {
            sScaleMatrix = m;
        }
    }

    return b;
}

由于在方法体中进行了此检查,对createBitmap()的调用应该返回未更改的源位图:

if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() &&
            height == source.getHeight() && (m == null || m.isIdentity())) {
        return source;
    }

看一下这似乎是你的原始位图被返回,但是,如果你的位图恰好是可变的,你有效地跳过这个检查并最终在这里

if (m == null || m.isIdentity()) {
        bitmap = createBitmap(neww,newh,source.hasAlpha() ? Config.ARGB_8888 : Config.RGB_565);
        paint = null;   // not needed
    }

由于您没有执行任何缩放,您的矩阵将是单位矩阵,并且条件得到满足.如您所见,创建的位图依赖于源位图中的alpha.如果不存在alpha,则最终得到RGB_565格式的结果位图,而不是ARGB_8888.

因此,要缩放和保留32位格式,您的位图应该是不可变的或使用Alpha通道.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...