Android Imageloader的配置的實(shí)現(xiàn)代碼
Android Imageloader的配置的實(shí)現(xiàn)代碼
ImageLoader 優(yōu)點(diǎn)
(1) 支持下載進(jìn)度監(jiān)聽
(2) 可以在 View 滾動(dòng)中暫停圖片加載
通過 PauseOnScrollListener 接口可以在 View 滾動(dòng)中暫停圖片加載。
(3) 默認(rèn)實(shí)現(xiàn)多種內(nèi)存緩存算法 這幾個(gè)圖片緩存都可以配置緩存算法,不過 ImageLoader 默認(rèn)實(shí)現(xiàn)了較多緩存算法,如 Size
最大先刪除、使用最少先刪除、最近最少使用、先進(jìn)先刪除、時(shí)間最長先刪除等。
(4) 支持本地緩存文件名規(guī)則定義
實(shí)現(xiàn)代碼:
/**
* 初始化ImageLoader
*/
public static void initImageLoader(Context context) {
File cacheDir = StorageUtils.getOwnCacheDirectory(context,
"bee_k77/Cache");// 獲取到緩存的目錄地址
Log.e("cacheDir", cacheDir.getPath());
// 創(chuàng)建配置ImageLoader(所有的選項(xiàng)都是可選的,只使用那些你真的想定制),這個(gè)可以設(shè)定在APPLACATION里面,設(shè)置為全局的配置參數(shù)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context)
// max width, max height,即保存的每個(gè)緩存文件的最大長寬
.memoryCacheExtraOptions(480, 800)
// Can slow ImageLoader, use it carefully (Better don't use it)設(shè)置緩存的詳細(xì)信息,最好不要設(shè)置這個(gè)
/ .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
// 線程池內(nèi)加載的數(shù)量
.threadPoolSize(3)
// 線程優(yōu)先級(jí)
.threadPriority(Thread.NORM_PRIORITY - 2)
/*
* When you display an image in a small ImageView
* and later you try to display this image (from identical URI) in a larger ImageView
* so decoded image of bigger size will be cached in memory as a previous decoded image of smaller size.
* So the default behavior is to allow to cache multiple sizes of one image in memory.
* You can deny it by calling this method:
* so when some image will be cached in memory then previous cached size of this image (if it exists)
* will be removed from memory cache before.
*/
/ .denyCacheImageMultipleSizesInMemory()
// You can pass your own memory cache implementation你可以通過自己的內(nèi)存緩存實(shí)現(xiàn)
// .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
// .memoryCacheSize(2 * 1024 * 1024)
//硬盤緩存50MB
.diskCacheSize(50 * 1024 * 1024)
//將保存的時(shí)候的URI名稱用MD5
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
// 加密
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())//將保存的時(shí)候的URI名稱用HASHCODE加密
.tasksProcessingOrder(QueueProcessingType.LIFO)
.diskCacheFileCount(100) //緩存的File數(shù)量
.diskCache(new UnlimitedDiscCache(cacheDir))// 自定義緩存路徑
// .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
// .imageDownloader(new BaseImageDownloader(context, 5 * 1000,
// 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超時(shí)時(shí)間
.writeDebugLogs() // Remove for release app
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);// 全局初始化此配置
}
Option類
package com.topnews.config;
import android.graphics.Bitmap;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.topnews.R;
public class Options {
/**
* 新聞列表中用到的圖片加載配置
*/
public static DisplayImageOptions getListOptions() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
// 設(shè)置圖片在下載期間顯示的圖片
.showImageOnLoading(R.drawable.ic_stub)
// 設(shè)置圖片Uri為空或是錯(cuò)誤的時(shí)候顯示的圖片
.showImageForEmptyUri(R.drawable.ic_stub)
// 設(shè)置圖片加載/解碼過程中錯(cuò)誤時(shí)候顯示的圖片
.showImageOnFail(R.drawable.ic_error)
// 設(shè)置下載的圖片是否緩存在內(nèi)存中
.cacheInMemory(false)
// 設(shè)置下載的圖片是否緩存在SD卡中
.cacheOnDisc(true)
// 保留Exif信息
.considerExifParams(true)
// 設(shè)置圖片以如何的編碼方式顯示
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
// 設(shè)置圖片的解碼類型
.bitmapConfig(Bitmap.Config.RGB_565)
// .decodingOptions(android.graphics.BitmapFactory.Options
// decodingOptions)//設(shè)置圖片的解碼配置
.considerExifParams(true)
// 設(shè)置圖片下載前的延遲
.delayBeforeLoading(100)// int
// delayInMillis為你設(shè)置的延遲時(shí)間
// 設(shè)置圖片加入緩存前,對(duì)bitmap進(jìn)行設(shè)置
// .preProcessor(BitmapProcessor preProcessor)
.resetViewBeforeLoading(true)// 設(shè)置圖片在下載前是否重置,復(fù)位
// .displayer(new RoundedBitmapDisplayer(20))//是否設(shè)置為圓角,弧度為多少
.displayer(new FadeInBitmapDisplayer(100))// 淡入
.build();
return options;
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android ListView實(shí)現(xiàn)ImageLoader圖片加載的方法
- Android Universal ImageLoader 緩存圖片
- Android ImageLoader第三方框架解析
- Android開發(fā)之ImageLoader本地緩存
- Android開發(fā)之ImageLoader使用詳解
- Android圖片加載的緩存類
- 非常實(shí)用的Android圖片工具類
- Android開發(fā)之多媒體文件獲取工具類實(shí)例【音頻,視頻,圖片等】
- Android開發(fā)之超強(qiáng)圖片工具類BitmapUtil完整實(shí)例
- Android開發(fā)之圖片壓縮工具類完整實(shí)例
- Android編程圖片加載類ImageLoader定義與用法實(shí)例分析
相關(guān)文章
Android開發(fā)中ImageLoder進(jìn)行圖片加載和緩存
這篇文章主要介紹了Android開發(fā)中ImageLoder進(jìn)行圖片加載和緩存的相關(guān)資料,需要的朋友可以參考下2016-04-04
Android逐幀動(dòng)畫實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android逐幀動(dòng)畫實(shí)現(xiàn)代碼,可以通過xml或java代碼實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android WebView開發(fā)之WebView與Native交互
隨著H5的廣泛使用,Android開發(fā)過程中免不了會(huì)使用網(wǎng)頁來做展示,那么web與native之間的通信就顯得尤其重要了,其實(shí)際上是JavaScript與java之間的通信。本文將為大家詳細(xì)介紹二者是如何實(shí)現(xiàn)交互的,需要的朋友可以參考一下2021-12-12
基于Android中實(shí)現(xiàn)定時(shí)器的3種解決方法
2013-05-05
Android自定義View新年煙花、祝福語橫幅動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android自定義View新年煙花、祝福語橫幅動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Android徹底清除APP數(shù)據(jù)的兩種方案總結(jié)
大家在用Android手機(jī)的時(shí)候肯定都遇到過內(nèi)存剩余空間越來越小的情況,所以下面這篇文章主要給大家介紹了關(guān)于Android徹底清除APP數(shù)據(jù)的兩種方案,需要的朋友可以參考下2021-11-11
android自動(dòng)工具類TextUtils使用詳解
這篇文章主要介紹了android自動(dòng)工具類TextUtils的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android判斷是否有拍照權(quán)限的實(shí)例代碼
android在開發(fā)中有時(shí)候要判斷應(yīng)用中是否有某項(xiàng)權(quán)限,下面通過本文給大家分享Android判斷是否有拍照權(quán)限的實(shí)例代碼,需要的的朋友參考下吧2017-07-07

