Android图片缓存之Lru算法
Lru:
LRU是Least Recently Used 的缩写,翻译过来就是“最近最少使用”,LRU缓存就是使用这种原理实现,简单的说就是缓存一定量的数据,当超过设定的阈值时就把一些过期的数据删除掉,比如我们缓存10000条数据,当数据小于10000时可以随意添加,当超过10000时就需要把新的数据添加进来,同时要把过期数据删除,以确保我们最大缓存10000条,那怎么确定删除哪条过期数据呢,采用LRU算法实现的话就是将最老的数据删掉。
基于LruCache实现内存缓存:
1.)初始化MemoryCache
这里内存缓存的是Drawable 而不是Bitmap 理由是Drawable相对Bitmap来说有很大的内存优势
![](https://img.haomeiwen.com/i365033/70d0e6adbbdb5e27.gif)
intmaxMemory = (int) Runtime.getRuntime().maxMemory();//获取系统分配给应用的总内存大小intmCacheSize = maxMemory / 8;//设置图片内存缓存占用八分之一mMemoryCache =newLruCache(mCacheSize) {//必须重写此方法,来测量Bitmap的大小@OverrideprotectedintsizeOf(String key, Drawable value) {if(valueinstanceofBitmapDrawable) {
Bitmap bitmap=((BitmapDrawable) value).getBitmap();returnbitmap ==null? 0: bitmap.getByteCount();
}returnsuper.sizeOf(key, value);
}
};
![](https://img.haomeiwen.com/i365033/c74be060e7395b34.gif)
2.)添加一个Drawable到内存缓存
![](https://img.haomeiwen.com/i365033/5f175a409f70b05a.gif)
/*** 添加Drawable到内存缓存
*
*@paramkey
*@paramdrawable*/privatevoidaddDrawableToMemoryCache(String key, Drawable drawable) {if(getDrawableFromMemCache(key) ==null&& drawable !=null) {
mMemoryCache.put(key, drawable);
}
}
![](https://img.haomeiwen.com/i365033/ad7d8d0f7848dfb5.gif)
3.)从内存缓存中获取一个Drawable
![](https://img.haomeiwen.com/i365033/7b93f1235e92e8bb.gif)
/*** 从内存缓存中获取一个Drawable
*
*@paramkey
*@return*/publicDrawable getDrawableFromMemCache(String key) {returnmMemoryCache.get(key);
}
![](https://img.haomeiwen.com/i365033/a6ac41524837092f.gif)
4.)从内存缓存中移除一个Drawable
![](https://img.haomeiwen.com/i365033/0b0b1f1b644dc484.gif)
/*** 从内存缓存中移除
*
*@paramkey*/publicvoidremoveCacheFromMemory(String key) {
mMemoryCache.remove(key);
}
![](https://img.haomeiwen.com/i365033/f1d67f218c13c493.gif)
5.)清空内存缓存
/*** 清理内存缓存*/publicvoidcleanMemoryCCache() {
mMemoryCache.evictAll();
}
其实Lru缓存机制本质上就是存储在一个LinkedHashMap存储,为了保障插入的数据顺序,方便清理。
基于DiskLruCache实现磁盘缓存:
DiskLruCache类并不是谷歌官方实现,需要自行下载,下载地址:https://github.com/JakeWharton/DiskLruCache
1.)初始化DiskLruCache
![](https://img.haomeiwen.com/i365033/8590b8d09d07fe77.gif)
File cacheDir = context.getCacheDir();//指定的是数据的缓存地址longdiskCacheSize = 1024 * 1024 * 30;//最多可以缓存多少字节的数据intappVersion = DiskLruUtils.getAppVersion(context);//指定当前应用程序的版本号intvalueCount = 1;//指定同一个key可以对应多少个缓存文件try{
mDiskCache=DiskLruCache.open(cacheDir, appVersion, valueCount, diskCacheSize);
}catch(Exception ex) {
}
![](https://img.haomeiwen.com/i365033/79dc3b4b65b3b3fb.gif)
2.)写入一个文件到磁盘缓存
![](https://img.haomeiwen.com/i365033/f1242d363c045e44.gif)
/*** 添加Bitmap到磁盘缓存
*
*@paramkey
*@paramvalue*/privatevoidaddBitmapToDiskCache(String key,byte[] value) {
OutputStream out=null;try{
DiskLruCache.Editor editor=mDiskCache.edit(key);if(editor !=null) {
out= editor.newOutputStream(0);if(value !=null&& value.length > 0) {
out.write(value);
out.flush();
editor.commit();
}else{
editor.abort();
}
}
mDiskCache.flush();
}catch(IOException e) {
e.printStackTrace();
}finally{
DiskLruUtils.closeQuietly(out);
}
}
![](https://img.haomeiwen.com/i365033/1550bb7e41c36628.gif)
3.)从磁盘缓存中读取Drawable
![](https://img.haomeiwen.com/i365033/6f46f69582f4050c.gif)
/*** 从磁盘缓存中获取一个Drawable
*
*@paramkey
*@return*/publicDrawable getDrawableFromDiskCache(String key) {try{
DiskLruCache.Snapshot snapShot=mDiskCache.get(key);if(snapShot !=null) {
InputStream is= snapShot.getInputStream(0);
Bitmap bitmap=BitmapFactory.decodeStream(is);
Drawable drawable=DiskLruUtils.bitmap2Drawable(bitmap);//从磁盘中读取到之后 加入内存缓存addDrawableToMemoryCache(key, drawable);returndrawable;
}
}catch(IOException e) {
e.printStackTrace();
}returnnull;
}
![](https://img.haomeiwen.com/i365033/9d5fce8e6c3749dd.gif)
4.)从磁盘缓存中移除
![](https://img.haomeiwen.com/i365033/58696b0b416946d4.gif)
/*** 从磁盘缓存中移除
*
*@paramkey*/publicvoidremoveCacheFromDisk(String key) {try{
mDiskCache.remove(key);
}catch(Exception e) {
}
}
![](https://img.haomeiwen.com/i365033/946f6f9bb810ba4e.gif)
5.)清空磁盘缓存
![](https://img.haomeiwen.com/i365033/fc8b8fb2b46831f0.gif)
/*** 清理磁盘缓存*/publicvoidcleanDiskCache() {try{
mDiskCache.delete();
}catch(Exception e) {
}
}
![](https://img.haomeiwen.com/i365033/52f29218e7aa1902.gif)
图片下载过程:
接下来实例中用到了一点RxJava的知识有不了解RxJava的请自行了解一下。
1.)采用异步方式操作磁盘缓存和网络下载, 内存缓存可以在主线程中操作
![](https://img.haomeiwen.com/i365033/40ceff8fb15ea7f9.gif)
publicvoiddisPlay(finalImageView imageView, String imageUrl) {//生成唯一keyfinalString key =DiskLruUtils.hashKeyForDisk(imageUrl);//先从内存中读取Drawable drawableFromMemCache =getDrawableFromMemCache(key);if(drawableFromMemCache !=null) {
imageView.setImageDrawable(drawableFromMemCache);return;
}
Observable.just(imageUrl)
.map(newFunc1() {
@OverridepublicDrawable call(String imageUrl) {//参数类型 String//从磁盘中读取Drawable drawableFromDiskCache =getDrawableFromDiskCache(key);if(drawableFromDiskCache !=null) {returndrawableFromDiskCache;
}//网络下载returndownload(imageUrl);//返回类型 Drawable}
})
.subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程.observeOn(AndroidSchedulers.mainThread())//指定 Subscriber 的回调发生在主线程.subscribe(newAction1() {
@Overridepublicvoidcall(Drawable drawable) {//参数类型 DrawableimageView.setImageDrawable(drawable);
}
});
}
![](https://img.haomeiwen.com/i365033/d3574e0d46d1f78b.gif)
2.)下载图片过程以及处理
![](https://img.haomeiwen.com/i365033/0a3d6c76c9d201e3.gif)
privateDrawable download(String imageUrl) {
HttpURLConnection urlConnection=null;
ByteArrayOutputStream bos=null;
InputStream ins=null;try{finalURL url =newURL(imageUrl);
urlConnection=(HttpURLConnection) url.openConnection();
ins=urlConnection.getInputStream();
bos=newByteArrayOutputStream();intb;while((b = ins.read()) != -1) {
bos.write(b);
}
bos.flush();byte[] bytes =bos.toByteArray();
Bitmap bitmap=DiskLruUtils.bytes2Bitmap(bytes);
String key=DiskLruUtils.hashKeyForDisk(imageUrl);
Drawable drawable=DiskLruUtils.bitmap2Drawable(bitmap);//加入内存缓存addDrawableToMemoryCache(key, drawable);//加入磁盘缓存addBitmapToDiskCache(key, bytes);returndrawable;
}catch(IOException e) {
e.printStackTrace();
}finally{if(urlConnection !=null) {
urlConnection.disconnect();
}
DiskLruUtils.closeQuietly(bos);
DiskLruUtils.closeQuietly(ins);
}returnnull;
}
![](https://img.haomeiwen.com/i365033/d0db55bdfe0fb43d.gif)
附上最终图片缓存单例简单实现全部代码以及DiskLruUtils工具类代码
ImageLoadManager.java
ImageLoadManager.java
![](https://img.haomeiwen.com/i365033/66ba3119272728c4.gif)
DiskLruUtils.java
DiskLruUtils.java
![](https://img.haomeiwen.com/i365033/f49409514fcbe9b4.gif)
总结:
以上就是基于Lru图片缓存简单实现