最近在面試中,被頻繁詢問到關于Android圖片處理的問題,對于一個初級Android 開發者來說,只會使用,而不懂得原理是不行的。所以做一下記錄:
圖片緩存
Java中軟引用SoftReference
- Map<String,SoftReference<Bitmap>> imageCache
- 原理:對一個需要引用的對象不進行直接引用,而是通過應用一個特定的SoftReference對象,然后再由該對象類去引用實際的對象。
- 作用:被SoftReference對象引用的實際對象,在Java運行時,如果出現內存緊張的時候,會被適當的回收,釋放內存,軟引用對于內存較少的設備起到了對內存很好的利用率。
- 我們從Google官方的Caching Bitmaps了解到,該方案的緩存策略已經被棄用了,只適用于Android2.3之前的設備,因為從Android2.3開始,GC更傾向于回收軟、弱引用,這使得會出現以下這種情況,在Listview中使用軟引用緩存異步加載圖片的情況下,對Listview進行滑動時,還是會進行網絡請求,因為軟引用中的圖片被回收,所以無法命中緩存中的圖片。
Note: In the past, a popular memory cache implementation was a SoftReference or WeakReference bitmap cache, however this is not recommended. Starting from Android 2.3 (API Level 9) the garbage collector is more aggressive with collecting soft/weak references which makes them fairly ineffective. In addition, prior to Android 3.0 (API Level 11), the backing data of a bitmap was stored in native memory which is not released in a predictable manner, potentially causing an application to briefly exceed its memory limits and crash.
LruCache和LruMemoryCache
- LruMemoryCache<String, Bitmap> mMemoryCache = new LruMemoryCache<String, Bitmap>(0.25f)
- Google推薦使用的方案是:LruCache最少使用算法,當內存達到設定的最大值的時候,會將內存中最近最少使用的對象進行移除,避免OOM。
- LruMemoryCache比LruCache多增加了一個緩存超期的處理。
LruCache原理
1.LruCache中LRU算法的實現是通過一個LinkedHashMap來實現的。LinkedHashMap繼承于HashMap,使用一個雙向鏈表來存儲Map中的Enty順序關系。
2.當我們執行Get方法從LruCache中取出對象時,將該對象移動到鏈表的末端。
3.當我們執行Put方法從LruCache中增加對象時,插入對象并將對象移動到鏈表的末端。
4.當設備內存達到設定的最大值時,將鏈表頭部的對象,也就是最近最少使用的對象(最近最多使用的對象都被移動到鏈表的末端)移除。
圖片失真
.9.png圖片
- 對于本地圖片,我們采用.9圖片,是Android提供的一種特殊的圖片技術,將圖片橫向和縱向同時進行拉伸,以實現在多分辨率下的不失真的效果。
Options.inJustDecodeBounds
- 對于網絡中的圖片,我們可以采用壓縮圖片的方式。我們根據控件的大?。@示到屏幕上的大小)來縮放圖片的inSamplesize(ex:顯示圖片控件大小為12896像素,那么就不需要用到1024768像素)
- 注意:由于解碼會占用內存,通過設置options.inJustDecodeBounds為true,在進行解碼就不會申請內存創建Bitmap,會返回一個空的Bitmap,但是可以從中獲取到圖片的屬性。
計算壓縮圖片的比例
int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if(height > reqHeight || width > reqWidth){
final int heightRatio = Math.round((float)height/(float)reqHeigth);
final int widhtRatio = Math.round((float)width/(float)reqWidth);
inSampleSize = heightRatio < widthRadio ? heightRatio : widthRatio;
}
return inSampleSize;
}
對圖片進行壓縮處理
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath,options);
options.inSampleSize = calculateInSampleSize(options,480,800);
optons.inJustDecodeBounds = false;
return BtimapFactory.decodeFile(filePath,options);