ImageLoader简单介绍 ImageLoader 是最早开源的 Android 图片缓存库, 强大的缓存机制, 早期使用这个图片加载框架的Android应用非常多, 至今仍然有不少 Android 开发者在使用。 使用第一步,配置一些参数 < class="ttyprint"> DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.ic_stub) .showImageOnLoading(R.drawable.ic_empty) .showImageForEmptyUri(R.drawable.ic_empty) .showImageOnFail(R.drawable.ic_error) .cacheInMemory(true) .cacheOnDisc(true) .build(); 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图片加载框架实例对象
< class="ttyprint"> ImageLoader imageLoader = ImageLoader.getInstance(); > 到这里就可以使用获取到的imagerLoader来进行图片加载了,使用imagerLoader加载图片有很多种方式,简单说三种最常用的,然后再把全部加在方法源码贴在后面,根据自己的不同需求,大家再具体使用相应的方法。 - 根据url把图片展示到imageView控件上(异步加载)
< class="ttyprint"> imageLoader.displayImage(imageUri, imageView); > - 根据url把图片展示到imageView控件上,并监听图片加载是否完毕(异步加载)
< class="ttyprint"> 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) { }}); > 上面代码中仅仅重写了监听对象SimpleImageLoadingListener的onLoadingComplete方法,其实还有其他三个方法:onLoadingStarted(加载开始),onLoadingFailed(加载失败),onLoadingCancelled(加载取消),可以根据自己的具体需要去考虑是否进行重写。 - 根据url获取到图片文件并返回为位图格式对象(同步加载)
< class="ttyprint"> Bitmap bmp = imageLoader.loadImageSync(imageUri); > - 以下代码为ImageLoader对象加载图片的三大类方法及其重载
方法loadImage是异步加载,一般需要对加载过程设置监听,待图片资源加载完毕,再手动操作把图片展示到控件上。 方法displayImage是异步加载,可以不设置监听,待图片资源加载完毕,会自动把图片展示到控件上。 方法loadImageSync是同步加载,待图片资源加载完毕,直接返回位图资源对象
< class="ttyprint"> 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.pareDisplayTaskFor(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(); } > < class="ttyprint"> 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) .showImageOnLoading(resourceId) .showImageForEmptyUri(resourceId) .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) .showImageOnLoading(resourceId) .showImageForEmptyUri(resourceId) .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) .showImageOnFail(R.drawable.ic_error) .cacheInMemory(true) .cacheOnDisc(true) .build(); 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上面的项目地址:点击打开
|