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 在抛起来的时候是否进行暂停加载