1.將universal-image-loader引入我們的工程
① 直接下載jar包,放到我們libs中,添加一下引用
② 通過maven添加依賴,將universal-image-loader添加進來
③ 通過gradle添加依賴,可以從官方文檔將引用路徑粘貼到我們項目中的build.gradle中的dependencies。也可以在我們open module settings中的dependency中通過搜索添加。
2.根據我們的需求,添加權限。
如果需要從網絡獲取圖片我們就需要添加internet權限
如果需要進行磁盤緩存或者從本地加載圖片我們就需要添加sd卡的寫權限
3.在我們使用前對框架進行初始化(喝前搖一搖)
一般我們的初始化會寫在Application中,在Application的onCreate方法中。
初始化
ImageLoader.getInstance.init(ImageLoaderConfiguration對象)
ImageLoaderConfiguration 是通過ImageLoaderConfiguration.Builder生成的
// DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.
// See the sample project how to use ImageLoader correctly.
File cacheDir = StorageUtils.getCacheDirectory(context);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.diskCacheExtraOptions(480, 800, null)
.taskExecutor(...)
.taskExecutorForCachedImages(...)
.threadPoolSize(3) // default
.threadPriority(Thread.NORM_PRIORITY - 2) // default
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.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()
.build();
我們在自己的項目中,必須指定defaultDisplayImageOptions。
DisplayImageOptions如何進行初始化?
通過DisplayImageOptions.Builder進行一個初始化。下面也包含了很多配置信息。
// DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.
// See the sample project how to use ImageLoader correctly.
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub) // resource or drawable
.showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable
.showImageOnFail(R.drawable.ic_error) // resource or drawable
.resetViewBeforeLoading(false) // default
.delayBeforeLoading(1000)
.cacheInMemory(false) // default
.cacheOnDisk(false) // default
.preProcessor(...)
.postProcessor(...)
.extraForDownloader(...)
.considerExifParams(false) // default
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
.bitmapConfig(Bitmap.Config.ARGB_8888) // default
.decodingOptions(...)
.displayer(new SimpleBitmapDisplayer()) // default
.handler(new Handler()) // default
.build();
在項目中調用圖片加載的時候ImageLoader.displayImage如果未指定DisplayImageOptions,框架會調用我們在初始化時配置的defaultDisplayImageOptions。如果我們自己指定了DisplayImageOptions,會只調用我們自己制定的。
OnPauseScrollListener:滑動的時候是否進行暫停加載,三個參數
① ImageLoader,傳入實例
② boolean 滑動的時候是否進行暫停加載
③ boolean 在拋起來的時候是否進行暫停加載