1.在activity中书写imageview的控件
2.调用工具类BitmapUtils;
在工具类中具体实现三级缓存的原理展示代码如下:
Context context;
//图片本地缓存路径
private final static String SDCARD_CACHE = Environment.getExternalStorageDirectory() + "/imagecache";
//图片存放文件夹
File fileDir = new File(SDCARD_CACHE);
private Map<String, SoftReference<Bitmap>> map = new HashMap<String, SoftReference<Bitmap>>();
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
ImageViewToBitmap imageViewToBitmap = (ImageViewToBitmap) msg.obj;
imageViewToBitmap.iv.setImageBitmap(imageViewToBitmap.bitmap);
break;
}
}
};
//构造方法
public BitmapUtils(Context context) {
this.context = context;
if (!fileDir.exists()) {
fileDir.mkdirs();
}
}
//加载图片的方法
public void display(ImageView iv, String url) {
//内存缓存
Bitmap bitmap = loadMemory(url);
if (bitmap != null) {
iv.setImageBitmap(bitmap);
} else {
//sdcard缓存
bitmap = loadSD(url);
if (bitmap != null) {
iv.s