Android 官方推荐优化Bitmap中data options属性列表.inBitmap属性是否真的有用

在Android开发过程中,Bitmap往往会给开发者带来一些困扰,因为对Bitmap操作不慎,就容易造成OOM(Java.lang.OutofMemoryError - 内存溢出),本篇博客,我们将一起探讨Bitmap的性能优化。
为什么Bitmap会导致OOM?
1.每个机型在编译ROM时都设置了一个应用堆内存VM值上限dalvik.vm.heapgrowthlimit,用来限定每个应用可用的最大内存,超出这个最大值将会报OOM。这个阀值,一般根据手机屏幕dpi大小递增,dpi越小的手机,每个应用可用最大内存就越低。所以当加载图片的数量很多时,就很容易超过这个阀值,造成OOM。
2.图片分辨率越高,消耗的内存越大,当加载高分辨率图片的时候,将会非常占用内存,一旦处理不当就会OOM。例如,一张分辨率为:的图片。如果Bitmap使用 ARGB_8888 32位来平铺显示的话,占用的内存是个字节,占用将近8M内存,可想而知,如果不对图片进行处理的话,就会OOM。
3.在使用ListView, GridView等这些大量加载view的组件时,如果没有合理的处理缓存,大量加载Bitmap的时候,也将容易引发OOM
Bitmap基础知识
一张图片Bitmap所占用的内存 = 图片长度 x 图片宽度 x 一个像素点占用的字节数
而Bitmap.Config,正是指定单位像素占用的字节数的重要参数。
其中,A代表透明度;R代表红色;G代表绿色;B代表蓝色。
表示8位Alpha位图,即A=8,一个像素点占用1个字节,它没有颜色,只有透明度
表示16位ARGB位图,即A=4,R=4,G=4,B=4,一个像素点占4+4+4+4=16位,2个字节
表示32位ARGB位图,即A=8,R=8,G=8,B=8,一个像素点占8+8+8+8=32位,4个字节
表示16位RGB位图,即R=5,G=6,B=5,它没有透明度,一个像素点占5+6+5=16位,2个字节
一张图片Bitmap所占用的内存 = 图片长度 x 图片宽度 x 一个像素点占用的字节数
根据以上的算法,可以计算出图片占用的内存,以100*100像素的图片为例
BitmapFactory解析Bitmap的原理
BitmapFactory提供的解析Bitmap的静态工厂方法有以下五种:
Bitmap decodeFile(...)
Bitmap decodeResource(...)
Bitmap decodeByteArray(...)
Bitmap decodeStream(...)
Bitmap decodeFileDescriptor(...)
其中常用的三个:decodeFile、decodeResource、decodeStream。
decodeFile和decodeResource其实最终都是调用decodeStream方法来解析Bitmap
decodeFile方法代码:
public static Bitmap decodeFile(String pathName, Options opts) {
Bitmap bm = null;
InputStream stream = null;
stream = new FileInputStream(pathName);
bm = decodeStream(stream, null, opts);
} catch (Exception e) {
Log.e("BitmapFactory", "Unable to decode stream: " + e);
} finally {
if (stream != null) {
stream.close();
} catch (IOException e) {
decodeResource方法的代码:
public static Bitmap decodeResource(Resources res, int id, Options opts) {
Bitmap bm = null;
InputStream is = null;
final TypedValue value = new TypedValue();
is = res.openRawResource(id, value);
bm = decodeResourceStream(res, value, is, null, opts);
} catch (Exception e) {
} finally {
if (is != null) is.close();
} catch (IOException e) {
if (bm == null && opts != null && opts.inBitmap != null) {
throw new IllegalArgumentException("Problem decoding into existing bitmap");
decodeStream的逻辑如下:
public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts) {
if (is == null) {
return null;
Bitmap bm = null;
Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "decodeBitmap");
if (is instanceof AssetManager.AssetInputStream) {
final long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
bm = nativeDecodeAsset(asset, outPadding, opts);
bm = decodeStreamInternal(is, outPadding, opts);
if (bm == null && opts != null && opts.inBitmap != null) {
throw new IllegalArgumentException("Problem decoding into existing bitmap");
setDensityFromOptions(bm, opts);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
private static Bitmap decodeStreamInternal(InputStream is, Rect outPadding, Options opts) {
byte [] tempStorage = null;
if (opts != null) tempStorage = opts.inTempS
if (tempStorage == null) tempStorage = new byte[DECODE_BUFFER_SIZE];
return nativeDecodeStream(is, tempStorage, outPadding, opts);
从上面的代码可以看出,decodeStream的代码最终会调用以下两个native方法之一
nativeDecodeAsset()
nativeDecodeStream()
这两个native方法只是对应decodeFile和decodeResource、decodeStream来解析的,像decodeByteArray、decodeFileDescriptor也有专门的native方法负责解析Bitmap。
decodeFile、decodeResource的区别在于他们方法的调用路径不同:
decodeFile-&decodeStream
decodeResource-&decodeResourceStream-&decodeStream
decodeResource在解析时多调用了一个decodeResourceStream方法,而这个decodeResourceStream方法代码如下:
public static Bitmap decodeResourceStream(Resources res, TypedValue value,
InputStream is, Rect pad, Options opts) {
if (opts == null) {
opts = new Options();
if (opts.inDensity == 0 && value != null) {
final int density = value.
if (density == TypedValue.DENSITY_DEFAULT) {
opts.inDensity = DisplayMetrics.DENSITY_DEFAULT;
} else if (density != TypedValue.DENSITY_NONE) {
opts.inDensity =
if (opts.inTargetDensity == 0 && res != null) {
opts.inTargetDensity = res.getDisplayMetrics().densityD
return decodeStream(is, pad, opts);
其中对Options进行处理了,在得到opts.inDensity属性的前提下,如果我们没有对该属性设定值,那么将opts.inDensity = DisplayMetrics.DENSITY_DEFAULT;赋定这个默认的Density值,这个默认值为160,为标准的dpi比例,即在Density=160的设备上1dp=1px,这个方法中还有这么一行
opts.inTargetDensity = res.getDisplayMetrics().densityD
对opts.inTargetDensity进行了赋值,该值为当前设备的densityDpi值,所以说在decodeResourceStream方法中主要做了两件事:
1.对opts.inDensity赋值,没有则赋默认值160
2.对opts.inTargetDensity赋值,没有则赋当前设备的densityDpi值
之后参数将传入decodeStream方法,该方法中在调用native方法进行解析Bitmap后会调用这个方法setDensityFromOptions(bm, opts);:
private static void setDensityFromOptions(Bitmap outputBitmap, Options opts) {
if (outputBitmap == null || opts == null) return;
final int density = opts.inD
if (density != 0) {
outputBitmap.setDensity(density);
final int targetDensity = opts.inTargetD
if (targetDensity == 0 || density == targetDensity || density == opts.inScreenDensity) {
byte[] np = outputBitmap.getNinePatchChunk();
final boolean isNinePatch = np != null && NinePatch.isNinePatchChunk(np);
if (opts.inScaled || isNinePatch) {
outputBitmap.setDensity(targetDensity);
} else if (opts.inBitmap != null) {
outputBitmap.setDensity(Bitmap.getDefaultDensity());
主要就是把刚刚赋值过的两个属性inDensity和inTargetDensity给Bitmap进行赋值,不过并不是直接赋给Bitmap就完了,中间有个判断,当inDensity的值与inTargetDensity或与设备的屏幕Density不相等时,则将应用inTargetDensity的值,如果相等则应用inDensity的值。
所以总结来说,setDensityFromOptions方法就是把inTargetDensity的值赋给Bitmap,不过前提是opts.inScaled = true;
进过上面的分析,结论如下:
在不配置Options的情况下:
1.decodeFile、decodeStream在解析时不会对Bitmap进行一系列的屏幕适配,解析出来的将是原始大小的图
2.decodeResource在解析时会对Bitmap根据当前设备屏幕像素密度densityDpi的值进行缩放适配操作,使得解析出来的Bitmap与当前设备的分辨率匹配,达到一个最佳的显示效果,并且Bitmap的大小将比原始的大
Bitmap的优化策略
经过上面的分析,我们可以得出Bitmap优化的思路:
1、BitmapConfig的配置
2、使用decodeFile、decodeResource、decodeStream进行解析Bitmap时,配置inDensity和inTargetDensity,两者应该相等,值可以等于屏幕像素密度*0.75f
3、使用inJustDecodeBounds预判断Bitmap的大小及使用inSampleSize进行压缩
4、对Density&240的设备进行Bitmap的适配(缩放Density)
5、2.3版本inNativeAlloc的使用
6、4.4以下版本inPurgeable、inInputShareable的使用
7、Bitmap的回收
所以我们根据以上的思路,我们将Bitmap优化的策略总结为以下3种:
1.对图片质量进行压缩
2.对图片尺寸进行压缩
3.使用libjpeg.so库进行压缩
对图片质量进行压缩
public static Bitmap compressImage(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pressFormat.JPEG, 100, baos);
int options = 100;
while ( baos.toByteArray().length / 1024&50) {
baos.reset();
pressFormat.JPEG, options, baos);
options -= 10;
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
Bitmap newBitmap = BitmapFactory.decodeStream(isBm, null, null);
return newB
对图片尺寸进行压缩
* 按图片尺寸压缩 参数是bitmap
public static Bitmap compressImageFromBitmap(Bitmap bitmap, int pixelW, int pixelH) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
pressFormat.JPEG, 100, os);
if( os.toByteArray().length / 1024&512) {
os.reset();
pressFormat.JPEG, 50, os);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPreferredConfig = Bitmap.Config.RGB_565;
BitmapFactory.decodeStream(is, null, options);
options.inJustDecodeBounds = false;
options.inSampleSize = computeSampleSize(options , pixelH & pixelW ? pixelW : pixelH ,pixelW * pixelH );
is = new ByteArrayInputStream(os.toByteArray());
Bitmap newBitmap = BitmapFactory.decodeStream(is, null, options);
return newB
* 动态计算出图片的inSampleSize
* minSideLength
* maxNumOfPixels
public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
int roundedS
if (initialSize &= 8) {
roundedSize = 1;
while (roundedSize & initialSize) {
roundedSize &&= 1;
roundedSize = (initialSize + 7) / 8 * 8;
return roundedS
private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
double w = options.outW
double h = options.outH
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 :(int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound & lowerBound) {
return lowerB
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
} else if (minSideLength == -1) {
return lowerB
return upperB
使用libjpeg.so库进行压缩
除了通过设置simpleSize根据图片尺寸压缩图片和通过press方法通过压缩图片质量两种方法外,我们还可以使用libjpeg.so这个库来进行压缩。
libjpeg是广泛使用的开源JPEG图像库,Android所用的是skia的压缩算法,而Skia对libjpeg进行了的封装。
libjpeg在压缩图像时,有一个参数叫optimize_coding,关于这个参数,libjpeg.doc有如下解释:
boolean optimize_coding
TRUE causes the compressor to compute optimal Huffman coding tables
for the image. This requires an extra pass over the data and
therefore costs a good deal of space and time. The default is
FALSE, which tells the compressor to use the supplied or default
Huffman tables. In most cases optimal tables save only a few percent
of file size compared to the default tables. Note that when this is
TRUE, you need not supply Huffman tables at all, and any you do
supply will be overwritten.
如果设置optimize_coding为TRUE,将会使得压缩图像过程中基于图像数据计算哈弗曼表,由于这个计算会显著消耗空间和时间,默认值被设置为FALSE。
谷歌的Skia项目工程师们最终没有设置这个参数,optimize_coding在Skia中默认的等于了FALSE,但是问题就随之出现了,如果我们想在FALSE和TRUE时压缩成相同大小的JPEG 图片,FALSE的品质将大大逊色于TRUE的,尽管谷歌工程师没有将该值设置为true,但是我们可以自己编译libjpeg进行图片的压缩。
libjpeg的官网下载地址:
从官网下载之后,我们必须自己对其进行编译。
编译libjpeg
下载最新的源码,解压后将所有文件放到jni目录中,准备用ndk编译
1、新建config.sh,将ndk中的交叉编译工具加入其中,内容如下:
NDK=/opt/ndk/android-ndk-r10e/
PLATFORM=$NDK/platforms/android-9/arch-arm/
PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86/
CC=$PREBUILT/bin/arm-linux-androideabi-gcc
./configure --prefix=/home/linc/jpeg-9b/jni/dist --host=arm CC="$CC --sysroot=$PLATFORM"
2、执行此脚本
$ sh config.sh
checking whether to build shared libraries... no
checking whether to build static libraries... yes
config.status: creating Makefile
config.status: creating jconfig.h
首先,它生成了Makefile,我们可以直接使用此Makefile进行编译;其次,它生成了重要的头文件,jconfig.h.
但是这个Makefile是编译static库而不是共享库的。
此时,我们可以执行构建命令进行编译:
jni$ make install-libLTLIBRARIES
libtool: install: ranlib /home/linc/jpeg-9b/jni/dist/lib/libjpeg.a
3、Android.mk
使用ndk-build指令编译,需要手动编写Android.mk文件,内容如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_ARM_MODE := arm
LOCAL_SRC_FILES :=jaricom.c jcapimin.c jcapistd.c jcarith.c jccoefct.c jccolor.c \
jcdctmgr.c jchuff.c jcinit.c jcmainct.c jcmarker.c jcmaster.c \
jcomapi.c jcparam.c jcprepct.c jcsample.c jctrans.c jdapimin.c \
jdapistd.c jdarith.c jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c \
jddctmgr.c jdhuff.c jdinput.c jdmainct.c jdmarker.c jdmaster.c \
jdmerge.c jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c \
jfdctfst.c jfdctint.c jidctflt.c jidctfst.c jidctint.c jquant1.c \
jquant2.c jutils.c jmemmgr.c jmemnobs.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_CFLAGS += -O3 -fstrict-aliasing -fprefetch-loop-arrays \
-DANDROID -DANDROID_TILE_BASED_DECODE -DENABLE_ANDROID_NULL_CONVERT
LOCAL_MODULE := libjpeg
LOCAL_MODULE_TAGS := optional
# unbundled branch, built against NDK.
LOCAL_SDK_VERSION := 17
include $(BUILD_SHARED_LIBRARY)
其中LOCAL_SRC_FILES后面的源文件可以参考刚刚生成的Makefile。
在jni目录上一级使用ndk-build编译即可。
$ ndk-build
[armeabi] Compile arm
: jpeg &= jaricom.c
[armeabi] Compile arm
: jpeg &= jmemnobs.c
[armeabi] SharedLibrary
: libjpeg.so
[armeabi] Install
: libjpeg.so =& libs/armeabi/libjpeg.so
在Android项目引入编译好的libjpeg
首先把so库加载到libs中,然后将编译好的头文件拷贝到项目的jni文件夹下,就可以使用Android的具体函数了,具体使用分为如下几步:
1、将Android的bitmap解码并转换为RGB数据
2、为JPEG对象分配空间并初始化
3、指定压缩数据源
4、获取文件信息
5、为压缩设定参数,包括图像大小,颜色空间
6、开始压缩
7、压缩完毕
8、释放资源
#include &string.h&
#include &android/bitmap.h&
#include &android/log.h&
#include &jni.h&
#include &stdio.h&
#include &setjmp.h&
#include &math.h&
#include &stdint.h&
#include &time.h&
#include "jpeglib.h"
#include "cdjpeg.h"
/* Common decls for cjpeg/djpeg applications */
#include "jversion.h"
/* for version message */
#include "config.h"
#define LOG_TAG "jni"
#define LOGW(...)
__android_log_write(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#define true 1
#define false 0
typedef uint8_t BYTE;
struct my_error_mgr {
struct jpeg_error_
jmp_buf setjmp_
typedef struct my_error_mgr * my_error_
METHODDEF(void)
my_error_exit (j_common_ptr cinfo)
my_error_ptr myerr = (my_error_ptr) cinfo-&
(*cinfo-&err-&output_message) (cinfo);
error=myerr-&pub.jpeg_message_table[myerr-&pub.msg_code];
LOGE("jpeg_message_table[%d]:%s", myerr-&pub.msg_code,myerr-&pub.jpeg_message_table[myerr-&pub.msg_code]);
longjmp(myerr-&setjmp_buffer, 1);
int generateJPEG(BYTE* data, int w, int h, int quality,
const char* outfilename, jboolean optimize) {
int nComponent = 3;
struct jpeg_compress_
struct my_error_
jcs.err = jpeg_std_error(&jem.pub);
jem.pub.error_exit = my_error_
if (setjmp(jem.setjmp_buffer)) {
jpeg_create_compress(&jcs);
FILE* f = fopen(outfilename, "wb");
if (f == NULL) {
jpeg_stdio_dest(&jcs, f);
jcs.image_width =
jcs.image_height =
if (optimize) {
LOGI("optimize==ture");
LOGI("optimize==false");
jcs.arith_code = false;
jcs.input_components = nC
if (nComponent == 1)
jcs.in_color_space = JCS_GRAYSCALE;
jcs.in_color_space = JCS_RGB;
jpeg_set_defaults(&jcs);
jcs.optimize_coding =
jpeg_set_quality(&jcs, quality, true);
jpeg_start_compress(&jcs, TRUE);
JSAMPROW row_pointer[1];
row_stride = jcs.image_width * nC
while (jcs.next_scanline & jcs.image_height) {
row_pointer[0] = &data[jcs.next_scanline * row_stride];
jpeg_write_scanlines(&jcs, row_pointer, 1);
if (jcs.optimize_coding) {
LOGI("optimize==ture");
LOGI("optimize==false");
jpeg_finish_compress(&jcs);
jpeg_destroy_compress(&jcs);
fclose(f);
typedef struct {
char* jstrinTostring(JNIEnv* env, jbyteArray barr) {
char* rtn = NULL;
jsize alen = (*env)-&GetArrayLength(env, barr);
jbyte* ba = (*env)-&GetByteArrayElements(env, barr, 0);
if (alen & 0) {
rtn = (char*) malloc(alen + 1);
memcpy(rtn, ba, alen);
rtn[alen] = 0;
(*env)-&ReleaseByteArrayElements(env, barr, ba, 0);
jstring Java_net_bither_util_NativeUtil_compressBitmap(JNIEnv* env,
jobject thiz, jobject bitmapcolor, int w, int h, int quality,
jbyteArray fileNameStr, jboolean optimize) {
AndroidBitmapI
char * fileName = jstrinTostring(env, fileNameStr);
if ((ret = AndroidBitmap_getInfo(env, bitmapcolor, &infocolor)) & 0) {
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
return (*env)-&NewStringUTF(env, "0");;
if ((ret = AndroidBitmap_lockPixels(env, bitmapcolor, &pixelscolor)) & 0) {
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
BYTE r, g,
data = NULL;
data = malloc(w * h * 3);
int j = 0, i = 0;
for (i = 0; i & i++) {
for (j = 0; j & j++) {
color = *((int *) pixelscolor);
r = ((color & 0x00FF0000) && 16);
g = ((color & 0x0000FF00) && 8);
b = color & 0x000000FF;
*(data + 1) =
*(data + 2) =
data = data + 3;
pixelscolor += 4;
AndroidBitmap_unlockPixels(env, bitmapcolor);
int resultCode= generateJPEG(tmpdata, w, h, quality, fileName, optimize);
free(tmpdata);
if(resultCode==0){
jstring result=(*env)-&NewStringUTF(env, error);
error=NULL;
return (*env)-&NewStringUTF(env, "1");
新建Android.mk,生成可执行文件:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= jpeg_compress.cpp
LOCAL_MODULE:= jtest
LOCAL_LDLIBS :=-llog
LOCAL_LDLIBS += $(LOCAL_PATH)/libjpeg.so
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
LOCAL_MODULE_TAGS := debug
include $(BUILD_EXECUTABLE)
本篇博客总结了3种图片压缩的方法,大家可以根据自己的情况进行相应的使用,如有错漏,欢迎留言。
参考文章:
本文已收录于以下专栏:
相关文章推荐
本文记录一个基于FFmpeg的libswscale的示例。Libswscale里面实现了各种图像像素格式的转换,例如YUV与RGB之间的转换;以及图像大小缩放(例如640x360拉伸为...
我们知道Android系统分配给每个应用程序的内存是有限的,Bitmap作为消耗内存大户,我们对Bitmap的管理稍有不当就可能引发OutOfMemoryError,而Bitmap对象在不同的Andr...
人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..
关于Android Bitmap.setDensity 和BitmapDrawable.setTargetDensity() 的作用:
   Bitmap.setDensity:
api文档时这样...
转自:http://www.androidchina.net/4532.html?utm_source=tuicool&utm_medium=referral
1、BitmapFactory解析Bi...
libjpeg是一个被广泛使用的JPEG解码、JPEG编码和其他的JPEG功能的实现库。
说它使用广泛,是因为它跨了很多平台。比如Linux平台、JDK、Android和其他库如tess-two等等...
在编写MusicPlayer的过程中,播放歌曲时搜索歌手的图片,并动态地将图片设为当前Activity的背景。当图片大小与屏幕不适应时会导致图片被拉神效果很不好看。比如你的手机分辨率是320X480,...
作者:郭孝星
微博:郭孝星的新浪微博
博客:http://blog.csdn.net/allenwells
Github:https://githu...
位图基本概念
位图文件的定义:扩展名可以是.bmp或者.dib,它将图像定义为由点(像素)组成,每个点可以由多种色彩表示,包括2、4、8、16、24和32位色彩。
根据定义,知道了一个位图的分辨率...
内存管理是个永恒的话题!
内存溢出:就是分配的内存不足以放下数据项序列。如在一个域中输入的数据超过了它的要求就会引发数据溢出问题,多余的数据就可以作为指令在计算机上运行。就是你要求分配的...
原文地址:http://anany.me//bitmap1/
一直以来Bitmap都是开发中很棘手的问题,这个问题就是传说中的OOM(java.lang.OutofMemor...
他的最新文章
讲师: 许鹏
讲师:董付国
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)Android性能优化之Bitmap的内存优化
1、BitmapFactory解析Bitmap的原理
BitmapFactory提供的解析Bitmap的静态工厂方法有以下五种:
Bitmap decodeFile(...)
Bitmap decodeResource(...)
Bitmap decodeByteArray(...)
Bitmap decodeStream(...)
Bitmap decodeFileDescriptor(...)
其中常用的三个:decodeFile、decodeResource、decodeStream。
decodeFile和decodeResource其实最终都是调用decodeStream方法来解析Bitmap,decodeStream的内部则是调用两个native方法解析Bitmap的:
nativeDecodeAsset()
nativeDecodeStream()
这两个native方法只是对应decodeFile和decodeResource、decodeStream来解析的,像decodeByteArray、decodeFileDescriptor也有专门的native方法负责解析Bitmap。
接下来就是看看这两个方法在解析Bitmap时究竟有什么区别decodeFile、decodeResource,查看后发现它们调用路径如下:
decodeFile-&decodeStream
decodeResource-&decodeResourceStream-&decodeStream
decodeResource在解析时多调用了一个decodeResourceStream方法,而这个decodeResourceStream方法代码如下:
public static Bitmap decodeResourceStream(Resources res, TypedValue value,
InputStream is, Rect pad, Options opts) {
if (opts == null) {
opts = new Options();
if (opts.inDensity == 0 && value != null) {
final int density = value.
if (density == TypedValue.DENSITY_DEFAULT) {
opts.inDensity = DisplayMetrics.DENSITY_DEFAULT;
} else if (density != TypedValue.DENSITY_NONE) {
opts.inDensity =
if (opts.inTargetDensity == 0 && res != null) {
opts.inTargetDensity = res.getDisplayMetrics().densityD
return decodeStream(is, pad, opts);
它主要是对Options进行处理了,在得到opts.inDensity属性的前提下,如果我们没有对该属性设定值,那么将opts.inDensity = DisplayMetrics.DENSITY_DEFAULT;赋定这个默认的Density值,这个默认值为160,为标准的dpi比例,即在Density=160的设备上1dp=1px,这个方法中还有这么一行
opts.inTargetDensity = res.getDisplayMetrics().densityD
对opts.inTargetDensity进行了赋值,该值为当前设备的densityDpi值,所以说在decodeResourceStream方法中主要做了两件事:
1、对opts.inDensity赋值,没有则赋默认值160
2、对opts.inTargetDensity赋值,没有则赋当前设备的densityDpi值
之后重点来了,之后参数将传入decodeStream方法,该方法中在调用native方法进行解析Bitmap后会调用这个方法setDensityFromOptions(bm, opts);:
private static void setDensityFromOptions(Bitmap outputBitmap, Options opts) {
if (outputBitmap == null || opts == null)
final int density = opts.inD
if (density != 0) {
outputBitmap.setDensity(density);
final int targetDensity = opts.inTargetD
if (targetDensity == 0 || density == targetDensity || density == opts.inScreenDensity) {
byte[] np = outputBitmap.getNinePatchChunk();
final boolean isNinePatch = np != null && NinePatch.isNinePatchChunk(np);
if (opts.inScaled || isNinePatch) {
outputBitmap.setDensity(targetDensity);
} else if (opts.inBitmap != null) {
// bitmap was reused, ensure density is reset
outputBitmap.setDensity(Bitmap.getDefaultDensity());
该方法主要就是把刚刚赋值过的两个属性inDensity和inTargetDensity给Bitmap进行赋值,不过并不是直接赋给Bitmap就完了,中间有个判断,当inDensity的值与inTargetDensity或与设备的屏幕Density不相等时,则将应用inTargetDensity的值,如果相等则应用inDensity的值。
所以总结来说,setDensityFromOptions方法就是把inTargetDensity的值赋给Bitmap,不过前提是opts.inScaled = true;
进过上面的分析,可以得出这样一个结论:
在不配置Options的情况下:
1、decodeFile、decodeStream在解析时不会对Bitmap进行一系列的屏幕适配,解析出来的将是原始大小的图
2、decodeResource在解析时会对Bitmap根据当前设备屏幕像素密度densityDpi的值进行缩放适配操作,使得解析出来的Bitmap与当前设备的分辨率匹配,达到一个最佳的显示效果,并且Bitmap的大小将比原始的大
1.1、关于Density、分辨率、-hdpi等res目录之间的关系
DensityDpi
dp与px的换算公式为:
px = dp * Density
1.2、DisplayMetrics::densityDpi与density的区别
getResources().getDisplayMetrics().densityDpi&&表示屏幕的像素密度
getResources().getDisplayMetrics().density&&1dp等于多少个像素(px)
举个栗子:在屏幕密度为160的设备下,1dp=1px。在屏幕密度为320的设备下,1dp=2px。
所以这就为什么在安卓中布局建议使用dp为单位,因为可以根据当前设备的屏幕密度动态的调整进行适配
2、Bitmap的优化策略
2.1、BitmapFactory.Options的属性解析
BitmapFactory.Options中有以下属性:
inBitmap&&在解析Bitmap时重用该Bitmap,不过必须等大的Bitmap而且inMutable须为true
inMutable&&配置Bitmap是否可以更改,比如:在Bitmap上隔几个像素加一条线段
inJustDecodeBounds&&为true仅返回Bitmap的宽高等属性
inSampleSize&&须&=1,表示Bitmap的压缩比例,如:inSampleSize=4,将返回一个是原始图的1/16大小的Bitmap
inPreferredConfig&&Bitmap.Config.ARGB_8888等
inDither&&是否抖动,默认为false
inPremultiplied&&默认为true,一般不改变它的值
inDensity&&Bitmap的像素密度
inTargetDensity&&Bitmap最终的像素密度
inScreenDensity&&当前屏幕的像素密度
inScaled&&是否支持缩放,默认为true,当设置了这个,Bitmap将会以inTargetDensity的值进行缩放
inPurgeable&&当存储Pixel的内存空间在内存不足时是否可以被回收
inInputShareable&&inPurgeable为true情况下才生效,是否可以共享一个InputStream
inPreferQualityOverSpeed&&为true则优先保证Bitmap质量其次是解码速度
outWidth&&返回的Bitmap的宽
outHeight&&返回的Bitmap的高
inTempStorage&&解码时的临时空间,建议16*1024
2.2、优化策略
1、BitmapConfig的配置
2、使用decodeFile、decodeResource、decodeStream进行解析Bitmap时,配置inDensity和inTargetDensity,两者应该相等,值可以等于屏幕像素密度*0.75f
3、使用inJustDecodeBounds预判断Bitmap的大小及使用inSampleSize进行压缩
4、对Density&240的设备进行Bitmap的适配(缩放Density)
5、2.3版本inNativeAlloc的使用
6、4.4以下版本inPurgeable、inInputShareable的使用
7、Bitmap的回收
针对上面方案,把Bitmap解码的代码封装成了一个工具类,如下:
public class BitmapDecodeUtil {
private static final int DEFAULT_DENSITY = 240;
private static final float SCALE_FACTOR = 0.75f;
private static final Bitmap.Config DEFAULT_BITMAP_CONFIG = Bitmap.Config.RGB_565;
private static BitmapFactory.Options getBitmapOptions(Context context) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled =
options.inPreferredConfig = DEFAULT_BITMAP_CONFIG;
options.inPurgeable =
options.inInputShareable =
options.inJustDecodeBounds =
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.GINGERBREAD_MR1) {
Field field =
field = BitmapFactory.Options.class.getDeclaredField(&inNativeAlloc&);
field.setAccessible(true);
field.setBoolean(options, true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
int displayDensityDpi = context.getResources().getDisplayMetrics().densityD
float displayDensity = context.getResources().getDisplayMetrics().
if (displayDensityDpi & DEFAULT_DENSITY && displayDensity & 1.5f) {
int density = (int) (displayDensityDpi * SCALE_FACTOR);
options.inDensity =
options.inTargetDensity =
public static Bitmap decodeBitmap(Context context, int resId) {
checkParam(context);
return BitmapFactory.decodeResource(context.getResources(), resId, getBitmapOptions(context));
public static Bitmap decodeBitmap(Context context, String pathName) {
checkParam(context);
return BitmapFactory.decodeFile(pathName, getBitmapOptions(context));
public static Bitmap decodeBitmap(Context context, InputStream is) {
checkParam(context);
checkParam(is);
return BitmapFactory.decodeStream(is, null, getBitmapOptions(context));
public static Bitmap compressBitmap(Context context,int resId, int maxWidth, int maxHeight) {
checkParam(context);
final TypedValue value = new TypedValue();
InputStream is =
is = context.getResources().openRawResource(resId, value);
return compressBitmap(context, is, maxWidth, maxHeight);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
is.close();
} catch (IOException e) {
e.printStackTrace();
public static Bitmap compressBitmap(Context context, String pathName, int maxWidth, int maxHeight) {
checkParam(context);
InputStream is =
is = new FileInputStream(pathName);
return compressBitmap(context, is, maxWidth, maxHeight);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (is != null) {
is.close();
} catch (IOException e) {
e.printStackTrace();
public static Bitmap compressBitmap(Context context, InputStream is, int maxWidth, int maxHeight) {
checkParam(context);
checkParam(is);
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds =
BitmapFactory.decodeStream(is, null, opt);
int height = opt.outH
int width = opt.outW
int sampleSize = computeSampleSize(width, height, maxWidth, maxHeight);
BitmapFactory.Options options = getBitmapOptions(context);
options.inSampleSize = sampleS
return BitmapFactory.decodeStream(is, null, options);
private static int computeSampleSize(int width, int height, int maxWidth, int maxHeight) {
int inSampleSize = 1;
if (height & maxHeight || width & maxWidth) {
final int heightRate = Math.round((float) height / (float) maxHeight);
final int widthRate = Math.round((float) width / (float) maxWidth);
inSampleSize = heightRate & widthRate ? heightRate : widthR
if (inSampleSize % 2 != 0) {
inSampleSize -= 1;
return inSampleSize &= 1 ? 1 : inSampleS
private static
void checkParam(T param){
if(param == null)
throw new NullPointerException();
主要有两类方法:
一、decodeBitmap:对Bitmap不压缩,但是会根据屏幕的密度合适的进行缩放压缩
二、compressBimtap:对Bitmap进行超过最大宽高的压缩,同时也会根据屏幕的密度合适的进行缩放压缩。
3、Bitmap优化前后性能对比
针对上面方案,做一下性能对比,图片大小为3.26M,分辨率为
有两台设备:
3.1、density为320的设备
3.2、density为560的设备
可以看到,都是加载同一图片,在高屏幕像素密度的设备下所需要的内存需要很大、载入内存中的Bitmap的宽高也因设备的屏幕像素密度也改变,正如上面分析的一样,使用decodeResource会自动适配当前设备的分辨率达到一个最佳效果,而只有这个方法会自动适配其它方法将不会,依次思路,我们在封装的工具类中在每一个方法都加入了依屏幕像素密度来自动适配,而在实际中并不需要那么高清的图片,所以我们可以根据设备的density来进行缩放,比如:在400&=density&240的情况下x0.8,在density&400的情况下x0.7,这样Bitmap所占用的内存将减少非常多,可以对面上面两个图片中bitmap和decodeBitmap两个值的大小,decodeBitmap只是对density进行了一定的缩放,而占用内存却减少非常多,而且显示效果也和原先的并无区别。
之后对比我们进行了inSampleSize压缩的图片,进行压缩后的效果也看不出太大区别,而占用内存也减少了很多。vcD4NCjxoMiBpZD0="4bitmap的回收">4、Bitmap的回收
4.1、 2.3.3(API 10)及以下的系统
在2.3以下的系统中,Bitmap的像素数据是存储在native中,Bitmap对象是存储在java堆中的,所以在回收Bitmap时,需要回收两个部分的空间:native和java堆。
即先调用recycle()释放native中Bitmap的像素数据,再对Bitmap对象置null,保证GC对Bitmap对象的回收
4.2、Android 3.0(API 11)及以上的系统
在3.0以上的系统中,Bitmap的像素数据和对象本身都是存储在java堆中的,无需主动调用recycle(),只需将对象置null,由GC自动管理

我要回帖

更多关于 options属性 的文章

 

随机推荐