TOP

Bitmap以及LRUCache

Bitmap优化

Bitmap解码

-常见的jpg(有损压缩),png(无损压缩),webp(结合两者优点,android4.2之后支持)使图像的存储格式。

-Android中要显示图片必须先经过解码(decode)读取图像的数据到内存中。

-BitmapFactory提供了常用的一些decode方法。

-图片真正占用的内存大小要看decode之后的数据大小。 
Bitmap解码耗时,最好放置异步线程


Bitmap复用

Bitmap复用工具--第三方库glide

  1. //指向一个已经创建的对象mCurrentBitmap,解码新的bitmap就会复用其内存
  2. mBitmapOptions.inBitmap=mCurrentBitmap;
  3. mCurrentBitmap=BitmapFactory.decodeFile(filename,mBitmapOptions);

Bitmap缩放

  • 按比例缩放
  1. //缩放到指定的大小
  2. //从一个inBmap获取指定大小的Bitmap
  3. createScaledBitmap(inBmp,64,128);
  1. //缩放到原图的1/4
  2. mBitmapOptions.inSampleSize=4;
  3. mCurrentBitmap=BitmapFactory.decodeFile(fileName,mBitmapOptions);
  1. //缩放为(int)dstWidth/srcWidth
  2. mBitmapOptions.inScaled=true;
  3. mBitmapOptions.inDensity=srcWidth;
  4. mBitmapOptions.inTargetDensity=dstWidth;
  5. mCurrentBitmap=BitmapFactory.decodeResources(getResources(),mImageId,mBitmapOptions);
  1. //先缩放为1/4,再缩放(int)dstWidth*4/srcWidth
  2. //这种缩放效率会很快
  3. mBitmapOptions.inScaled=true;
  4. mBitmapOptions.inSampleSize=4;
  5. mBitmapOptions.inDensity=srcWidth;
  6. mBitmapOptions.inTargetDensity=dstWidth*mBitmapOptions.inSampleSize;
  7. mCurrentBitmap=BitmapFactory.decodeFile(fileName,mBitmapOptions);
  • 不加载图片至内存就获取原图的宽和高
  1. mBitmapOptions.inJustDecodeBounds=true;
  2. BitmapFactory.decodeFile(fileName,mBitmapOptions);
  3. srcWidth = mBitmapOptions.outWidth;
  4. srcHeight= mBitmapOptions.outHeight;
  1. public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,int reqWidth, int reqHeight) {
  2. final BitmapFactory.Options options = new BitmapFactory.Options();
  3. options.inJustDecodeBounds = true;
  4. BitmapFactory.decodeResource(res, resId, options);
  5. options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  6. options.inJustDecodeBounds = false;
  7. return BitmapFactory.decodeResource(res, resId, options);
  8. }
  9. ```
  10. ```
  11. //计算inSampleSize
  12. public static int calculateInSampleSize(BitmapFactory.Options options,
  13. int reqWidth, int reqHeight) {
  14. final int height = options.outHeight;
  15. final int width = options.outWidth;
  16. int inSampleSize = 1;
  17. if (height > reqHeight || width > reqWidth) {
  18. if (width > height) {
  19. inSampleSize = Math.round((float) height / (float) reqHeight);
  20. } else {
  21. inSampleSize = Math.round((float) width / (float) reqWidth);
  22. }
  23. final float totalPixels = width * height;
  24. final float totalReqPixelsCap = reqWidth * reqHeight * 2;
  25. while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
  26. inSampleSize++;
  27. }
  28. }
  29. return inSampleSize;
  30. }
  31. }

Bitmap减小占用字节

A:透明度 R:红色 G:绿 B:蓝 
Bitmap.Config ARGB_4444:每个像素占四位,共16位 
Bitmap.Config ARGB_8888:每个像素占四位,共32位 
Bitmap.Config RGB_565:每个像素占四位,共16位 
Bitmap.Config ALPHA_8:每个像素占四位,只有透明度,没有颜色。

  1. //设置图片像素格式
  2. BitmapFactory.Options options = new BitmapFactory.Options();
  3. // 默认是Bitmap.Config.ARGB_8888
  4. options.inPreferredConfig = Bitmap.Config.ARGB_4444;
  5. BitmapFactory.decodeResources(getResources(),mImgId,options);

LRU cache

  • 初始化
  1. ActivityManager am=(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
  2. int availMernlnBytes=am.getMemoryClass()*1024*1024;
  3. LruCache bitmapCache=new LruCache<String,Bitmap>(availMernlnBytes/8);
  • 指定插入数据的大小
  1. public class ThumbnailCache extends LruCache(String,Bitmap){
  2. @Override
  3. protected int sizeOf(String key,Bitmap value){
  4. return value.getByteCount();
  5. }
  6. }
  • 实现
  1. Bitmap map=mCache.get(fileName);
  2. if(map==null){
  3. map=BitmapFactory.decodeFile(fileName);
  4. mCache.put(fileName,map);
  5. }





posted @ 2015-07-25 09:20  星空守候  阅读(298)  评论(0编辑  收藏  举报