在我的应用程序中,我以这种方式将图像加载为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通道.
原文地址:https://www.jb51.cc/android/315268.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。