1.三级缓存设计步骤:
* 从内存中取图片
* 从本地文件中取图片
向内存中保持一份
* 请求网络图片,获取图片,显示到控件上
向内存存一份
向本地文件中存一份
1 package com.atguigu.beijingnews.utils; 2 3 import android.graphics.Bitmap; 4 import android.os.Handler; 5 6 public class BitmapCacheUtils { 7 8 /** 9 * 网络缓存工具类 10 */ 11 private NetCacheUtils netCacheUtils; 12 13 /** 14 * 本地缓存工具类 15 */ 16 17 private LocalCacheUtils localCacheUtils; 18 19 /** 20 内存缓存工具类 21 */ 22 private MemoryCacheUtils memoryCacheUtils; 23 24 public BitmapCacheUtils(Handler handler) { 25 memoryCacheUtils = new MemoryCacheUtils(); 26 localCacheUtils = new LocalCacheUtils(memoryCacheUtils); 27 netCacheUtils = new NetCacheUtils(handler,localCacheUtils,memoryCacheUtils); 28 29 } 30 31 32 /** 33 * 三级缓存设计步骤: 34 * * 从内存中取图片 35 * * 从本地文件中取图片 36 * 向内存中保持一份 37 * * 请求网络图片,获取图片,显示到控件上,Hanlder,postion 38 * * 向内存存一份 39 * * 向本地文件中存一份 40 * 41 * @param imageUrl 42 * @param position 43 * @return 44 */ 45 public Bitmap getBitmap(String imageUrl, int position) { 46 //1.从内存中取图片 47 if (memoryCacheUtils != null) { 48 Bitmap bitmap = memoryCacheUtils.getBitmapFromUrl(imageUrl); 49 if (bitmap != null) { 50 LogUtil.e("内存加载图片成功=="+position); 51 return bitmap; 52 } 53 } 54 55 //2.从本地文件中取图片 56 if (localCacheUtils != null) { 57 Bitmap bitmap = localCacheUtils.getBitmapFromUrl(imageUrl); 58 if (bitmap != null) { 59 LogUtil.e("本地加载图片成功=="+position); 60 return bitmap; 61 } 62 } 63 64 //3.请求网络图片 65 netCacheUtils.getBitmapFomNet(imageUrl, position); 66 return null; 67 } 68 }