http://lipeng88213.iteye.com/blog/1845117
2013.04.10——— android 图片缓存之三createBitmap
参考:http://blog.csdn.net/mujianwei/article/details/8016844
http://onewayonelife.iteye.com/blog/1158698
http://zhiweiofli.iteye.com/blog/905066
http://blog.csdn.net/mzz5240/article/details/8722876
我做一个应用需要非常频繁的加载很多图片,经常遇到内存溢出的问题 直接crash,这里有几个避免溢出的注意点,分享一下:
1、颜色数修改
- * ALPHA_8:数字为8,图形参数应该由一个字节来表示,应该是一种8位的位图
- * ARGB_4444:4+4+4+4=16,图形的参数应该由两个字节来表示,应该是一种16位的位图.
- * ARGB_8888:8+8+8+8=32,图形的参数应该由四个字节来表示,应该是一种32位的位图.
- * RGB_565:5+6+5=16,图形的参数应该由两个字节来表示,应该是一种16位的位图.
Bitmap.Config | ALPHA_8 | Each pixel is stored as a single translucency (alpha) channel. |
Bitmap.Config | ARGB_4444 | This field is deprecated. Because of the poor quality of this configuration, it is advised to use ARGB_8888 instead. |
Bitmap.Config | ARGB_8888 | Each pixel is stored on 4 bytes. |
Bitmap.Config | RGB_565 | Each pixel is stored on 2 bytes and only the RGB channels are encoded: red is stored with 5 bits of precision (32 possible values), green is stored with 6 bits of precision (64 possible values) and blue is stored with 5 bits of precision. |
创建bitmap默认是ARGB_8888,表示24bit颜色和8bit透明通道,如果不需要透明度 尽量修改为RGB_565
- opt.inPreferredConfig = Bitmap.Config.RGB_565;
2、压缩图片
有时候 我们需要的图片宽和高很小,这个时候 就不要把图片按原始尺寸显示出来,就可以缩小一下
获取原始图片大小
- BitmapFactory.Options opt = new BitmapFactory.Options();
- opt.inJustDecodeBounds = true;
- //设置只是解码图片的边距,此操作目的是度量图片的实际宽度和高度
- BitmapFactory.decodeFile(file, opt);
- int outWidth = opt.outWidth; //获得图片的实际高和宽
- int outHeight = opt.outHeight;
计算缩放比例
- opt.inSampleSize = 1;
- //设置缩放比,1表示原比例,2表示原来的四分之一....
- //计算缩放比
- if (outWidth != 0 && outHeight != 0 && width != 0 && height != 0) {
- int sampleSize = (outWidth / width + outHeight / height) / 2;
- opt.inSampleSize = sampleSize;
- }
- opt.inJustDecodeBounds = false;//最后把标志复原
google官方给出的计算inSampleSize的大小
- public static int calculateInSampleSize(BitmapFactory.Options options,
- int reqWidth, int reqHeight) {
- // Raw height and width of image
- final int height = options.outHeight;
- final int width = options.outWidth;
- int inSampleSize = 1;
- if (height > reqHeight || width > reqWidth) {
- // Calculate ratios of height and width to requested height and width
- final int heightRatio = Math.round((float) height / (float) reqHeight);
- final int widthRatio = Math.round((float) width / (float) reqWidth);
- // Choose the smallest ratio as inSampleSize value, this will guarantee a final image
- // with both dimensions larger than or equal to the requested height and width.
- inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
- // This offers some additional logic in case the image has a strange
- // aspect ratio. For example, a panorama may have a much larger
- // width than height. In these cases the total pixels might still
- // end up being too large to fit comfortably in memory, so we should
- // be more aggressive with sample down the image (=larger inSampleSize).
- final float totalPixels = width * height;
- // Anything more than 2x the requested pixels we'll sample down further
- final float totalReqPixelsCap = reqWidth * reqHeight * 2;
- while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
- inSampleSize++;
- }
- }
- return inSampleSize;
- }
3、options其他的设置
- BitmapFactory.Options.inPurgeable;
- *
- * 如果 inPurgeable 设为True的话表示使用BitmapFactory创建的Bitmap
- * 用于存储Pixel的内存空间在系统内存不足时可以被回收,
- * 在应用需要再次访问Bitmap的Pixel时(如绘制Bitmap或是调用getPixel),
- * 系统会再次调用BitmapFactory decoder重新生成Bitmap的Pixel数组。
- * 为了能够重新解码图像,bitmap要能够访问存储Bitmap的原始数据。
- *
- * 在inPurgeable为false时表示创建的Bitmap的Pixel内存空间不能被回收,
- * 这样BitmapFactory在不停decodeByteArray创建新的Bitmap对象,
- * 不同设备的内存不同,因此能够同时创建的Bitmap个数可能有所不同,
- * 200个bitmap足以使大部分的设备重新OutOfMemory错误。
- * 当isPurgable设为true时,系统中内存不足时,
- * 可以回收部分Bitmap占据的内存空间,这时一般不会出现OutOfMemory 错误。
- *
- *
- * inInputShareable 是否深拷贝 这个和inPurgeable一起出现
- public static Bitmap readBitMap(Context context, int resId) {
- BitmapFactory.Options opt = new BitmapFactory.Options();
- opt.inPreferredConfig = Bitmap.Config.RGB_565;
- opt.inPurgeable = true;
- opt.inInputShareable = true;
- InputStream is = context.getResources().openRawResource(resId);
- return BitmapFactory.decodeStream(is, null, opt);
- }
4、可以尽量用decodeStream
因为源码里面 无论decodeFile还是decodeResourceStream都是调用了decodeStream
为了避免java.lang.OutOfMemory 的异常,我们在真正decode图片之前检查它的尺寸,除非你确定这个数据源提供了准确无误的图片且不会导致占用过多的内存。