转 如何高效使用和管理Bitmap--图片缓存管理模块的设计与实现

原文链接: http://www.cnblogs.com/wikiki/p/4323478.html

上周为360全景项目引入了图片缓存模块。因为是在Android4.0平台以上运作,出于惯性,都会在设计之前查阅相关资料,尽量避免拿一些以前2.3平台积累的经验来进行类比处理。开发文档中有一个 BitmapFun的示例,仔细拜读了一下,虽说围绕着Bitmap的方方面面讲得都很深入,但感觉很难引入到当前项目中去。         现在的图片服务提供者基本上都来源于网络。对于应用平台而言,访问网络属于耗时操作。尤其是在移动终端设备上,它的显著表现为系统的延迟时间变长、用户交互性变差等。可以想象,一个携带着这些问题的应用在市场上是很难与同类产品竞争的。 
        说明一下,本文借鉴了 Keegan小钢和安卓巴士的处理模板,主要针对的是4.0以上平台应用。2.3以前平台执行效果未知,请斟酌使用或直接略过:),当然更欢迎您把测试结果告知笔者。 
一、图片加载流程 
        首先,我们谈谈加载图片的流程,项目中的该模块处理流程如下: 
1.在UI主线程中,从内存缓存中获取图片,找到后返回。找不到进入下一步; 
2.在工作线程中,从磁盘缓存中获取图片,找到即返回并更新内存缓存。找不到进入下一步; 
3.在工作线程中,从网络中获取图片,找到即返回并同时更新内存缓存和磁盘缓存。找不到显示默认以提示。 

二、内存缓存类(PanoMemCache)

        这里使用Android提供的LruCache类,该类保存一个强引用来限制内容数量,每当Item被访问的时候,此Item就会移动到队列的头部。当cache已满的时候加入新的item时,在队列尾部的item会被回收。

[java]  view plain  copy  print  ?
  1. public class PanoMemoryCache {  
  2.   
  3.     // LinkedHashMap初始容量  
  4.     private static final int INITIAL_CAPACITY = 16;  
  5.     // LinkedHashMap加载因子  
  6.     private static final int LOAD_FACTOR = 0.75f;  
  7.     // LinkedHashMap排序模式  
  8.     private static final boolean ACCESS_ORDER = true;  
  9.   
  10.     // 软引用缓存  
  11.     private static LinkedHashMap<String, SoftReference<Bitmap>> mSoftCache;  
  12.     // 硬引用缓存  
  13.     private static LruCache<String, Bitmap> mLruCache;  
  14.       
  15.     public PanoMemoryCache() {  
  16.     // 获取单个进程可用内存的最大值  
  17.     // 方式一:使用ActivityManager服务(计量单位为M)  
  18.         /*int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();*/  
  19.     // 方式二:使用Runtime类(计量单位为Byte)  
  20.         final int memClass = (int) Runtime.getRuntime().maxMemory();  
  21.         // 设置为可用内存的1/4(按Byte计算)  
  22.         final int cacheSize = memClass / 4;  
  23.         mLruCache = new LruCache<String, Bitmap>(cacheSize) {  
  24.             @Override  
  25.             protected int sizeOf(String key, Bitmap value) {  
  26.                 if(value != null) {  
  27.                     // 计算存储bitmap所占用的字节数  
  28.                     return value.getRowBytes() * value.getHeight();  
  29.                 } else {  
  30.                     return 0;  
  31.                 }  
  32.             }  
  33.               
  34.             @Override  
  35.             protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {  
  36.                 if(oldValue != null) {  
  37.                     // 当硬引用缓存容量已满时,会使用LRU算法将最近没有被使用的图片转入软引用缓存  
  38.                     mSoftCache.put(key, new SoftReference<Bitmap>(oldValue));  
  39.                 }  
  40.             }  
  41.         };  
  42.           
  43.     /* 
  44.     * 第一个参数:初始容量(默认16) 
  45.     * 第二个参数:加载因子(默认0.75) 
  46.     * 第三个参数:排序模式(true:按访问次数排序;false:按插入顺序排序) 
  47.     */  
  48.         mSoftCache = new LinkedHashMap<String, SoftReference<Bitmap>>(INITIAL_CAPACITY, LOAD_FACTOR, ACCESS_ORDER) {  
  49.             private static final long serialVersionUID = 7237325113220820312L;  
  50.             @Override  
  51.             protected boolean removeEldestEntry(Entry<String, SoftReference<Bitmap>> eldest) {  
  52.                 if(size() > SOFT_CACHE_SIZE) {  
  53.                     return true;  
  54.                 }  
  55.                 return false;  
  56.             }  
  57.         };  
  58.     }  
  59.       
  60.     /** 
  61.      * 从缓存中获取Bitmap 
  62.      * @param url 
  63.      * @return bitmap 
  64.      */  
  65.     public Bitmap getBitmapFromMem(String url) {  
  66.         Bitmap bitmap = null;  
  67.         // 先从硬引用缓存中获取  
  68.         synchronized (mLruCache) {  
  69.             bitmap = mLruCache.get(url);  
  70.             if(bitmap != null) {  
  71.                 // 找到该Bitmap之后,将其移到LinkedHashMap的最前面,保证它在LRU算法中将被最后删除。  
  72.                 mLruCache.remove(url);  
  73.                 mLruCache.put(url, bitmap);  
  74.                 return bitmap;  
  75.             }  
  76.         }  
  77.   
  78.   
  79.         // 再从软引用缓存中获取  
  80.         synchronized (mSoftCache) {  
  81.             SoftReference<Bitmap> bitmapReference = mSoftCache.get(url);  
  82.             if(bitmapReference != null) {  
  83.                 bitmap = bitmapReference.get();  
  84.                 if(bitmap != null) {  
  85.                     // 找到该Bitmap之后,将它移到硬引用缓存。并从软引用缓存中删除。  
  86.                     mLruCache.put(url, bitmap);  
  87.                     mSoftCache.remove(url);  
  88.                     return bitmap;  
  89.                 } else {  
  90.                     mSoftCache.remove(url);  
  91.                 }  
  92.             }  
  93.         }  
  94.         return null;  
  95.     }  
  96.       
  97.     /** 
  98.      * 添加Bitmap到内存缓存 
  99.      * @param url 
  100.      * @param bitmap 
  101.      */  
  102.     public void addBitmapToCache(String url, Bitmap bitmap) {  
  103.         if(bitmap != null) {  
  104.             synchronized (mLruCache) {  
  105.               mLruCache.put(url, bitmap);    
  106.             }  
  107.         }  
  108.     }  
  109.       
  110.     /** 
  111.      * 清理软引用缓存 
  112.      */  
  113.     public void clearCache() {  
  114.         mSoftCache.clear();  
  115.     mSoftCache = null;  
  116.     }  
  117. }  

        补充一点,由于4.0平台以后对SoftReference类引用的对象调整了回收策略,所以该类中的软引用缓存实际上没什么效果,可以去掉。2.3以前平台建议保留。 
三、磁盘缓存类(PanoDiskCache) 

[java]  view plain  copy  print  ?
  1. public class PanoDiskCache {  
  2.       
  3.     private static final String TAG = "PanoDiskCache";  
  4.   
  5.     // 文件缓存目录  
  6.     private static final String CACHE_DIR = "panoCache";  
  7.     private static final String CACHE_FILE_SUFFIX = ".cache";  
  8.       
  9.     private static final int MB = 1024 * 1024;  
  10.     private static final int CACHE_SIZE = 10; // 10M  
  11.     private static final int SDCARD_CACHE_THRESHOLD = 10;  
  12.       
  13.     public PanoDiskCache() {  
  14.         // 清理文件缓存  
  15.         removeCache(getDiskCacheDir());  
  16.     }  
  17.       
  18.     /** 
  19.      * 从磁盘缓存中获取Bitmap 
  20.      * @param url 
  21.      * @return  
  22.      */  
  23.     public Bitmap getBitmapFromDisk(String url) {  
  24.         String path = getDiskCacheDir() + File.separator + genCacheFileName(url);  
  25.         File file = new File(path);  
  26.         if(file.exists()) {  
  27.             Bitmap bitmap = BitmapFactory.decodeFile(path);  
  28.             if(bitmap == null) {  
  29.                 file.delete();  
  30.             } else {  
  31.                 updateLastModified(path);  
  32.                 return bitmap;  
  33.             }  
  34.         }  
  35.         return null;  
  36.     }  
  37.       
  38.     /** 
  39.      * 将Bitmap写入文件缓存 
  40.      * @param bitmap 
  41.      * @param url 
  42.      */  
  43.     public void addBitmapToCache(Bitmap bitmap, String url) {  
  44.         if(bitmap == null) {  
  45.             return;  
  46.         }  
  47.         // 判断当前SDCard上的剩余空间是否足够用于文件缓存  
  48.         if(SDCARD_CACHE_THRESHOLD > calculateFreeSpaceOnSd()) {  
  49.             return;  
  50.         }  
  51.         String fileName = genCacheFileName(url);  
  52.         String dir = getDiskCacheDir();  
  53.         File dirFile = new File(dir);  
  54.         if(!dirFile.exists()) {  
  55.             dirFile.mkdirs();  
  56.         }  
  57.         File file = new File(dir + File.separator + fileName);  
  58.         try {  
  59.             file.createNewFile();  
  60.             FileOutputStream out = new FileOutputStream(file);  
  61.             bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);  
  62.             out.flush();  
  63.             out.close();  
  64.         } catch (FileNotFoundException e) {  
  65.             Log.e(TAG, "FileNotFoundException");  
  66.         } catch (IOException e) {  
  67.             Log.e(TAG, "IOException");  
  68.         }  
  69.     }  
  70.       
  71.     /** 
  72.      * 清理文件缓存 
  73.      * 当缓存文件总容量超过CACHE_SIZE或SDCard的剩余空间小于SDCARD_CACHE_THRESHOLD时,将删除40%最近没有被使用的文件 
  74.      * @param dirPath 
  75.      * @return  
  76.      */  
  77.     private boolean removeCache(String dirPath) {  
  78.         File dir = new File(dirPath);  
  79.         File[] files = dir.listFiles();  
  80.         if(files == null || files.length == 0) {  
  81.             return true;  
  82.         }  
  83.         if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {  
  84.             return false;  
  85.         }  
  86.           
  87.         int dirSize = 0;  
  88.         for (int i = 0; i < files.length; i++) {  
  89.             if(files[i].getName().contains(CACHE_FILE_SUFFIX)) {  
  90.                 dirSize += files[i].length();  
  91.             }  
  92.         }  
  93.         if(dirSize > CACHE_SIZE * MB || SDCARD_CACHE_THRESHOLD > calculateFreeSpaceOnSd()) {  
  94.             int removeFactor = (int) (0.4 * files.length + 1);  
  95.             Arrays.sort(files, new FileLastModifiedSort());  
  96.             for (int i = 0; i < removeFactor; i++) {  
  97.                 if(files[i].getName().contains(CACHE_FILE_SUFFIX)) {  
  98.                     files[i].delete();  
  99.                 }  
  100.             }  
  101.         }  
  102.           
  103.         if(calculateFreeSpaceOnSd() <= SDCARD_CACHE_THRESHOLD) {  
  104.             return false;  
  105.         }  
  106.         return true;  
  107.     }  
  108.       
  109.     /** 
  110.      * 更新文件的最后修改时间 
  111.      * @param path 
  112.      */  
  113.     private void updateLastModified(String path) {  
  114.         File file = new File(path);  
  115.         long time = System.currentTimeMillis();  
  116.         file.setLastModified(time);  
  117.     }  
  118.       
  119.     /** 
  120.      * 计算SDCard上的剩余空间 
  121.      * @return  
  122.      */  
  123.     private int calculateFreeSpaceOnSd() {  
  124.         StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());  
  125.         double sdFreeMB = ((double) stat.getAvailableBlocks() * (double) stat.getBlockSize()) / MB;  
  126.         return (int) sdFreeMB;  
  127.     }  
  128.   
  129.   
  130.     /** 
  131.      * 生成统一的磁盘文件后缀便于维护 
  132.      * 从URL中得到源文件名称,并为它追加缓存后缀名.cache 
  133.      * @param url 
  134.      * @return 文件存储后的名称 
  135.      */  
  136.     private String genCacheFileName(String url) {  
  137.         String[] strs = url.split(File.separator);  
  138.         return strs[strs.length - 1] + CACHE_FILE_SUFFIX;  
  139.     }  
  140.       
  141.     /** 
  142.      * 获取磁盘缓存目录 
  143.      * @return  
  144.      */  
  145.     private String getDiskCacheDir() {  
  146.         return getSDPath() + File.separator + CACHE_DIR;  
  147.     }  
  148.       
  149.     /** 
  150.      * 获取SDCard目录 
  151.      * @return  
  152.      */  
  153.     private String getSDPath() {  
  154.         File sdDir = null;  
  155.         // 判断SDCard是否存在  
  156.         boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);  
  157.         if(sdCardExist) {  
  158.             // 获取SDCard根目录  
  159.             sdDir = Environment.getExternalStorageDirectory();  
  160.         }  
  161.         if(sdDir != null) {  
  162.             return sdDir.toString();  
  163.         } else {  
  164.             return "";  
  165.         }  
  166.     }  
  167.       
  168.     /** 
  169.      * 根据文件最后修改时间进行排序 
  170.      */  
  171.     private class FileLastModifiedSort implements Comparator<File> {  
  172.         @Override  
  173.         public int compare(File lhs, File rhs) {  
  174.             if(lhs.lastModified() > rhs.lastModified()) {  
  175.                 return 1;  
  176.             } else if(lhs.lastModified() == rhs.lastModified()) {  
  177.                 return 0;  
  178.             } else {  
  179.                 return -1;  
  180.             }  
  181.         }  
  182.     }  
  183. }  

四、图片工具类(PanoUtils) 
1.从网络上获取图片:downloadBitmap() 

[java]  view plain  copy  print  ?
  1. /** 
  2.     * 从网络上获取Bitmap,并进行适屏和分辨率处理。 
  3.     * @param context 
  4.     * @param url 
  5.     * @return  
  6.     */  
  7.    public static Bitmap downloadBitmap(Context context, String url) {  
  8.        HttpClient client = new DefaultHttpClient();  
  9.        HttpGet request = new HttpGet(url);  
  10.          
  11.        try {  
  12.            HttpResponse response = client.execute(request);  
  13.            int statusCode = response.getStatusLine().getStatusCode();  
  14.            if(statusCode != HttpStatus.SC_OK) {  
  15.                Log.e(TAG, "Error " + statusCode + " while retrieving bitmap from " + url);  
  16.                return null;  
  17.            }  
  18.              
  19.            HttpEntity entity = response.getEntity();  
  20.            if(entity != null) {  
  21.                InputStream in = null;  
  22.                try {  
  23.                    in = entity.getContent();  
  24.                    return scaleBitmap(context, readInputStream(in));  
  25.                } finally {  
  26.                    if(in != null) {  
  27.                        in.close();  
  28.                        in = null;  
  29.                    }  
  30.                    entity.consumeContent();  
  31.                }  
  32.            }  
  33.        } catch (IOException e) {  
  34.            request.abort();  
  35.            Log.e(TAG, "I/O error while retrieving bitmap from " + url, e);  
  36.        } catch (IllegalStateException e) {  
  37.            request.abort();  
  38.            Log.e(TAG, "Incorrect URL: " + url);  
  39.        } catch (Exception e) {  
  40.            request.abort();  
  41.            Log.e(TAG, "Error while retrieving bitmap from " + url, e);  
  42.        } finally {  
  43.            client.getConnectionManager().shutdown();  
  44.        }  
  45.        return null;  
  46.    }     

2.从输入流读取字节数组,看起来是不是很眼熟啊! 

[java]  view plain  copy  print  ?
  1. public static byte[] readInputStream(InputStream in) throws Exception {  
  2.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  3.         byte[] buffer = new byte[1024];  
  4.         int len = 0;  
  5.         while((len = in.read(buffer)) != -1) {  
  6.             out.write(buffer, 0, len);  
  7.         }  
  8.         in.close();  
  9.         return out.toByteArray();  
  10.     }      

3.对下载的源图片进行适屏处理,这也是必须的:) 

[java]  view plain  copy  print  ?
  1. /** 
  2.      * 按使用设备屏幕和纹理尺寸适配Bitmap 
  3.      * @param context 
  4.      * @param in 
  5.      * @return  
  6.      */  
  7.     private static Bitmap scaleBitmap(Context context, byte[] data) {  
  8.           
  9.         WindowManager windowMgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);  
  10.         DisplayMetrics outMetrics = new DisplayMetrics();  
  11.         windowMgr.getDefaultDisplay().getMetrics(outMetrics);  
  12.         int scrWidth = outMetrics.widthPixels;  
  13.         int scrHeight = outMetrics.heightPixels;  
  14.           
  15.         BitmapFactory.Options options = new BitmapFactory.Options();  
  16.         options.inJustDecodeBounds = true;  
  17.         Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);  
  18.         int imgWidth = options.outWidth;  
  19.         int imgHeight = options.outHeight;  
  20.           
  21.         if(imgWidth > scrWidth || imgHeight > scrHeight) {  
  22.             options.inSampleSize = calculateInSampleSize(options, scrWidth, scrHeight);  
  23.         }  
  24.         options.inJustDecodeBounds = false;  
  25.         bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);  
  26.           
  27.         // 根据业务的需要,在此处还可以进一步做处理  
  28.         ...  
  29.   
  30.         return bitmap;  
  31.     }  
  32.       
  33.     /** 
  34.      * 计算Bitmap抽样倍数 
  35.      * @param options 
  36.      * @param reqWidth 
  37.      * @param reqHeight 
  38.      * @return  
  39.      */  
  40.     public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {  
  41.         // 原始图片宽高  
  42.         final int height = options.outHeight;  
  43.         final int width = options.outWidth;  
  44.         int inSampleSize = 1;  
  45.       
  46.         if (height > reqHeight || width > reqWidth) {  
  47.       
  48.             // 计算目标宽高与原始宽高的比值  
  49.             final int heightRatio = Math.round((float) height / (float) reqHeight);  
  50.             final int widthRatio = Math.round((float) width / (float) reqWidth);  
  51.       
  52.             // 选择两个比值中较小的作为inSampleSize的值  
  53.             inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
  54.             if(inSampleSize < 1) {  
  55.                 inSampleSize = 1;  
  56.             }  
  57.         }  
  58.   
  59.         return inSampleSize;  
  60.     }  

五、使用decodeByteArray()还是decodeStream()? 
        讲到这里,有童鞋可能会问我为什么使用BitmapFactory.decodeByteArray(data, 0, data.length, opts)来创建Bitmap,而非使用BitmapFactory.decodeStream(is, null, opts)。你这样做不是要多写一个静态方法readInputStream()吗? 
        没错,decodeStream()确实是该使用情景下的首选方法,但是在有些情形下,它会导致图片资源不能即时获取,或者说图片被它偷偷地缓存起来,交 还给我们的时间有点长。但是延迟性是致命的,我们等不起。所以在这里选用decodeByteArray()获取,它直接从字节数组中获取,贴近于底层 IO、脱离平台限制、使用起来风险更小。 
六、引入缓存机制后获取图片的方法 

[java]  view plain  copy  print  ?
  1. /** 
  2.      * 加载Bitmap 
  3.      * @param url 
  4.      * @return  
  5.      */  
  6.     private Bitmap loadBitmap(String url) {  
  7.         // 从内存缓存中获取,推荐在主UI线程中进行  
  8.         Bitmap bitmap = memCache.getBitmapFromMem(url);  
  9.         if(bitmap == null) {  
  10.             // 从文件缓存中获取,推荐在工作线程中进行  
  11.             bitmap = diskCache.getBitmapFromDisk(url);  
  12.             if(bitmap == null) {  
  13.                 // 从网络上获取,不用推荐了吧,地球人都知道~_~  
  14.                 bitmap = PanoUtils.downloadBitmap(this, url);  
  15.                 if(bitmap != null) {  
  16.                     diskCache.addBitmapToCache(bitmap, url);  
  17.                     memCache.addBitmapToCache(url, bitmap);  
  18.                 }  
  19.             } else {  
  20.                 memCache.addBitmapToCache(url, bitmap);  
  21.             }  
  22.         }  
  23.         return bitmap;  
  24.     }  

七、工作线程池化 
        有关多线程的切换问题以及在UI线程中执行loadBitmap()方法无效的问题,请参见另一篇博文: 使用严苛模式打破Android4.0以上平台应用中UI主线程的“独断专行”。 
有关工作线程的处理方式,这里推荐使用定制线程池的方式,核心代码如下: 

[java]  view plain  copy  print  ?
  1. // 线程池初始容量  
  2. private static final int POOL_SIZE = 4;  
  3. private ExecutorService executorService;  
  4. @Override  
  5. public void onCreate(Bundle savedInstanceState) {  
  6.     super.onCreate(savedInstanceState);  
  7.   
  8.     // 获取当前使用设备的CPU个数  
  9.     int cpuNums = Runtime.getRuntime().availableProcessors();  
  10.     // 预开启线程池数目  
  11.     executorService = Executors.newFixedThreadPool(cpuNums * POOL_SIZE);  
  12.   
  13.     ...  
  14.     executorService.submit(new Runnable() {  
  15.         // 此处执行一些耗时工作,不要涉及UI工作。如果遇到,直接转交UI主线程  
  16.         pano.setImage(loadBitmap(url));  
  17.     });  
  18.     ...  
  19.   
  20. }  

        我们知道,线程构造也是比较耗资源的。一定要对其进行有效的管理和维护。千万不要随意而行,一张图片的工作线程不搭理也许没什么,当使用场景变为 ListView和GridView时,线程池化工作就显得尤为重要了。Android不是提供了AsyncTask吗?为什么不用它?其实 AsyncTask底层也是靠线程池支持的,它默认分配的线程数是128,是远大于我们定制的executorService。

转载于:https://www.cnblogs.com/wikiki/p/4323478.html

猜你喜欢

转载自blog.csdn.net/weixin_30627341/article/details/94788281