此处只介绍使用 具体的看源码 注释的非常详细
引用次工具方法超级简单(功能及方法可不简单 具体实现可以看代码 源码在下面):
使用的地方加上如下方法:
ima = (ImageView) findViewById(R.id.imageView1);
new CacheImageAsyncTask(ima,this).execute("http://zhibo.tianyuan161.com/uploads/start_logo/2014/0429/d33063dfed90e18a8135156f97f02177.png");
解释工具类一共四个:
AsyncTask类中主要部分:
public Bitmap getBitmap(String url) {
///
// 先从内存缓存中获取图 如果没有再从缓存文件中读取 在没有就从网络上读取并保存
Bitmap result = memoryCache.getBitmapFromCache(url); if (result == null) { // 文件缓存中获取 result = fileCache.getImage(url);
if (result == null) { // 从网络获取 result = ImageGetFromHttp.downloadBitmap(url); if (result != null) { fileCache.saveBitmap(result, url); memoryCache.addBitmapToCache(url, result); } } else { // 添加到内存缓存 memoryCache.addBitmapToCache(url, result); } } return result;}protected
Bitmap doInBackground(String... params) {return getBitmap(params[0]);}
缓存类ImageFileCache:会判断SD卡空间 超过设置大小就会自动清理等
Memory类:主要就是软饮用和硬引用的结合使用 根据时间来自动处理等
顺便推荐另外一篇文章有关图片的: