Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
2014-08-02 10:08
513 查看
一、介绍
Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示。所以,如果你的程序里需要这个功能的话,那么不妨试试它。因为已经封装好了一些类和方法。我们 可以直接拿来用了。而不用重复去写了。其实,写一个这方面的程序还是比较麻烦的,要考虑多线程缓存,内存溢出等很多方面。二、具体使用
一个好的类库的重要特征就是可配置性强。我们先简单使用Android-Universal-Image-Loader,一般情况下使用默认配置就可以了。下面的实例利用Android-Universal-Image-Loader将网络图片加载到图片墙中。
public class BaseActivity extends Activity { ImageLoader imageLoader; @Override protected void onCreate(Bundle savedInstanceState) { // Create global configuration and initialize ImageLoader with this configuration ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()) .build(); ImageLoader.getInstance().init(config); super.onCreate(savedInstanceState); } }
public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageLoader imageLoader = ImageLoader.getInstance(); GridView gridView = (GridView) this.findViewById(R.id.grdvImageWall); gridView.setAdapter(new PhotoWallAdapter(Constants.IMAGES)); } static class ViewHolder { ImageView imageView; ProgressBar progressBar; } public class PhotoWallAdapter extends BaseAdapter { String[] imageUrls; ImageLoader imageLoad; DisplayImageOptions options; LinearLayout gridViewItem; public PhotoWallAdapter(String[] imageUrls) { assert imageUrls != null; this.imageUrls = imageUrls; 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 .considerExifParams(false) // default .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default .bitmapConfig(Bitmap.Config.ARGB_8888) // default .displayer(new SimpleBitmapDisplayer()) // default .handler(new Handler()) // default .build(); this.imageLoad = ImageLoader.getInstance(); } @Override public int getCount() { return this.imageUrls.length; } @Override public Object getItem(int position) { if (position <= 0 || position >= this.imageUrls.length) { throw new IllegalArgumentException( "position<=0||position>=this.imageUrls.length"); } return this.imageUrls[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // 判断这个image是否已经在缓存当中,如果没有就下载 final ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); gridViewItem = (LinearLayout) getLayoutInflater().inflate( R.layout.image_wall_item, null); holder.imageView = (ImageView) gridViewItem .findViewById(R.id.item_image); holder.progressBar = (ProgressBar) gridViewItem .findViewById(R.id.item_process); gridViewItem.setTag(holder); convertView = gridViewItem; } else { holder = (ViewHolder) gridViewItem.getTag(); } this.imageLoad.displayImage(this.imageUrls[position], holder.imageView, options, new SimpleImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { holder.progressBar.setProgress(0); holder.progressBar.setVisibility(View.VISIBLE); } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { holder.progressBar.setVisibility(View.GONE); } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { holder.progressBar.setVisibility(View.GONE); } }, new ImageLoadingProgressListener() { @Override public void onProgressUpdate(String imageUri, View view, int current, int total) { holder.progressBar.setProgress(Math.round(100.0f * current / total)); } }); // 通过URL判断图片是否已经下载 return convertView; } } }
里面主要的对象都用 突出显示了。
三者的关系
ImageLoaderConfiguration是针对图片缓存的全局配置,主要有线程类、缓存大小、磁盘大小、图片下载与解析、日志方面的配置。ImageLoader是具体下载图片,缓存图片,显示图片的具体执行类,它有两个具体的方法displayImage(...)、loadImage(...),但是其实最终他们的实现都是displayImage(...)。
DisplayImageOptions用于指导每一个Imageloader根据网络图片的状态(空白、下载错误、正在下载)显示对应的图片,是否将缓存加载到磁盘上,下载完后对图片进行怎么样的处理。
从三者的协作关系上看,他们有点像厨房规定、厨师、客户个人口味之间的关系。ImageLoaderConfiguration就像是厨房里面的规定,每一个厨师要怎么着装,要怎么保持厨房的干净,这是针对每一个厨师都适用的规定,而且不允许个性化改变。ImageLoader就像是具体做菜的厨师,负责具体菜谱的制作。DisplayImageOptions就像每个客户的偏好,根据客户是重口味还是清淡,每一个imageLoader根据DisplayImageOptions的要求具体执行。
ImageLoaderConfiguration
在上面的示例代码中,我们使用ImageLoaderConfiguration的默认配置,下面给出ImageLoaderConfiguration比较详尽的配置,从下面的配置中,可以看出ImageLoaderConfiguration的配置主要是全局性的配置,主要有线程类、缓存大小、磁盘大小、图片下载与解析、日志方面的配置。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 - 1) // default .tasksProcessingOrder(QueueProcessingType.FIFO) // default .denyCacheImageMultipleSizesInMemory() .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) .memoryCacheSize(2 * 1024 * 1024) .memoryCacheSizePercentage(13) // default .diskCache(new UnlimitedDiscCache(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();
ImageLoaderConfiguration的主要职责就是记录相关的配置,它的内部其实就是一些字段的集合(如下面的源代码)。它有一个builder的内部类,这个类中的字段跟ImageLoaderConfiguration中的字段完全一致,它有一些默认值,通过修改builder可以配置ImageLoaderConfiguration。
public final class DisplayImageOptions { private final int imageResOnLoading; private final int imageResForEmptyUri; private final int imageResOnFail; private final Drawable imageOnLoading; private final Drawable imageForEmptyUri; private final Drawable imageOnFail; private final boolean resetViewBeforeLoading; private final boolean cacheInMemory; private final boolean cacheOnDisk; private final ImageScaleType imageScaleType; private final Options decodingOptions; private final int delayBeforeLoading; private final boolean considerExifParams; private final Object extraForDownloader; private final BitmapProcessor preProcessor; private final BitmapProcessor postProcessor; private final BitmapDisplayer displayer; private final Handler handler; private final boolean isSyncLoading; private DisplayImageOptions(Builder builder) { imageResOnLoading = builder.imageResOnLoading; imageResForEmptyUri = builder.imageResForEmptyUri; imageResOnFail = builder.imageResOnFail; imageOnLoading = builder.imageOnLoading; imageForEmptyUri = builder.imageForEmptyUri; imageOnFail = builder.imageOnFail; resetViewBeforeLoading = builder.resetViewBeforeLoading; cacheInMemory = builder.cacheInMemory; cacheOnDisk = builder.cacheOnDisk; imageScaleType = builder.imageScaleType; decodingOptions = builder.decodingOptions; delayBeforeLoading = builder.delayBeforeLoading; considerExifParams = builder.considerExifParams; extraForDownloader = builder.extraForDownloader; preProcessor = builder.preProcessor; postProcessor = builder.postProcessor; displayer = builder.displayer; handler = builder.handler; isSyncLoading = builder.isSyncLoading; } public boolean shouldShowImageOnLoading() { return imageOnLoading != null || imageResOnLoading != 0; } public boolean shouldShowImageForEmptyUri() { return imageForEmptyUri != null || imageResForEmptyUri != 0; } public boolean shouldShowImageOnFail() { return imageOnFail != null || imageResOnFail != 0; } public boolean shouldPreProcess() { return preProcessor != null; } public boolean shouldPostProcess() { return postProcessor != null; } public boolean shouldDelayBeforeLoading() { return delayBeforeLoading > 0; } public Drawable getImageOnLoading(Resources res) { return imageResOnLoading != 0 ? res.getDrawable(imageResOnLoading) : imageOnLoading; } public Drawable getImageForEmptyUri(Resources res) { return imageResForEmptyUri != 0 ? res.getDrawable(imageResForEmptyUri) : imageForEmptyUri; } public Drawable getImageOnFail(Resources res) { return imageResOnFail != 0 ? res.getDrawable(imageResOnFail) : imageOnFail; } public boolean isResetViewBeforeLoading() { return resetViewBeforeLoading; } public boolean isCacheInMemory() { return cacheInMemory; } public boolean isCacheOnDisk() { return cacheOnDisk; } public ImageScaleType getImageScaleType() { return imageScaleType; } public Options getDecodingOptions() { return decodingOptions; } public int getDelayBeforeLoading() { return delayBeforeLoading; } public boolean isConsiderExifParams() { return considerExifParams; } public Object getExtraForDownloader() { return extraForDownloader; } public BitmapProcessor getPreProcessor() { return preProcessor; } public BitmapProcessor getPostProcessor() { return postProcessor; } public BitmapDisplayer getDisplayer() { return displayer; } public Handler getHandler() { return handler; } boolean isSyncLoading() { return isSyncLoading; } /** * Builder for {@link DisplayImageOptions} * * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) */ public static class Builder { private int imageResOnLoading = 0; private int imageResForEmptyUri = 0; private int imageResOnFail = 0; private Drawable imageOnLoading = null; private Drawable imageForEmptyUri = null; private Drawable imageOnFail = null; private boolean resetViewBeforeLoading = false; private boolean cacheInMemory = false; private boolean cacheOnDisk = false; private ImageScaleType imageScaleType = ImageScaleType.IN_SAMPLE_POWER_OF_2; private Options decodingOptions = new Options(); private int delayBeforeLoading = 0; private boolean considerExifParams = false; private Object extraForDownloader = null; private BitmapProcessor preProcessor = null; private BitmapProcessor postProcessor = null; private BitmapDisplayer displayer = DefaultConfigurationFactory.createBitmapDisplayer(); private Handler handler = null; private boolean isSyncLoading = false; public Builder() { decodingOptions.inPurgeable = true; decodingOptions.inInputShareable = true; } /** * Stub image will be displayed in {@link com.nostra13.universalimageloader.core.imageaware.ImageAware * image aware view} during image loading * * @param imageRes Stub image resource * @deprecated Use {@link #showImageOnLoading(int)} instead */ @Deprecated public Builder showStubImage(int imageRes) { imageResOnLoading = imageRes; return this; } /** * Incoming image will be displayed in {@link com.nostra13.universalimageloader.core.imageaware.ImageAware * image aware view} during image loading * * @param imageRes Image resource */ public Builder showImageOnLoading(int imageRes) { imageResOnLoading = imageRes; return this; } /** * Incoming drawable will be displayed in {@link com.nostra13.universalimageloader.core.imageaware.ImageAware * image aware view} during image loading. * This option will be ignored if {@link DisplayImageOptions.Builder#showImageOnLoading(int)} is set. */ public Builder showImageOnLoading(Drawable drawable) { imageOnLoading = drawable; return this; } /** * Incoming image will be displayed in {@link com.nostra13.universalimageloader.core.imageaware.ImageAware * image aware view} if empty URI (null or empty * string) will be passed to <b>ImageLoader.displayImage(...)</b> method. * * @param imageRes Image resource */ public Builder showImageForEmptyUri(int imageRes) { imageResForEmptyUri = imageRes; return this; } /** * Incoming drawable will be displayed in {@link com.nostra13.universalimageloader.core.imageaware.ImageAware * image aware view} if empty URI (null or empty * string) will be passed to <b>ImageLoader.displayImage(...)</b> method. * This option will be ignored if {@link DisplayImageOptions.Builder#showImageForEmptyUri(int)} is set. */ public Builder showImageForEmptyUri(Drawable drawable) { imageForEmptyUri = drawable; return this; } /** * Incoming image will be displayed in {@link com.nostra13.universalimageloader.core.imageaware.ImageAware * image aware view} if some error occurs during * requested image loading/decoding. * * @param imageRes Image resource */ public Builder showImageOnFail(int imageRes) { imageResOnFail = imageRes; return this; } /** * Incoming drawable will be displayed in {@link com.nostra13.universalimageloader.core.imageaware.ImageAware * image aware view} if some error occurs during * requested image loading/decoding. * This option will be ignored if {@link DisplayImageOptions.Builder#showImageOnFail(int)} is set. */ public Builder showImageOnFail(Drawable drawable) { imageOnFail = drawable; return this; } /** * {@link com.nostra13.universalimageloader.core.imageaware.ImageAware * image aware view} will be reset (set <b>null</b>) before image loading start * * @deprecated Use {@link #resetViewBeforeLoading(boolean) resetViewBeforeLoading(true)} instead */ public Builder resetViewBeforeLoading() { resetViewBeforeLoading = true; return this; } /** * Sets whether {@link com.nostra13.universalimageloader.core.imageaware.ImageAware * image aware view} will be reset (set <b>null</b>) before image loading start */ public Builder resetViewBeforeLoading(boolean resetViewBeforeLoading) { this.resetViewBeforeLoading = resetViewBeforeLoading; return this; } /** * Loaded image will be cached in memory * * @deprecated Use {@link #cacheInMemory(boolean) cacheInMemory(true)} instead */ @Deprecated public Builder cacheInMemory() { cacheInMemory = true; return this; } /** Sets whether loaded image will be cached in memory */ public Builder cacheInMemory(boolean cacheInMemory) { this.cacheInMemory = cacheInMemory; return this; } /** * Loaded image will be cached on disk * * @deprecated Use {@link #cacheOnDisk(boolean) cacheOnDisk(true)} instead */ @Deprecated public Builder cacheOnDisc() { return cacheOnDisk(true); } /** * Sets whether loaded image will be cached on disk * * @deprecated Use {@link #cacheOnDisk(boolean)} instead */ @Deprecated public Builder cacheOnDisc(boolean cacheOnDisk) { return cacheOnDisk(cacheOnDisk); } /** Sets whether loaded image will be cached on disk */ public Builder cacheOnDisk(boolean cacheOnDisk) { this.cacheOnDisk = cacheOnDisk; return this; } /** * Sets {@linkplain ImageScaleType scale type} for decoding image. This parameter is used while define scale * size for decoding image to Bitmap. Default value - {@link ImageScaleType#IN_SAMPLE_POWER_OF_2} */ public Builder imageScaleType(ImageScaleType imageScaleType) { this.imageScaleType = imageScaleType; return this; } /** Sets {@link Bitmap.Config bitmap config} for image decoding. Default value - {@link Bitmap.Config#ARGB_8888} */ public Builder bitmapConfig(Bitmap.Config bitmapConfig) { if (bitmapConfig == null) throw new IllegalArgumentException("bitmapConfig can't be null"); decodingOptions.inPreferredConfig = bitmapConfig; return this; } /** * Sets options for image decoding.<br /> * <b>NOTE:</b> {@link Options#inSampleSize} of incoming options will <b>NOT</b> be considered. Library * calculate the most appropriate sample size itself according yo {@link #imageScaleType(ImageScaleType)} * options.<br /> * <b>NOTE:</b> This option overlaps {@link #bitmapConfig(android.graphics.Bitmap.Config) bitmapConfig()} * option. */ public Builder decodingOptions(Options decodingOptions) { if (decodingOptions == null) throw new IllegalArgumentException("decodingOptions can't be null"); this.decodingOptions = decodingOptions; return this; } /** Sets delay time before starting loading task. Default - no delay. */ public Builder delayBeforeLoading(int delayInMillis) { this.delayBeforeLoading = delayInMillis; return this; } /** Sets auxiliary object which will be passed to {@link ImageDownloader#getStream(String, Object)} */ public Builder extraForDownloader(Object extra) { this.extraForDownloader = extra; return this; } /** Sets whether ImageLoader will consider EXIF parameters of JPEG image (rotate, flip) */ public Builder considerExifParams(boolean considerExifParams) { this.considerExifParams = considerExifParams; return this; } /** * Sets bitmap processor which will be process bitmaps before they will be cached in memory. So memory cache * will contain bitmap processed by incoming preProcessor.<br /> * Image will be pre-processed even if caching in memory is disabled. */ public Builder preProcessor(BitmapProcessor preProcessor) { this.preProcessor = preProcessor; return this; } /** * Sets bitmap processor which will be process bitmaps before they will be displayed in * {@link com.nostra13.universalimageloader.core.imageaware.ImageAware image aware view} but * after they'll have been saved in memory cache. */ public Builder postProcessor(BitmapProcessor postProcessor) { this.postProcessor = postProcessor; return this; } /** * Sets custom {@link BitmapDisplayer displayer} for image loading task. Default value - * {@link DefaultConfigurationFactory#createBitmapDisplayer()} */ public Builder displayer(BitmapDisplayer displayer) { if (displayer == null) throw new IllegalArgumentException("displayer can't be null"); this.displayer = displayer; return this; } Builder syncLoading(boolean isSyncLoading) { this.isSyncLoading = isSyncLoading; return this; } /** * Sets custom {@linkplain Handler handler} for displaying images and firing {@linkplain ImageLoadingListener * listener} events. */ public Builder handler(Handler handler) { this.handler = handler; return this; } /** Sets all options equal to incoming options */ public Builder cloneFrom(DisplayImageOptions options) { imageResOnLoading = options.imageResOnLoading; imageResForEmptyUri = options.imageResForEmptyUri; imageResOnFail = options.imageResOnFail; imageOnLoading = options.imageOnLoading; imageForEmptyUri = options.imageForEmptyUri; imageOnFail = options.imageOnFail; resetViewBeforeLoading = options.resetViewBeforeLoading; cacheInMemory = options.cacheInMemory; cacheOnDisk = options.cacheOnDisk; imageScaleType = options.imageScaleType; decodingOptions = options.decodingOptions; delayBeforeLoading = options.delayBeforeLoading; considerExifParams = options.considerExifParams; extraForDownloader = options.extraForDownloader; preProcessor = options.preProcessor; postProcessor = options.postProcessor; displayer = options.displayer; handler = options.handler; isSyncLoading = options.isSyncLoading; return this; } /** Builds configured {@link DisplayImageOptions} object */ public DisplayImageOptions build() { return new DisplayImageOptions(this); } } /** * Creates options appropriate for single displaying: * <ul> * <li>View will <b>not</b> be reset before loading</li> * <li>Loaded image will <b>not</b> be cached in memory</li> * <li>Loaded image will <b>not</b> be cached on disk</li> * <li>{@link ImageScaleType#IN_SAMPLE_POWER_OF_2} decoding type will be used</li> * <li>{@link Bitmap.Config#ARGB_8888} bitmap config will be used for image decoding</li> * <li>{@link SimpleBitmapDisplayer} will be used for image displaying</li> * </ul> * <p/> * These option are appropriate for simple single-use image (from drawables or from Internet) displaying. */ public static DisplayImageOptions createSimple() { return new Builder().build(); } }
View Code
参考链接
http://blog.csdn.net/wangjinyu501/article/details/8091623 https://github.com/nostra13/Android-Universal-Image-Loader http://www.intexsoft.com/blog/item/74-universal-image-loader-part-3.html
相关文章推荐
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解 一、介绍
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解:
- Android-Universal-Image-Loader类库三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- (图片处理)Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解