Bitmap.Config的四种枚举类型。
(ARGB分别是alpha透明度和red、green、blue三色)
ARGB_8888:分别用8位来记录4个值,所以每个像素会占用32位。4 bytes
ARGB_4444:分别用4位来记录4个值,所以每个像素会占用16位。2 bytes
RGB_565:分别用5位、6位和5位来记录RGB三色值,所以每个像素会占用16位。2 bytes
ALPHA_8:根据注释应该是不保存颜色值,只保存透明度(8位),每个像素会占用8位。1 byte
以ARGB_8888为基准来进行对比。 4 bytes
ARGB_4444:内存占用减少一半,但是每个值图片失真度很严重,这个参数本身已经不推荐使用了。2 bytes
RGB_565:内存占用减少一半,舍弃了透明度,同时三色值也有部分损失,但是图片失真度很小。2 bytes
ALPHA_8:内存占用没有减少!按注释的解释个人理解应该是减少3/4的内存占用,而且图片与ARGB_8888下的没有区别。1 byte
图片格式(Bitmap.Config) | 占用内存的计算方向 | 一张100*100的图片占用内存的大小 |
ALPHA_8 | 图片长度*图片宽度 | 100*100=10000字节 |
ARGB_4444 | 图片长度*图片宽度*2 | 100*100*2=20000字节 |
ARGB_8888 | 图片长度*图片宽度*4 | 100*100*4=40000字节 |
RGB_565 | 图片长度*图片宽度*2 | 100*100*2=20000字节 |
由于ARGB_4444不推荐使用和ALPHA_8效果不明。我们大多数是用的还是ARGB_8888和RGB_565。
RGB_565能够在保证图片质量的情况下大大减少内存的开销,是解决oom的一种方法。但是一定要注意RGB_565是没有透明度的,如果图片本身需要保留透明度,那么就不能使用RGB_565。
try {
URL url = new URL("http://h.hiphotos.baidu.com/image/pic/item/b21c8701a18b87d6b025e513040828381f30fd53.jpg");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
InputStream in = connection.getInputStream();
ByteArrayOutputStream content = new ByteArrayOutputStream();
byte[] buffer = new byte[10 * 1024];
int count;
while((count = in.read(buffer)) > 0){
content.write(buffer, 0, count);
}
byte[] data = content.toByteArray();
BitmapFactory.Options options1 = new BitmapFactory.Options();
options1.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap1 = BitmapFactory.decodeByteArray(data, 0 , data.length, options1);
//System.out.println("bitmap ARGB_8888 length " + bitmap1.getByteCount());
System.out.println("bitmap ARGB_8888 length " + bitmap1.getRowBytes() * bitmap1.getHeight());
BitmapFactory.Options options2 = new BitmapFactory.Options();
options2.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap2 = BitmapFactory.decodeByteArray(data, 0 , data.length, options2);
//System.out.println("bitmap RGB_565 length " + bitmap2.getByteCount());
System.out.println("bitmap RGB_565 length " + bitmap2.getRowBytes() * bitmap2.getHeight());
BitmapFactory.Options options3 = new BitmapFactory.Options();
options3.inPreferredConfig = Bitmap.Config.ALPHA_8;
Bitmap bitmap3 = BitmapFactory.decodeByteArray(data, 0 , data.length, options3);
//System.out.println("bitmap ALPHA_8 length " + bitmap3.getByteCount());
System.out.println("bitmap ALPHA_8 length " + bitmap3.getRowBytes() * bitmap3.getHeight());
BitmapFactory.Options options4 = new BitmapFactory.Options();
options4.inPreferredConfig = Bitmap.Config.ARGB_4444;
Bitmap bitmap4 = BitmapFactory.decodeByteArray(data, 0 , data.length, options4);
//System.out.println("bitmap ARGB_4444 length " + bitmap4.getByteCount());
System.out.println("bitmap ARGB_4444 length " + bitmap4.getRowBytes() * bitmap4.getHeight());
} catch (Exception e) {
e.printStackTrace();
}
BitmapFactory
这个类提供了多个解析方法(decodeByteArray, decodeFile, decodeResource等)用于创建Bitmap对象.
SD卡中的图片可以使用decodeFile方法,网络上的图片可以使用decodeStream方法,资源文件中的图片可以使用decodeResource方法.
可选的BitmapFactory.Options参数inJustDecodeBounds属性设置为true就可以让解析方法禁止为bitmap分配内存,返回值也不再是一个Bitmap对象,而是null。虽然Bitmap是null了,但是BitmapFactory.Options的outWidth、outHeight和outMimeType属性都会被赋值。
代码块
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// 第一次解析将inJustDecodeBounds设置为true,来获取图片大小
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// 调用上面定义的方法计算inSampleSize值
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// 使用获取到的inSampleSize值再次解析图片
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
读取过一次InputStream流了,所以再次通过InputStream流得到bitmap的时候就为空了,因为InputStream流只能被读取一次,下次读取就为空了。
代码:
public static final InputStream byte2Input(byte[] buf) {
return new ByteArrayInputStream(buf);
}
public static final byte[] input2byte(InputStream inStream)
throws IOException {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = inStream.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] in2b = swapStream.toByteArray();
return in2b;
}
public static Bitmap revitionImageStream(InputStream is) throws IOException {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inJustDecodeBounds = true;
byte[] data = StreamUtils.input2byte(is);//将InputStream转为byte数组,可以多次读取
BitmapFactory.decodeByteArray(data, 0, data.length, options);
int i = 0;
Bitmap bitmap = null;
while (true) {
if ((options.outWidth >> i <= 256)
&& (options.outHeight >> i <= 256)) {
options.inSampleSize = (int) Math.pow(2.0D, i);
options.inJustDecodeBounds = false;
options.inInputShareable = true;
options.inPurgeable = true;
bitmap=BitmapFactory.decodeByteArray(data, 0, data.length, options);//返回空;
break;
}
i += 1;
}
return bitmap;
}
使用图片缓存技术
public LruCache<String, Bitmap> mMemoryCache;
int memClass = ((ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
memClass = memClass > 32 ? 32 : memClass;
// 使用可用内存的1/8作为图片缓存
final int cacheSize = 1024 * 1024 * memClass / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight();
}
};
Log.i(TAG, "MemoryCache will use up to "+limit/1024./1024.+"MB");