- 添加依賴
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.4'
- 添權重限
如需要請求網絡圖檔,添加
<uses-permission android:name="android.permission.INTERNET" />
如需要SD卡緩存,添加
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- 初始化配置 , 在調用UIL前必須初始化配置
ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
ImageLoader.getInstance().init(configuration);
Configuration 參數詳解
File cacheDir = StorageUtils.getCacheDirectory(context); // 自定義緩存檔案夾
ImageLoaderConfiguration config =new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(480, 800) // 指定緩存到記憶體時圖檔的大小,預設是螢幕尺寸的長寬
.diskCacheExtraOptions(480, 800, null)// 指定緩存到硬碟時圖檔的大小,并不建議使用
.taskExecutor(new Executor()) // 自定義一個線程來加載和顯示圖檔
.taskExecutorForCachedImages(new Executor())// 自定義一個線程來緩存圖檔
.threadPoolSize(3) // default, 指定線程池大小
.threadPriority(Thread.NORM_PRIORITY-2) // default ,指定線程優先級
.tasksProcessingOrder(QueueProcessingType.FIFO) // default , 指定加載顯示圖檔的任務隊列的類型
.denyCacheImageMultipleSizesInMemory() // 禁止在記憶體中緩存同一張圖檔的多個尺寸類型
.memoryCache(new LruMemoryCache(2*1024*1024)) // 指定記憶體緩存的大小,預設值為1/8 應用的最大可用記憶體
.memoryCacheSize(2*1024*1024)
.memoryCacheSizePercentage(13) // default
.diskCache(new UnlimitedDiskCache(cacheDir)) // default , 指定硬碟緩存的位址
.diskCacheSize(50*1024*1024) // 指定硬碟緩存的大小
.diskCacheFileCount(100) // 指定硬碟緩存的檔案個數
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default , 指定硬碟緩存時檔案名的生成器
.imageDownloader(new BaseImageDownloader(context)) // default , 指定圖檔下載下傳器
.imageDecoder(new BaseImageDecoder()) // default , 指定圖檔解碼器
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default , 指定圖檔顯示的配置
.writeDebugLogs() // 是否顯示Log
.build();
Display Options 參數詳解
DisplayImageOptions options =new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub) // 圖檔正在加載時顯示的圖檔資源ID
.showImageForEmptyUri(R.drawable.ic_empty) // URI為空時顯示的圖檔資源ID
.showImageOnFail(R.drawable.ic_error) // 圖檔加載失敗時顯示的圖檔資源ID
.resetViewBeforeLoading(false) // default 圖檔在下載下傳前是否重置,複位
.delayBeforeLoading(1000) // 圖檔開始加載前的延時.預設是0
.cacheInMemory(false) // default , 是否緩存在記憶體中, 預設不緩存
.cacheOnDisk(false) // default , 是否緩存在硬碟 , 預設不緩存
.preProcessor(new BitmapProcessor) // 設定圖檔緩存在記憶體前的圖檔處理器
.postProcessor(new BitmapProcessor) // 設定圖檔在緩存到記憶體以後 , 顯示在界面之前的圖檔處理器
.extraForDownloader(...) // 為圖檔下載下傳設定輔助參數
.considerExifParams(false) // default , 設定是否考慮JPEG圖檔的EXIF參數資訊,預設不考慮
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default , 指定圖檔縮放的方式,ListView/GridView/Gallery推薦使用此預設值
.bitmapConfig(Bitmap.Config.ARGB_8888) // default , 指定圖檔的品質,預設是 ARGB_8888
.decodingOptions(...) // 指定圖檔的解碼方式
.displayer(new SimpleBitmapDisplayer()) // default , 設定圖檔顯示的方式,用于自定義
.handler(new Handler()) // default ,設定圖檔顯示的方式和ImageLoadingListener的監聽, 用于自定義
.build();
常用方法
顯示圖檔
ImageLoader.getInstance().loadImage(String uri, ImageLoadingListener listener)
SimpleImageLoadingListener是ImageLoadingListener的空實作
displayImage(String uri, ImageView imageView)
displayImage(String uri, ImageView imageView, DisplayImageOptions options)
displayImage(String uri, ImageView imageView, DisplayImageOptions options,
ImageLoadingListener listener, ImageLoadingProgressListener progressListener)
加載其他圖檔
// 圖檔來自本機檔案
String imageUrl = ImageDownloader.Scheme.FILE.wrap("/mnt/sdcard/image.png");
// 圖檔來自Content Provider
String contentURL = ImageDownloader.Scheme.CONTENT.wrap("/media/external/audio/albumart/13");
//圖檔來源于assets
String assetsUrl = ImageDownloader.Scheme.ASSETS.wrap("image.png");
//圖檔來源于
String drawableUrl = ImageDownloader.Scheme.DRAWABLE.wrap("R.drawable.image");
注意事項:
- 預設情況UIL是沒有緩存功能的 , 是以需要在構造DisplayImageOptions時打開緩存功能. cacheInMemory() cacheOnDisk()
- 如果要緩存圖檔到本地硬碟 , 不要忘記添加SD卡讀寫權限
- 如果發生OOM異常,參考如下處理
縮小線程池的大小, ImageLoaderConfiguration. threadPoolSize() , 推薦值為1-5
指定圖檔品質時,使用RGB_565, DisplayImageOptions. bitmapConfig(Bitmap.Config.RGB_565)
推薦使用DisplayImageOptions.imageScaleType(ImageScaleType.EXACTLY)
使用diskCacheExtraOptions時,推薦參數為ImageLoaderConfiguration.diskCacheExtraOptions(480, 320, null)
- UIL已實作的記憶體緩存配置類.用于ImageLoaderConfiguration memoryCache()
僅強引用 LruMemoryCache
強引用 + 弱引用
UsingFreqLimitedMemoryCache
LRULimitedMemoryCache
FIFOLimitedMemoryCache
LargestLimitedMemoryCache
LimitedAgeMemoryCache
僅弱引用 WeakMemoryCache
- UIL已實作的硬碟緩存配置類,用于ImageLoaderConfiguration diskCache()
(預設值)
UnlimitedDiscCache
LruDiskCache
LimitedAgeDiscCache
- UIL已實作的圖檔顯示類, 用于DisplayImageOptions displayer()
RoundedBitmapDisplayer (
圓角圖檔) 使用該屬性時,ImageView必須指定寬高
FadeInBitmapDisplayer (
漸顯圖檔)
- 為了避免在可滾動視圖中(ListView / GridView)發生OOM異常,請使用PauseOnScrollListener , 如
boolean pauseOnScroll = false; // or true
boolean pauseOnFling = true; // or false
PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);
listView.setOnScrollListener(listener);
其他圖檔庫介紹
其他圖檔庫介紹
Fresco
https://github.com/facebook/fresco
http://fresco-cn.org/docs/index.html
picasso
https://github.com/square/picasso
glide
https://github.com/bumptech/glide