Android Universal Image Loader是GitHub上的一个图片异步加载类库。
下载地址:https://github.com/nostra13/Android-Universal-Image-Loader
源码分析网站:http://a.codekk.com/detail/Android/huxian99/Android%20Universal%20Image%20Loader%20%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90
核心类:ImageLoaderConfiguration 、DisplayImageOptions 、ImageLoader
接下来我就为大家介绍在studio上具体使用的步骤:
第一步:在dependencies中添加依赖
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" />
第三初步:始化配置,在Application或Activity的onCreate方法中创建 ImageLoaderConfiguration对象,配置参数
可以创建默认的ImageLoader配置参数
ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
或者也可以自己配置参数
File cacheDir = StorageUtils.getCacheDirectory(this); // 自定义缓存文件夹
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.denyCacheImageMultipleSizesInMemory() // 禁止在内存中缓存同一张图片的多个尺寸类型(必须设置,放置出现内存溢出(OOM))
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())// default , 指定硬盘缓存时文件名的生成器
.diskCache(new UnlimitedDiskCache(cacheDir))
.build();
最后初始化全局变量
ImageLoader.getInstance().init(config);// 初始化全局变量
注意:如果是放在Application的子类中,记得在清单文件里注册Application
第四步:使用添加了配置的ImageLoader下载图片
String imageUrl = "https://www.baidu.com/img/bd_logo1.png";
ImageSize mImageSize = new ImageSize(100, 100);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher) // 图片正在加载时显示的图片资源ID
.showImageForEmptyUri(R.mipmap.ic_launcher) // URI为空时显示的图片资源ID
.showImageOnFail(R.mipmap.ic_launcher) // 图片加载失败时显示的图片资源ID
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new FadeInBitmapDisplayer(5000))//<span style="color:#969896;">设置图片显示的方式</span>
.build();
ImageLoader.getInstance().displayImage(imageUrl, image, options);
image 是要显示的布局文件里的控件对象。
好了,使用步骤就到这里了,你可以测测能不能下载图片,有的时候会因为下载地址出现问题,所以建议先使用百度的图片测试。地址:https://www.baidu.com/img/bd_logo1.png
确定里面的实现原理,源码之类的你还是先会用了,后面自己上源码分析网看吧!
源码分析网站:http://a.codekk.com/detail/Android/huxian99/Android%20Universal%20Image%20Loader%20%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90
备注:
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();