Android圖片緩存之Lru算法(二)
前言:
上篇我們總結(jié)了Bitmap的處理,同時對比了各種處理的效率以及對內(nèi)存占用大小,點擊查看。我們得知一個應(yīng)用如果使用大量圖片就會導致OOM(out of memory),那該如何處理才能近可能的降低oom發(fā)生的概率呢?之前我們一直在使用SoftReference軟引用,SoftReference是一種現(xiàn)在已經(jīng)不再推薦使用的方式,因為從 Android 2.3 (API Level 9)開始,垃圾回收器會更傾向于回收持有軟引用或弱引用的對象,這讓軟引用變得不再可靠,所以今天我們來認識一種新的緩存處理算法Lru,然后學習一下基于Lru的Lrucache、DiskLruCache 實現(xiàn)我們的圖片緩存。
Lru:
LRU是Least Recently Used 的縮寫,翻譯過來就是“最近最少使用”,LRU緩存就是使用這種原理實現(xiàn),簡單的說就是緩存一定量的數(shù)據(jù),當超過設(shè)定的閾值時就把一些過期的數(shù)據(jù)刪除掉,比如我們緩存10000條數(shù)據(jù),當數(shù)據(jù)小于10000時可以隨意添加,當超過10000時就需要把新的數(shù)據(jù)添加進來,同時要把過期數(shù)據(jù)刪除,以確保我們最大緩存10000條,那怎么確定刪除哪條過期數(shù)據(jù)呢,采用LRU算法實現(xiàn)的話就是將最老的數(shù)據(jù)刪掉。
基于LruCache實現(xiàn)內(nèi)存緩存:
1.)初始化MemoryCache
這里內(nèi)存緩存的是Drawable 而不是Bitmap 理由是Drawable相對Bitmap來說有很大的內(nèi)存優(yōu)勢
int maxMemory = (int) Runtime.getRuntime().maxMemory();//獲取系統(tǒng)分配給應(yīng)用的總內(nèi)存大小 int mCacheSize = maxMemory / 8;//設(shè)置圖片內(nèi)存緩存占用八分之一 mMemoryCache = new LruCache<String, Drawable>(mCacheSize) { //必須重寫此方法,來測量Bitmap的大小 @Override protected int sizeOf(String key, Drawable value) { if (value instanceof BitmapDrawable) { Bitmap bitmap = ((BitmapDrawable) value).getBitmap(); return bitmap == null ? 0 : bitmap.getByteCount(); } return super.sizeOf(key, value); } };
2.)添加一個Drawable到內(nèi)存緩存
/** * 添加Drawable到內(nèi)存緩存 * * @param key * @param drawable */ private void addDrawableToMemoryCache(String key, Drawable drawable) { if (getDrawableFromMemCache(key) == null && drawable != null) { mMemoryCache.put(key, drawable); } }
3.)從內(nèi)存緩存中獲取一個Drawable
/** * 從內(nèi)存緩存中獲取一個Drawable * * @param key * @return */ public Drawable getDrawableFromMemCache(String key) { return mMemoryCache.get(key); }
4.)從內(nèi)存緩存中移除一個Drawable
/** * 從內(nèi)存緩存中移除 * * @param key */ public void removeCacheFromMemory(String key) { mMemoryCache.remove(key); }
5.)清空內(nèi)存緩存
/** * 清理內(nèi)存緩存 */ public void cleanMemoryCCache() { mMemoryCache.evictAll(); }
其實Lru緩存機制本質(zhì)上就是存儲在一個LinkedHashMap存儲,為了保障插入的數(shù)據(jù)順序,方便清理。
基于DiskLruCache實現(xiàn)磁盤緩存:
DiskLruCache類并不是谷歌官方實現(xiàn),需要自行下載,下載地址:https://github.com/JakeWharton/DiskLruCache
1.)初始化DiskLruCache
File cacheDir = context.getCacheDir();//指定的是數(shù)據(jù)的緩存地址 long diskCacheSize = 1024 * 1024 * 30;//最多可以緩存多少字節(jié)的數(shù)據(jù) int appVersion = DiskLruUtils.getAppVersion(context);//指定當前應(yīng)用程序的版本號 int valueCount = 1;//指定同一個key可以對應(yīng)多少個緩存文件 try { mDiskCache = DiskLruCache.open(cacheDir, appVersion, valueCount, diskCacheSize); } catch (Exception ex) { }
2.)寫入一個文件到磁盤緩存
/** * 添加Bitmap到磁盤緩存 * * @param key * @param value */ private void addBitmapToDiskCache(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); } }
3.)從磁盤緩存中讀取Drawable
/** * 從磁盤緩存中獲取一個Drawable * * @param key * @return */ public Drawable 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); //從磁盤中讀取到之后 加入內(nèi)存緩存 addDrawableToMemoryCache(key, drawable); return drawable; } } catch (IOException e) { e.printStackTrace(); } return null; }
4.)從磁盤緩存中移除
/** * 從磁盤緩存中移除 * * @param key */ public void removeCacheFromDisk(String key) { try { mDiskCache.remove(key); } catch (Exception e) { } }
5.)清空磁盤緩存
/** * 清理磁盤緩存 */ public void cleanDiskCache() { try { mDiskCache.delete(); } catch (Exception e) { } }
圖片下載過程:
接下來實例中用到了一點RxJava的知識有不了解RxJava的請自行了解一下。
1.)采用異步方式操作磁盤緩存和網(wǎng)絡(luò)下載, 內(nèi)存緩存可以在主線程中操作
public void disPlay(final ImageView imageView, String imageUrl) { //生成唯一key final String key = DiskLruUtils.hashKeyForDisk(imageUrl); //先從內(nèi)存中讀取 Drawable drawableFromMemCache = getDrawableFromMemCache(key); if (drawableFromMemCache != null) { imageView.setImageDrawable(drawableFromMemCache); return; } Observable.just(imageUrl) .map(new Func1<String, Drawable>() { @Override public Drawable call(String imageUrl) { // 參數(shù)類型 String //從磁盤中讀取 Drawable drawableFromDiskCache = getDrawableFromDiskCache(key); if (drawableFromDiskCache != null) { return drawableFromDiskCache; } //網(wǎng)絡(luò)下載 return download(imageUrl); // 返回類型 Drawable } }) .subscribeOn(Schedulers.io()) // 指定 subscribe() 發(fā)生在 IO 線程 .observeOn(AndroidSchedulers.mainThread()) // 指定 Subscriber 的回調(diào)發(fā)生在主線程 .subscribe(new Action1<Drawable>() { @Override public void call(Drawable drawable) { // 參數(shù)類型 Drawable imageView.setImageDrawable(drawable); } }); }
2.)下載圖片過程以及處理
private Drawable download(String imageUrl) { HttpURLConnection urlConnection = null; ByteArrayOutputStream bos = null; InputStream ins = null; try { final URL url = new URL(imageUrl); urlConnection = (HttpURLConnection) url.openConnection(); ins = urlConnection.getInputStream(); bos = new ByteArrayOutputStream(); int b; 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); //加入內(nèi)存緩存 addDrawableToMemoryCache(key, drawable); //加入磁盤緩存 addBitmapToDiskCache(key, bytes); return drawable; } catch (IOException e) { e.printStackTrace(); } finally { if (urlConnection != null) { urlConnection.disconnect(); } DiskLruUtils.closeQuietly(bos); DiskLruUtils.closeQuietly(ins); } return null; }
附上最終圖片緩存單例簡單實現(xiàn)全部代碼以及DiskLruUtils工具類代碼
ImageLoadManager.java
public class ImageLoadManager { private LruCache<String, Drawable> mMemoryCache;//內(nèi)存緩存 private DiskLruCache mDiskCache;//磁盤緩存 private static ImageLoadManager mInstance;//獲取圖片下載單例引用 /** * 構(gòu)造器 * * @param context */ private ImageLoadManager(Context context) { int maxMemory = (int) Runtime.getRuntime().maxMemory();//獲取系統(tǒng)分配給應(yīng)用的總內(nèi)存大小 int mCacheSize = maxMemory / 8;//設(shè)置圖片內(nèi)存緩存占用八分之一 mMemoryCache = new LruCache<String, Drawable>(mCacheSize) { //必須重寫此方法,來測量Bitmap的大小 @Override protected int sizeOf(String key, Drawable value) { if (value instanceof BitmapDrawable) { Bitmap bitmap = ((BitmapDrawable) value).getBitmap(); return bitmap == null ? 0 : bitmap.getByteCount(); } return super.sizeOf(key, value); } }; File cacheDir = context.getCacheDir();//指定的是數(shù)據(jù)的緩存地址 long diskCacheSize = 1024 * 1024 * 30;//最多可以緩存多少字節(jié)的數(shù)據(jù) int appVersion = DiskLruUtils.getAppVersion(context);//指定當前應(yīng)用程序的版本號 int valueCount = 1;//指定同一個key可以對應(yīng)多少個緩存文件 try { mDiskCache = DiskLruCache.open(cacheDir, appVersion, valueCount, diskCacheSize); } catch (Exception ex) { } } /** * 獲取單例引用 * * @return */ public static ImageLoadManager getInstance(Context context) { ImageLoadManager inst = mInstance; if (inst == null) { synchronized (RequestManager.class) { inst = mInstance; if (inst == null) { inst = new ImageLoadManager(context.getApplicationContext()); mInstance = inst; } } } return inst; } public void disPlay(final ImageView imageView, String imageUrl) { //生成唯一key final String key = DiskLruUtils.hashKeyForDisk(imageUrl); //先從內(nèi)存中讀取 Drawable drawableFromMemCache = getDrawableFromMemCache(key); if (drawableFromMemCache != null) { imageView.setImageDrawable(drawableFromMemCache); return; } Observable.just(imageUrl) .map(new Func1<String, Drawable>() { @Override public Drawable call(String imageUrl) { // 參數(shù)類型 String //從磁盤中讀取 Drawable drawableFromDiskCache = getDrawableFromDiskCache(key); if (drawableFromDiskCache != null) { return drawableFromDiskCache; } //網(wǎng)絡(luò)下載 return download(imageUrl); // 返回類型 Drawable } }) .subscribeOn(Schedulers.io()) // 指定 subscribe() 發(fā)生在 IO 線程 .observeOn(AndroidSchedulers.mainThread()) // 指定 Subscriber 的回調(diào)發(fā)生在主線程 .subscribe(new Action1<Drawable>() { @Override public void call(Drawable drawable) { // 參數(shù)類型 Drawable imageView.setImageDrawable(drawable); } }); } /** * 添加Drawable到內(nèi)存緩存 * * @param key * @param drawable */ private void addDrawableToMemoryCache(String key, Drawable drawable) { if (getDrawableFromMemCache(key) == null && drawable != null) { mMemoryCache.put(key, drawable); } } /** * 從內(nèi)存緩存中獲取一個Drawable * * @param key * @return */ public Drawable getDrawableFromMemCache(String key) { return mMemoryCache.get(key); } /** * 從磁盤緩存中獲取一個Drawable * * @param key * @return */ public Drawable 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); //從磁盤中讀取到之后 加入內(nèi)存緩存 addDrawableToMemoryCache(key, drawable); return drawable; } } catch (IOException e) { e.printStackTrace(); } return null; } /** * 添加Bitmap到磁盤緩存 * * @param key * @param value */ private void addBitmapToDiskCache(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); } } private Drawable download(String imageUrl) { HttpURLConnection urlConnection = null; ByteArrayOutputStream bos = null; InputStream ins = null; try { final URL url = new URL(imageUrl); urlConnection = (HttpURLConnection) url.openConnection(); ins = urlConnection.getInputStream(); bos = new ByteArrayOutputStream(); int b; 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); //加入內(nèi)存緩存 // addDrawableToMemoryCache(key, drawable); //加入磁盤緩存 addBitmapToDiskCache(key, bytes); return drawable; } catch (IOException e) { e.printStackTrace(); } finally { if (urlConnection != null) { urlConnection.disconnect(); } DiskLruUtils.closeQuietly(bos); DiskLruUtils.closeQuietly(ins); } return null; } /** * 從緩存中移除 * * @param key */ public void removeCache(String key) { removeCacheFromMemory(key); removeCacheFromDisk(key); } /** * 從內(nèi)存緩存中移除 * * @param key */ public void removeCacheFromMemory(String key) { mMemoryCache.remove(key); } /** * 從磁盤緩存中移除 * * @param key */ public void removeCacheFromDisk(String key) { try { mDiskCache.remove(key); } catch (Exception e) { } } /** * 磁盤緩存大小 * * @return */ public long diskCacheSize() { return mDiskCache.size(); } /** * 內(nèi)存緩存大小 * * @return */ public long memoryCacheSize() { return mMemoryCache.size(); } /** * 關(guān)閉磁盤緩存 */ public void closeDiskCache() { try { mDiskCache.close(); } catch (Exception e) { } } /** * 清理緩存 */ public void cleanCache() { cleanMemoryCCache(); cleanDiskCache(); } /** * 清理磁盤緩存 */ public void cleanDiskCache() { try { mDiskCache.delete(); } catch (Exception e) { } } /** * 清理內(nèi)存緩存 */ public void cleanMemoryCCache() { mMemoryCache.evictAll(); } }
DiskLruUtils.java
final class DiskLruUtils { /** * 關(guān)閉輸入輸出流 */ public static void closeQuietly(/*Auto*/Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (RuntimeException rethrown) { throw rethrown; } catch (Exception ignored) { } } } /** * 獲取versionCode */ public static int getAppVersion(Context context) { try { PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); return info.versionCode; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return 1; } public static String hashKeyForDisk(String key) { String cacheKey; try { final MessageDigest mDigest = MessageDigest.getInstance("MD5"); mDigest.update(key.getBytes()); cacheKey = bytesToHexString(mDigest.digest()); } catch (NoSuchAlgorithmException e) { cacheKey = String.valueOf(key.hashCode()); } return cacheKey; } public static String bytesToHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(0xFF & bytes[i]); if (hex.length() == 1) { sb.append('0'); } sb.append(hex); } return sb.toString(); } /** * Bitmap → bytes */ public static byte[] bitmap2Bytes(Bitmap bm) { if (bm == null) { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } /** * bytes → Bitmap */ public static Bitmap bytes2Bitmap(byte[] bytes) { return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); } /** * Drawable → Bitmap */ public static Bitmap drawable2Bitmap(Drawable drawable) { if (drawable == null) { return null; } // 取 drawable 的長寬 int w = drawable.getIntrinsicWidth(); int h = drawable.getIntrinsicHeight(); // 取 drawable 的顏色格式 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; // 建立對應(yīng) bitmap Bitmap bitmap = Bitmap.createBitmap(w, h, config); // 建立對應(yīng) bitmap 的畫布 Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, w, h); // 把 drawable 內(nèi)容畫到畫布中 drawable.draw(canvas); return bitmap; } /* * Bitmap → Drawable */ public static Drawable bitmap2Drawable(Bitmap bm) { if (bm == null) { return null; } BitmapDrawable bd = new BitmapDrawable(bm); bd.setTargetDensity(bm.getDensity()); return new BitmapDrawable(bm); } }
以上就是基于Lru圖片緩存簡單實現(xiàn),希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中backgroundDimEnabled的作用
這篇文章主要介紹了Android中backgroundDimEnabled的作用的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10Android App中使用AudioManager類來編寫音頻播放器
這篇文章主要介紹了Android App中使用AudioManager類來編寫音樂播放器的方法,文中舉了一個簡單的例子實現(xiàn)了基礎(chǔ)的播放暫停和靜音等功能,需要的朋友可以參考下2016-04-04React Native開發(fā)中自動打包腳本的實例代碼
這篇文章主要介紹了React Native開發(fā)中自動打包腳本的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09Windows下搭建Flutter開發(fā)環(huán)境
這篇文章介紹了Windows下搭建Flutter開發(fā)環(huán)境的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-11-11android panellistview 圓角實現(xiàn)代碼
android panellistview 圓角是每一個android開發(fā)者都具備的一項,對于新手朋友來說可能有點難度,接下來將詳細介紹,需要了解的朋友可以參考下2012-12-12Android開源項目PullToRefresh下拉刷新功能詳解2
這篇文章主要為大家進一步的介紹了Android開源項目PullToRefresh下拉刷新功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09