Android第十六天圖片載入框架
Android載入圖片快取原則:
Android對於記憶體一定要倍加的珍惜使用
圖片在載入的時候一般採用
三級快取原則:
第一級 :記憶體 臨時存貯
第二級 :磁碟(檔案/SD卡)持久化儲存
第三級 :網路 持久化
當想要載入一張圖片的時候,首先去記憶體中查詢,這張圖片是否存在,如果存在,則直接載入,如果不存在去磁碟中查詢,如果磁碟中存在,則將圖片載入到記憶體中,並且展示出來,如果磁碟中不存在,則從網路獲取,當下載成功後,存入磁碟中一份,載入到記憶體中,並且展示出來。
優勢:1.載入更快捷
2.節省流量開銷
3.減小伺服器壓力
一、Picasso - square公司出品。
A powerful image downloading and caching library for Android
Picasso的主要特性:1.自帶記憶體和硬碟的二級快取功能2.載入本地資源、資產(assets)、SD卡 及COntentProvider中的圖片3.在Adapter中需要取消已經不在視野範圍的ImageView 圖片資源的載入,否則會導致圖片錯位,Picasso解決了這個錯位的問題4.使用圖片壓縮儘可能的減少記憶體消耗5.圖形轉換操作,如變換大小,旋轉等,提供了藉口讓使用者可以自定義轉換操作Picasso的基本用法
倒入Picasso 框架依賴。
compile 'com.squareup.picasso:picasso:2.5.2'
基本語法:Picasso在(with)當前上下文中載入(load)一張圖片到(into)ImageView控制元件Bitmap.Config.ARGB_8888 1個畫素點就是 8×4= 32位 /8 = 4(b 位元組)
Bitmap.Config.ARGB_4444 1個畫素點就是 4×4= 16位 /8 = 2(b 位元組)
Bitmap.Config.RGB_565 1個畫素點就是 5+6+5 = 16位 /8 =2(b 位元組)
A R G B
A = alpha 透明度
R = Red 紅色
G = Green 綠色
B = Blue 藍色
後面的尾數表示的是 8888 每個畫素點佔的位數
假設 有一張圖片是 600× 800 大小
如果畫素格式是ARGB_8888 600×800×4 = 1920000 位元組 = 1920kb = 1.857M如果畫素格式是ARGB_4444 600×800×2 = 960000 位元組= 960kb = 0.9M
如果畫素格式是RGB_565 600×800×2 = 960000 位元組 =960kb = 0.9M
8.檢視大圖時放棄記憶體快取memorycachePicasso預設會使用裝置的15%的記憶體作為記憶體圖片快取,且現有的API無法清空記憶體快取。在檢視大圖時放棄使用記憶體快取,圖片從網路下載完成後會自動快取到磁碟中,載入會從磁碟中載入,這樣可以加速記憶體回收Picasso.with(Conetxt).load(url).memoryPolicy(MemoruPolicy.NO_CACHE,MemoryPolicy.NO_STORE).into(ImageView);<備註> :NO_CACHE 是指圖片載入跳過從記憶體快取中查詢NO_STORE 是指圖片儲存時不往記憶體快取儲存9. 設定tag標籤 設定網路請求的Tag對於每次載入圖片的時候,請求可以進行 一下三種動作(類似okHttp)通過設定tag標籤,可以用來pause、resume、cancel訪問Picasso.with(Context).pauseTag("”);Picasso.with(Context).resumeTag("”);Picasso.with(Context).cancelTag("”);Application中常見的設定初始化Picasso屬性:Picasso picasso = newPicasso.Builder(this)//設定記憶體快取大小,10M.memoryCache(new LruCache(10 << 20)) //設定下載的圖片格式,節省記憶體 預設時 ARGB_8888.defaultBitmapConfig(Bitmap.Config.RGB_565)//設定下載器, 預設是URLConnection.downLoader(new UrlConnectionDownloader())//.downLoader(new OkHttpDownloader())//設定除錯標示 (圖片左上角的三角形標記)紅色代表從網路獲取的資料藍色代表從本地磁碟中獲取的資料綠色代表從記憶體中獲取的資料.indicatorsEnabled(true).build();//設定Picasso單例模式Picasso.setSingletonInstance(picasso);
二、Glide
An image loading and caching library for Android focused on smooth scrolling
Glide4.0及以上版本需要AndroidStudio3.0以上版本支援所以我們採用Glide3.6版本的Jar包
倒入方式:
然後選擇:ProjectStructrue:
進行新增jar包的配置:
然後繼續選擇:
然後就可以正常使用了。
語法與Picasso基本一致。
三、Universal-Image-Loader
倒入依賴方式,在Gradle檔案裡面新增
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
在使用ImageLoader前:
//初始化ImageLoader
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(configuration);
使用:
//使用 ImageLoader載入圖片
ImageLoader loader = ImageLoader.getInstance();
loader.displayImage(IMAGELOADER_IMAGE_URL, showIv);
//引數配置
DisplayImageOptions options = new DisplayImageOptions.Builder().build();
//Loader 的監聽器
loader.displayImage(IMAGELOADER_IMAGE_URL, showIv, options, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
//監聽進度
}
});