图片加载框架简单介绍 ImageLoader 的基本使用
16lz
2021-01-25
**ImageLoader简单介绍 **
ImageLoader 是最早开源的 Android 图片缓存库, 强大的缓存机制, 早期使用这个图片加载框架的Android应用非常多, 至今仍然有不少 Android 开发者在使用。使用第一步,配置一些参数
DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.ic_stub) // 设置图片下载期间显示的图片 .showImageOnLoading(R.drawable.ic_empty) //设置下载过程中图片显示 .showImageForEmptyUri(R.drawable.ic_empty) // 设置图片Uri为空或是错误的时候显示的图片 .showImageOnFail(R.drawable.ic_error) // 设置图片加载或解码过程中发生错误显示的图片 .cacheInMemory(true) // 设置下载的图片是否缓存在内存中 .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中 .build(); // 创建配置过得DisplayImageOption对象ImageLoaderConfiguration config = new ImageLoaderConfiguration .Builder( context.getApplicationContext()) .defaultDisplayImageOptions(options) .threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .discCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO) .build(); imageLoader = ImageLoader.getInstance(); imageLoader.init(config);
因为ImageLoader使用的是单例模式。所以上面的这些配置(还有一些其他配置选项,这里就不一一写出来了),在项目中只需要设置一次就可以了。
- 第二步,获取ImageLoader图片加载框架实例对象
ImageLoader imageLoader = ImageLoader.getInstance();
到这里就可以使用获取到的imagerLoader来进行图片加载了,使用imagerLoader加载图片有很多种方式,简单说三种最常用的,然后再把全部加在方法源码贴在后面,根据自己的不同需求,大家再具体使用相应的方法。
- 根据url把图片展示到imageView控件上(异步加载)
imageLoader.displayImage(imageUri, imageView);
- 根据url把图片展示到imageView控件上,并监听图片加载是否完毕(异步加载)
imageLoader.displayImage(imageUrl, imageView, null , new SimpleImageLoadingListener(){ @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { //在这里执行你想要做的事情 }}, new ImageLoadingProgressListener(){ @Override public void onProgressUpdate(String imageUri, View view, int current, int total) { //在这里执行你想要做的事情,total总进度,current当前加载进度 }});
上面代码中仅仅重写了监听对象SimpleImageLoadingListener的onLoadingComplete方法,其实还有其他三个方法:onLoadingStarted(加载开始),onLoadingFailed(加载失败),onLoadingCancelled(加载取消),可以根据自己的具体需要去考虑是否进行重写。
- 根据url获取到图片文件并返回为位图格式对象(同步加载)
Bitmap bmp = imageLoader.loadImageSync(imageUri);
- 以下代码为ImageLoader对象加载图片的三大类方法及其重载
方法loadImage是异步加载,一般需要对加载过程设置监听,待图片资源加载完毕,再手动操作把图片展示到控件上。
方法displayImage是异步加载,可以不设置监听,待图片资源加载完毕,会自动把图片展示到控件上。
方法loadImageSync是同步加载,待图片资源加载完毕,直接返回位图资源对象
//ImageViewAware这个类主要是将ImageView进行一个包装,将 ImageView的强引用变成弱引用, //当内存不足的时候,可以更好的回收ImageView对象,还有就是获取ImageView的宽度和高度。 //这使得我们可以根据ImageView的宽高去对图片进行一个裁剪,减少内存的使用。 public void displayImage(String uri, ImageAware imageAware) { this.displayImage(uri, (ImageAware)imageAware, (DisplayImageOptions)null, (ImageLoadingListener)null, (ImageLoadingProgressListener)null); } public void displayImage(String uri, ImageAware imageAware, ImageLoadingListener listener) { this.displayImage(uri, (ImageAware)imageAware, (DisplayImageOptions)null, listener, (ImageLoadingProgressListener)null); } public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options) { this.displayImage(uri, (ImageAware)imageAware, options, (ImageLoadingListener)null, (ImageLoadingProgressListener)null); } public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options, ImageLoadingListener listener) { this.displayImage(uri, (ImageAware)imageAware, options, listener, (ImageLoadingProgressListener)null); } public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) { this.checkConfiguration(); if(imageAware == null) { throw new IllegalArgumentException("Wrong arguments were passed to displayImage() method (ImageView reference must not be null)"); } else { if(listener == null) { listener = this.emptyListener; } if(options == null) { options = this.configuration.defaultDisplayImageOptions; } if(TextUtils.isEmpty(uri)) { this.engine.cancelDisplayTaskFor(imageAware); listener.onLoadingStarted(uri, imageAware.getWrappedView()); if(options.shouldShowImageForEmptyUri()) { imageAware.setImageDrawable(options.getImageForEmptyUri(this.configuration.resources)); } else { imageAware.setImageDrawable((Drawable)null); } listener.onLoadingComplete(uri, imageAware.getWrappedView(), (Bitmap)null); } else { ImageSize targetSize = ImageSizeUtils.defineTargetSizeForView(imageAware, this.configuration.getMaxImageSize()); String memoryCacheKey = MemoryCacheUtils.generateKey(uri, targetSize); this.engine.prepareDisplayTaskFor(imageAware, memoryCacheKey); listener.onLoadingStarted(uri, imageAware.getWrappedView()); Bitmap bmp = (Bitmap)this.configuration.memoryCache.get(memoryCacheKey); ImageLoadingInfo imageLoadingInfo; if(bmp != null && !bmp.isRecycled()) { L.d("Load image from memory cache [%s]", new Object[]{memoryCacheKey}); if(options.shouldPostProcess()) { imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey, options, listener, progressListener, this.engine.getLockForUri(uri)); ProcessAndDisplayImageTask displayTask1 = new ProcessAndDisplayImageTask(this.engine, bmp, imageLoadingInfo, defineHandler(options)); if(options.isSyncLoading()) { displayTask1.run(); } else { this.engine.submit(displayTask1); } } else { options.getDisplayer().display(bmp, imageAware, LoadedFrom.MEMORY_CACHE); listener.onLoadingComplete(uri, imageAware.getWrappedView(), bmp); } } else { if(options.shouldShowImageOnLoading()) { imageAware.setImageDrawable(options.getImageOnLoading(this.configuration.resources)); } else if(options.isResetViewBeforeLoading()) { imageAware.setImageDrawable((Drawable)null); } imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey, options, listener, progressListener, this.engine.getLockForUri(uri)); LoadAndDisplayImageTask displayTask = new LoadAndDisplayImageTask(this.engine, imageLoadingInfo, defineHandler(options)); if(options.isSyncLoading()) { displayTask.run(); } else { this.engine.submit(displayTask); } } } } } public void displayImage(String uri, ImageView imageView) { this.displayImage(uri, (ImageAware)(new ImageViewAware(imageView)), (DisplayImageOptions)null, (ImageLoadingListener)null, (ImageLoadingProgressListener)null); } public void displayImage(String uri, ImageView imageView, DisplayImageOptions options) { this.displayImage(uri, (ImageAware)(new ImageViewAware(imageView)), options, (ImageLoadingListener)null, (ImageLoadingProgressListener)null); } public void displayImage(String uri, ImageView imageView, ImageLoadingListener listener) { this.displayImage(uri, (ImageAware)(new ImageViewAware(imageView)), (DisplayImageOptions)null, listener, (ImageLoadingProgressListener)null); } public void displayImage(String uri, ImageView imageView, DisplayImageOptions options, ImageLoadingListener listener) { this.displayImage(uri, (ImageView)imageView, options, listener, (ImageLoadingProgressListener)null); } public void displayImage(String uri, ImageView imageView, DisplayImageOptions options, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) { this.displayImage(uri, (ImageAware)(new ImageViewAware(imageView)), options, listener, progressListener); } public void loadImage(String uri, ImageLoadingListener listener) { this.loadImage(uri, (ImageSize)null, (DisplayImageOptions)null, listener, (ImageLoadingProgressListener)null); } public void loadImage(String uri, ImageSize targetImageSize, ImageLoadingListener listener) { this.loadImage(uri, targetImageSize, (DisplayImageOptions)null, listener, (ImageLoadingProgressListener)null); } public void loadImage(String uri, DisplayImageOptions options, ImageLoadingListener listener) { this.loadImage(uri, (ImageSize)null, options, listener, (ImageLoadingProgressListener)null); } public void loadImage(String uri, ImageSize targetImageSize, DisplayImageOptions options, ImageLoadingListener listener) { this.loadImage(uri, targetImageSize, options, listener, (ImageLoadingProgressListener)null); } public void loadImage(String uri, ImageSize targetImageSize, DisplayImageOptions options, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) { this.checkConfiguration(); if(targetImageSize == null) { targetImageSize = this.configuration.getMaxImageSize(); } if(options == null) { options = this.configuration.defaultDisplayImageOptions; } NonViewAware imageAware = new NonViewAware(uri, targetImageSize, ViewScaleType.CROP); this.displayImage(uri, (ImageAware)imageAware, options, listener, progressListener); } public Bitmap loadImageSync(String uri) { return this.loadImageSync(uri, (ImageSize)null, (DisplayImageOptions)null); } public Bitmap loadImageSync(String uri, DisplayImageOptions options) { return this.loadImageSync(uri, (ImageSize)null, options); } public Bitmap loadImageSync(String uri, ImageSize targetImageSize) { return this.loadImageSync(uri, targetImageSize, (DisplayImageOptions)null); } public Bitmap loadImageSync(String uri, ImageSize targetImageSize, DisplayImageOptions options) { if(options == null) { options = this.configuration.defaultDisplayImageOptions; } options = (new Builder()).cloneFrom(options).syncLoading(true).build(); ImageLoader.SyncImageLoadingListener listener = new ImageLoader.SyncImageLoadingListener(); this.loadImage(uri, targetImageSize, options, listener); return listener.getLoadedBitmap(); }
-
对ImageLoader进行封装
建议大家在使用框架的时候,自己再进行一下二次封装,这样如果以后换框架,整个项目改动的地方会非常少,而且自己封装的东西,用起来肯定会更加得心应手。下面是我自己对ImageLoader进行简单封装生成的一个类,大家可以参考下然后自己封装一个最好。
public class ImagerLoaderUtil { private ImageLoader imageLoader; private DisplayImageOptions options; private Context context; private static ImagerLoaderUtil imagerLoaderUtil; private ImagerLoaderUtil(Context context) { super(); this.context = context; } public synchronized static ImagerLoaderUtil getInstance(Context context){ if(imagerLoaderUtil == null){ imagerLoaderUtil = new ImagerLoaderUtil(context); imagerLoaderUtil.initImageLoader(); } return imagerLoaderUtil; } public void displayMyImage(String imageUrl, ImageView imageView ){ imageLoader.displayImage(imageUrl, imageView); } public void displayMyImage(String imageUrl, ImageView imageView,SimpleImageLoadingListener listener ){ imageLoader.displayImage(imageUrl, imageView, listener); } public void displayMyImage(String imageUrl, ImageView imageView, int resourceId){ DisplayImageOptions tempOptions = new DisplayImageOptions.Builder() .cacheInMemory(false)//设置下载的图片是否缓存在内存 .cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中 .showImageOnLoading(resourceId) // 设置图片下载期间显示的图片 .showImageForEmptyUri(resourceId) // 设置图片Uri为空或是错误的时候显示的图片 .showImageOnFail(resourceId) // 设置图片加载或解码过程中发生错误显示的图片 .build(); imageLoader.displayImage(imageUrl, imageView, tempOptions); } public void displayMyImage(String imageUrl, ImageView imageView, int resourceId, SimpleImageLoadingListener listener) { DisplayImageOptions tempOptions = new DisplayImageOptions.Builder() .cacheInMemory(false)//设置下载的图片是否缓存在内存 .cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中 .showImageOnLoading(resourceId) // 设置图片下载期间显示的图片 .showImageForEmptyUri(resourceId) // 设置图片Uri为空或是错误的时候显示的图片 .showImageOnFail(resourceId) // 设置图片加载或解码过程中发生错误显示的图片 .build(); imageLoader.displayImage(imageUrl, imageView, tempOptions, listener); } public void initImageLoader() { DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.ic_stub) // 设置图片下载期间显示的图片 .showImageOnLoading(R.drawable.ic_empty) //设置下载过程中图片显示 .showImageForEmptyUri(R.drawable.ic_empty) // 设置图片Uri为空或是错误的时候显示的图片 .showImageOnFail(R.drawable.ic_error) // 设置图片加载或解码过程中发生错误显示的图片 .cacheInMemory(true) // 设置下载的图片是否缓存在内存中 .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中 .build(); // 创建配置过得DisplayImageOption对象 ImageLoaderConfiguration config = new ImageLoaderConfiguration .Builder( context.getApplicationContext()) .defaultDisplayImageOptions(options) .threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .discCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO) .build(); imageLoader = ImageLoader.getInstance(); imageLoader.init(config); }}
- 最后附上ImagerLoader在gitHub上面的项目地址:点击打开
更多相关文章
- Android(安卓)Intent 总结
- android ImageButton显示本地图片
- android 游戏开发过程中让图片自适应
- Android(安卓)首页轮播图实现
- 缓存统一管理工具类--android
- android 高效显示Bitmap - 开发文档翻译
- android中访问 webView加载URL时的response数据
- Android(安卓)给图片、文字、控件、布局添加阴影效果(悬浮效果)
- android 使用zxing生成中间嵌套logo的二维码