搜索
热搜: 活动 交友 discuz
查看: 3115|回复: 0
打印 上一主题 下一主题

Android-Universal-Image-Loader详解

[复制链接]

160

主题

165

帖子

814

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
814
跳转到指定楼层
楼主
发表于 2016-8-19 14:11:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一、介绍

Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示。所以,如果你的程序里需要这个功能的话,那么不妨试试它。因为已经封装好了一些类和方法。我们 可以直接拿来用了。而不用重复去写了。其实,写一个这方面的程序还是比较麻烦的,要考虑多线程缓存,内存溢出等很多方面。

二、具体使用

一个好的类库的重要特征就是可配置性强。我们先简单使用Android-Universal-Image-Loader,一般情况下使用默认配置就可以了。

下面的实例利用Android-Universal-Image-Loader将网络图片加载到图片墙中。


  1. public class BaseActivity extends Activity {
  2.     ImageLoader imageLoader;
  3.     @Override
  4.     protected void onCreate(Bundle savedInstanceState) {
  5.           // Create global configuration and initialize ImageLoader with this configuration
  6.         ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
  7.             .build();
  8.         ImageLoader.getInstance().init(config);
  9.         super.onCreate(savedInstanceState);
  10.     }
  11. }
复制代码
  1. public class MainActivity extends BaseActivity {

  2.     @Override
  3.     protected void onCreate(Bundle savedInstanceState) {
  4.         super.onCreate(savedInstanceState);
  5.         setContentView(R.layout.activity_main);
  6.   
  7.         ImageLoader imageLoader = ImageLoader.getInstance();

  8.         GridView gridView = (GridView) this.findViewById(R.id.grdvImageWall);
  9.         gridView.setAdapter(new PhotoWallAdapter(Constants.IMAGES));
  10.     }

  11.     static class ViewHolder {
  12.         ImageView imageView;
  13.         ProgressBar progressBar;
  14.     }

  15.     public class PhotoWallAdapter extends BaseAdapter {
  16.         String[] imageUrls;
  17.         ImageLoader imageLoad;
  18.         DisplayImageOptions options;
  19.         LinearLayout gridViewItem;

  20.         public PhotoWallAdapter(String[] imageUrls) {
  21.             assert imageUrls != null;
  22.             this.imageUrls = imageUrls;

  23.             options = new DisplayImageOptions.Builder()
  24.                     .showImageOnLoading(R.drawable.ic_stub) // resource or
  25.                                                             // drawable
  26.                     .showImageForEmptyUri(R.drawable.ic_empty) // resource or
  27.                                                                 // drawable
  28.                     .showImageOnFail(R.drawable.ic_error) // resource or
  29.                                                             // drawable
  30.                     .resetViewBeforeLoading(false) // default
  31.                     .delayBeforeLoading(1000).cacheInMemory(false) // default
  32.                     .cacheOnDisk(false) // default
  33.                     .considerExifParams(false) // default
  34.                     .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
  35.                     .bitmapConfig(Bitmap.Config.ARGB_8888) // default
  36.                     .displayer(new SimpleBitmapDisplayer()) // default
  37.                     .handler(new Handler()) // default
  38.                     .build();
  39.             this.imageLoad = ImageLoader.getInstance();

  40.         }

  41.         @Override
  42.         public int getCount() {
  43.             return this.imageUrls.length;
  44.         }

  45.         @Override
  46.         public Object getItem(int position) {
  47.             if (position <= 0 || position >= this.imageUrls.length) {
  48.                 throw new IllegalArgumentException(
  49.                         "position<=0||position>=this.imageUrls.length");
  50.             }
  51.             return this.imageUrls[position];
  52.         }

  53.         @Override
  54.         public long getItemId(int position) {
  55.             return position;
  56.         }

  57.         @Override
  58.         public View getView(int position, View convertView, ViewGroup parent) {
  59.             // 判断这个image是否已经在缓存当中,如果没有就下载
  60.             final ViewHolder holder;
  61.             if (convertView == null) {
  62.                 holder = new ViewHolder();
  63.                 gridViewItem = (LinearLayout) getLayoutInflater().inflate(
  64.                         R.layout.image_wall_item, null);
  65.                 holder.imageView = (ImageView) gridViewItem
  66.                         .findViewById(R.id.item_image);
  67.                 holder.progressBar = (ProgressBar) gridViewItem
  68.                         .findViewById(R.id.item_process);
  69.                 gridViewItem.setTag(holder);
  70.                 convertView = gridViewItem;
  71.             } else {
  72.                 holder = (ViewHolder) gridViewItem.getTag();
  73.             }
  74.             this.imageLoad.displayImage(this.imageUrls[position],
  75.                     holder.imageView, options,
  76.                     new SimpleImageLoadingListener() {

  77.                         @Override
  78.                         public void onLoadingStarted(String imageUri, View view) {
  79.                             holder.progressBar.setProgress(0);
  80.                             holder.progressBar.setVisibility(View.VISIBLE);
  81.                         }

  82.                         @Override
  83.                         public void onLoadingFailed(String imageUri, View view,
  84.                                 FailReason failReason) {
  85.                             holder.progressBar.setVisibility(View.GONE);
  86.                         }

  87.                         @Override
  88.                         public void onLoadingComplete(String imageUri,
  89.                                 View view, Bitmap loadedImage) {
  90.                             holder.progressBar.setVisibility(View.GONE);
  91.                         }

  92.                     }, new ImageLoadingProgressListener() {

  93.                         @Override
  94.                         public void onProgressUpdate(String imageUri,
  95.                                 View view, int current, int total) {
  96.                             holder.progressBar.setProgress(Math.round(100.0f
  97.                                     * current / total));
  98.                         }
  99.                     }); // 通过URL判断图片是否已经下载
  100.             return convertView;
  101.         }

  102.     }
  103. }
复制代码

里面主要的对象都用        突出显示了。

三者的关系

ImageLoaderConfiguration是针对图片缓存的全局配置,主要有线程类、缓存大小、磁盘大小、图片下载与解析、日志方面的配置。

ImageLoader是具体下载图片,缓存图片,显示图片的具体执行类,它有两个具体的方法displayImage(...)、loadImage(...),但是其实最终他们的实现都是displayImage(...)。

DisplayImageOptions用于指导每一个Imageloader根据网络图片的状态(空白、下载错误、正在下载)显示对应的图片,是否将缓存加载到磁盘上,下载完后对图片进行怎么样的处理。

从三者的协作关系上看,他们有点像厨房规定、厨师、客户个人口味之间的关系。ImageLoaderConfiguration就像是厨房里面的规定,每一个厨师要怎么着装,要怎么保持厨房的干净,这是针对每一个厨师都适用的规定,而且不允许个性化改变。ImageLoader就像是具体做菜的厨师,负责具体菜谱的制作。DisplayImageOptions就像每个客户的偏好,根据客户是重口味还是清淡,每一个imageLoader根据DisplayImageOptions的要求具体执行。


ImageLoaderConfiguration

在上面的示例代码中,我们使用ImageLoaderConfiguration的默认配置,下面给出ImageLoaderConfiguration比较详尽的配置,从下面的配置中,可以看出ImageLoaderConfiguration的配置主要是全局性的配置,主要有线程类、缓存大小、磁盘大小、图片下载与解析、日志方面的配置。

  1. ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
  2.         .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
  3.         .diskCacheExtraOptions(480, 800, null)
  4.         .taskExecutor(...)
  5.         .taskExecutorForCachedImages(...)
  6.         .threadPoolSize(3) // default
  7.         .threadPriority(Thread.NORM_PRIORITY - 1) // default
  8.         .tasksProcessingOrder(QueueProcessingType.FIFO) // default
  9.         .denyCacheImageMultipleSizesInMemory()
  10.         .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
  11.         .memoryCacheSize(2 * 1024 * 1024)
  12.         .memoryCacheSizePercentage(13) // default
  13.         .diskCache(new UnlimitedDiscCache(cacheDir)) // default
  14.         .diskCacheSize(50 * 1024 * 1024)
  15.         .diskCacheFileCount(100)
  16.         .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
  17.         .imageDownloader(new BaseImageDownloader(context)) // default
  18.         .imageDecoder(new BaseImageDecoder()) // default
  19.         .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
  20.         .writeDebugLogs()
  21.         .build();
复制代码

ImageLoaderConfiguration的主要职责就是记录相关的配置,它的内部其实就是一些字段的集合(如下面的源代码)。它有一个builder的内部类,这个类中的字段跟ImageLoaderConfiguration中的字段完全一致,它有一些默认值,通过修改builder可以配置ImageLoaderConfiguration。

  1. /*******************************************************************************
  2. * Copyright 2011-2013 Sergey Tarasevich
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *******************************************************************************/
  16. package com.nostra13.universalimageloader.core;

  17. import android.content.Context;
  18. import android.content.res.Resources;
  19. import android.util.DisplayMetrics;
  20. import com.nostra13.universalimageloader.cache.disc.DiskCache;
  21. import com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator;
  22. import com.nostra13.universalimageloader.cache.memory.MemoryCache;
  23. import com.nostra13.universalimageloader.cache.memory.impl.FuzzyKeyMemoryCache;
  24. import com.nostra13.universalimageloader.core.assist.FlushedInputStream;
  25. import com.nostra13.universalimageloader.core.assist.ImageSize;
  26. import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
  27. import com.nostra13.universalimageloader.core.decode.ImageDecoder;
  28. import com.nostra13.universalimageloader.core.download.ImageDownloader;
  29. import com.nostra13.universalimageloader.core.process.BitmapProcessor;
  30. import com.nostra13.universalimageloader.utils.L;
  31. import com.nostra13.universalimageloader.utils.MemoryCacheUtils;

  32. import java.io.IOException;
  33. import java.io.InputStream;
  34. import java.util.concurrent.Executor;

  35. /**
  36. * Presents configuration for {@link ImageLoader}
  37. *
  38. * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
  39. * @see ImageLoader
  40. * @see MemoryCache
  41. * @see DiskCache
  42. * @see DisplayImageOptions
  43. * @see ImageDownloader
  44. * @see FileNameGenerator
  45. * @since 1.0.0
  46. */
  47. public final class ImageLoaderConfiguration {

  48.     final Resources resources;

  49.     final int maxImageWidthForMemoryCache;
  50.     final int maxImageHeightForMemoryCache;
  51.     final int maxImageWidthForDiskCache;
  52.     final int maxImageHeightForDiskCache;
  53.     final BitmapProcessor processorForDiskCache;

  54.     final Executor taskExecutor;
  55.     final Executor taskExecutorForCachedImages;
  56.     final boolean customExecutor;
  57.     final boolean customExecutorForCachedImages;

  58.     final int threadPoolSize;
  59.     final int threadPriority;
  60.     final QueueProcessingType tasksProcessingType;

  61.     final MemoryCache memoryCache;
  62.     final DiskCache diskCache;
  63.     final ImageDownloader downloader;
  64.     final ImageDecoder decoder;
  65.     final DisplayImageOptions defaultDisplayImageOptions;

  66.     final ImageDownloader networkDeniedDownloader;
  67.     final ImageDownloader slowNetworkDownloader;

  68.     private ImageLoaderConfiguration(final Builder builder) {
  69.         resources = builder.context.getResources();
  70.         maxImageWidthForMemoryCache = builder.maxImageWidthForMemoryCache;
  71.         maxImageHeightForMemoryCache = builder.maxImageHeightForMemoryCache;
  72.         maxImageWidthForDiskCache = builder.maxImageWidthForDiskCache;
  73.         maxImageHeightForDiskCache = builder.maxImageHeightForDiskCache;
  74.         processorForDiskCache = builder.processorForDiskCache;
  75.         taskExecutor = builder.taskExecutor;
  76.         taskExecutorForCachedImages = builder.taskExecutorForCachedImages;
  77.         threadPoolSize = builder.threadPoolSize;
  78.         threadPriority = builder.threadPriority;
  79.         tasksProcessingType = builder.tasksProcessingType;
  80.         diskCache = builder.diskCache;
  81.         memoryCache = builder.memoryCache;
  82.         defaultDisplayImageOptions = builder.defaultDisplayImageOptions;
  83.         downloader = builder.downloader;
  84.         decoder = builder.decoder;

  85.         customExecutor = builder.customExecutor;
  86.         customExecutorForCachedImages = builder.customExecutorForCachedImages;

  87.         networkDeniedDownloader = new NetworkDeniedImageDownloader(downloader);
  88.         slowNetworkDownloader = new SlowNetworkImageDownloader(downloader);

  89.         L.writeDebugLogs(builder.writeLogs);
  90.     }

  91.     /**
  92.      * Creates default configuration for {@link ImageLoader} <br />
  93.      * <b>Default values:</b>
  94.      * <ul>
  95.      * <li>maxImageWidthForMemoryCache = device's screen width</li>
  96.      * <li>maxImageHeightForMemoryCache = device's screen height</li>
  97.      * <li>maxImageWidthForDikcCache = unlimited</li>
  98.      * <li>maxImageHeightForDiskCache = unlimited</li>
  99.      * <li>threadPoolSize = {@link Builder#DEFAULT_THREAD_POOL_SIZE this}</li>
  100.      * <li>threadPriority = {@link Builder#DEFAULT_THREAD_PRIORITY this}</li>
  101.      * <li>allow to cache different sizes of image in memory</li>
  102.      * <li>memoryCache = {@link DefaultConfigurationFactory#createMemoryCache(int)}</li>
  103.      * <li>diskCache = {@link com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache}</li>
  104.      * <li>imageDownloader = {@link DefaultConfigurationFactory#createImageDownloader(Context)}</li>
  105.      * <li>imageDecoder = {@link DefaultConfigurationFactory#createImageDecoder(boolean)}</li>
  106.      * <li>diskCacheFileNameGenerator = {@link DefaultConfigurationFactory#createFileNameGenerator()}</li>
  107.      * <li>defaultDisplayImageOptions = {@link DisplayImageOptions#createSimple() Simple options}</li>
  108.      * <li>tasksProcessingOrder = {@link QueueProcessingType#FIFO}</li>
  109.      * <li>detailed logging disabled</li>
  110.      * </ul>
  111.      */
  112.     public static ImageLoaderConfiguration createDefault(Context context) {
  113.         return new Builder(context).build();
  114.     }

  115.     ImageSize getMaxImageSize() {
  116.         DisplayMetrics displayMetrics = resources.getDisplayMetrics();

  117.         int width = maxImageWidthForMemoryCache;
  118.         if (width <= 0) {
  119.             width = displayMetrics.widthPixels;
  120.         }
  121.         int height = maxImageHeightForMemoryCache;
  122.         if (height <= 0) {
  123.             height = displayMetrics.heightPixels;
  124.         }
  125.         return new ImageSize(width, height);
  126.     }

  127.     /**
  128.      * Builder for {@link ImageLoaderConfiguration}
  129.      *
  130.      * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
  131.      */
  132.     public static class Builder {

  133.         private static final String WARNING_OVERLAP_DISK_CACHE_PARAMS = "diskCache(), diskCacheSize() and diskCacheFileCount calls overlap each other";
  134.         private static final String WARNING_OVERLAP_DISK_CACHE_NAME_GENERATOR = "diskCache() and diskCacheFileNameGenerator() calls overlap each other";
  135.         private static final String WARNING_OVERLAP_MEMORY_CACHE = "memoryCache() and memoryCacheSize() calls overlap each other";
  136.         private static final String WARNING_OVERLAP_EXECUTOR = "threadPoolSize(), threadPriority() and tasksProcessingOrder() calls "
  137.                 + "can overlap taskExecutor() and taskExecutorForCachedImages() calls.";

  138.         /** {@value} */
  139.         public static final int DEFAULT_THREAD_POOL_SIZE = 3;
  140.         /** {@value} */
  141.         public static final int DEFAULT_THREAD_PRIORITY = Thread.NORM_PRIORITY - 1;
  142.         /** {@value} */
  143.         public static final QueueProcessingType DEFAULT_TASK_PROCESSING_TYPE = QueueProcessingType.FIFO;

  144.         private Context context;

  145.         private int maxImageWidthForMemoryCache = 0;
  146.         private int maxImageHeightForMemoryCache = 0;
  147.         private int maxImageWidthForDiskCache = 0;
  148.         private int maxImageHeightForDiskCache = 0;
  149.         private BitmapProcessor processorForDiskCache = null;

  150.         private Executor taskExecutor = null;
  151.         private Executor taskExecutorForCachedImages = null;
  152.         private boolean customExecutor = false;
  153.         private boolean customExecutorForCachedImages = false;

  154.         private int threadPoolSize = DEFAULT_THREAD_POOL_SIZE;
  155.         private int threadPriority = DEFAULT_THREAD_PRIORITY;
  156.         private boolean denyCacheImageMultipleSizesInMemory = false;
  157.         private QueueProcessingType tasksProcessingType = DEFAULT_TASK_PROCESSING_TYPE;

  158.         private int memoryCacheSize = 0;
  159.         private long diskCacheSize = 0;
  160.         private int diskCacheFileCount = 0;

  161.         private MemoryCache memoryCache = null;
  162.         private DiskCache diskCache = null;
  163.         private FileNameGenerator diskCacheFileNameGenerator = null;
  164.         private ImageDownloader downloader = null;
  165.         private ImageDecoder decoder;
  166.         private DisplayImageOptions defaultDisplayImageOptions = null;

  167.         private boolean writeLogs = false;

  168.         public Builder(Context context) {
  169.             this.context = context.getApplicationContext();
  170.         }

  171.         /**
  172.          * Sets options for memory cache
  173.          *
  174.          * @param maxImageWidthForMemoryCache  Maximum image width which will be used for memory saving during decoding
  175.          *                                     an image to {@link android.graphics.Bitmap Bitmap}. <b>Default value - device's screen width</b>
  176.          * @param maxImageHeightForMemoryCache Maximum image height which will be used for memory saving during decoding
  177.          *                                     an image to {@link android.graphics.Bitmap Bitmap}. <b>Default value</b> - device's screen height
  178.          */
  179.         public Builder memoryCacheExtraOptions(int maxImageWidthForMemoryCache, int maxImageHeightForMemoryCache) {
  180.             this.maxImageWidthForMemoryCache = maxImageWidthForMemoryCache;
  181.             this.maxImageHeightForMemoryCache = maxImageHeightForMemoryCache;
  182.             return this;
  183.         }

  184.         /**
  185.          * @deprecated Use
  186.          * {@link #diskCacheExtraOptions(int, int, com.nostra13.universalimageloader.core.process.BitmapProcessor)}
  187.          * instead
  188.          */
  189.         @Deprecated
  190.         public Builder discCacheExtraOptions(int maxImageWidthForDiskCache, int maxImageHeightForDiskCache,
  191.                 BitmapProcessor processorForDiskCache) {
  192.             return diskCacheExtraOptions(maxImageWidthForDiskCache, maxImageHeightForDiskCache, processorForDiskCache);
  193.         }

  194.         /**
  195.          * Sets options for resizing/compressing of downloaded images before saving to disk cache.<br />
  196.          * <b>NOTE: Use this option only when you have appropriate needs. It can make ImageLoader slower.</b>
  197.          *
  198.          * @param maxImageWidthForDiskCache  Maximum width of downloaded images for saving at disk cache
  199.          * @param maxImageHeightForDiskCache Maximum height of downloaded images for saving at disk cache
  200.          * @param processorForDiskCache      null-ok; {@linkplain BitmapProcessor Bitmap processor} which process images before saving them in disc cache
  201.          */
  202.         public Builder diskCacheExtraOptions(int maxImageWidthForDiskCache, int maxImageHeightForDiskCache,
  203.                 BitmapProcessor processorForDiskCache) {
  204.             this.maxImageWidthForDiskCache = maxImageWidthForDiskCache;
  205.             this.maxImageHeightForDiskCache = maxImageHeightForDiskCache;
  206.             this.processorForDiskCache = processorForDiskCache;
  207.             return this;
  208.         }

  209.         /**
  210.          * Sets custom {@linkplain Executor executor} for tasks of loading and displaying images.<br />
  211.          * <br />
  212.          * <b>NOTE:</b> If you set custom executor then following configuration options will not be considered for this
  213.          * executor:
  214.          * <ul>
  215.          * <li>{@link #threadPoolSize(int)}</li>
  216.          * <li>{@link #threadPriority(int)}</li>
  217.          * <li>{@link #tasksProcessingOrder(QueueProcessingType)}</li>
  218.          * </ul>
  219.          *
  220.          * @see #taskExecutorForCachedImages(Executor)
  221.          */
  222.         public Builder taskExecutor(Executor executor) {
  223.             if (threadPoolSize != DEFAULT_THREAD_POOL_SIZE || threadPriority != DEFAULT_THREAD_PRIORITY || tasksProcessingType != DEFAULT_TASK_PROCESSING_TYPE) {
  224.                 L.w(WARNING_OVERLAP_EXECUTOR);
  225.             }

  226.             this.taskExecutor = executor;
  227.             return this;
  228.         }

  229.         /**
  230.          * Sets custom {@linkplain Executor executor} for tasks of displaying <b>cached on disk</b> images (these tasks
  231.          * are executed quickly so UIL prefer to use separate executor for them).<br />
  232.          * <br />
  233.          * If you set the same executor for {@linkplain #taskExecutor(Executor) general tasks} and
  234.          * tasks about cached images (this method) then these tasks will be in the
  235.          * same thread pool. So short-lived tasks can wait a long time for their turn.<br />
  236.          * <br />
  237.          * <b>NOTE:</b> If you set custom executor then following configuration options will not be considered for this
  238.          * executor:
  239.          * <ul>
  240.          * <li>{@link #threadPoolSize(int)}</li>
  241.          * <li>{@link #threadPriority(int)}</li>
  242.          * <li>{@link #tasksProcessingOrder(QueueProcessingType)}</li>
  243.          * </ul>
  244.          *
  245.          * @see #taskExecutor(Executor)
  246.          */
  247.         public Builder taskExecutorForCachedImages(Executor executorForCachedImages) {
  248.             if (threadPoolSize != DEFAULT_THREAD_POOL_SIZE || threadPriority != DEFAULT_THREAD_PRIORITY || tasksProcessingType != DEFAULT_TASK_PROCESSING_TYPE) {
  249.                 L.w(WARNING_OVERLAP_EXECUTOR);
  250.             }

  251.             this.taskExecutorForCachedImages = executorForCachedImages;
  252.             return this;
  253.         }

  254.         /**
  255.          * Sets thread pool size for image display tasks.<br />
  256.          * Default value - {@link #DEFAULT_THREAD_POOL_SIZE this}
  257.          */
  258.         public Builder threadPoolSize(int threadPoolSize) {
  259.             if (taskExecutor != null || taskExecutorForCachedImages != null) {
  260.                 L.w(WARNING_OVERLAP_EXECUTOR);
  261.             }

  262.             this.threadPoolSize = threadPoolSize;
  263.             return this;
  264.         }

  265.         /**
  266.          * Sets the priority for image loading threads. Should be <b>NOT</b> greater than {@link Thread#MAX_PRIORITY} or
  267.          * less than {@link Thread#MIN_PRIORITY}<br />
  268.          * Default value - {@link #DEFAULT_THREAD_PRIORITY this}
  269.          */
  270.         public Builder threadPriority(int threadPriority) {
  271.             if (taskExecutor != null || taskExecutorForCachedImages != null) {
  272.                 L.w(WARNING_OVERLAP_EXECUTOR);
  273.             }

  274.             if (threadPriority < Thread.MIN_PRIORITY) {
  275.                 this.threadPriority = Thread.MIN_PRIORITY;
  276.             } else {
  277.                 if (threadPriority > Thread.MAX_PRIORITY) {
  278.                     this.threadPriority = Thread.MAX_PRIORITY;
  279.                 } else {
  280.                     this.threadPriority = threadPriority;
  281.                 }
  282.             }
  283.             return this;
  284.         }

  285.         /**
  286.          * When you display an image in a small {@link android.widget.ImageView ImageView} and later you try to display
  287.          * this image (from identical URI) in a larger {@link android.widget.ImageView ImageView} so decoded image of
  288.          * bigger size will be cached in memory as a previous decoded image of smaller size.<br />
  289.          * So <b>the default behavior is to allow to cache multiple sizes of one image in memory</b>. You can
  290.          * <b>deny</b> it by calling <b>this</b> method: so when some image will be cached in memory then previous
  291.          * cached size of this image (if it exists) will be removed from memory cache before.
  292.          */
  293.         public Builder denyCacheImageMultipleSizesInMemory() {
  294.             this.denyCacheImageMultipleSizesInMemory = true;
  295.             return this;
  296.         }

  297.         /**
  298.          * Sets type of queue processing for tasks for loading and displaying images.<br />
  299.          * Default value - {@link QueueProcessingType#FIFO}
  300.          */
  301.         public Builder tasksProcessingOrder(QueueProcessingType tasksProcessingType) {
  302.             if (taskExecutor != null || taskExecutorForCachedImages != null) {
  303.                 L.w(WARNING_OVERLAP_EXECUTOR);
  304.             }

  305.             this.tasksProcessingType = tasksProcessingType;
  306.             return this;
  307.         }

  308.         /**
  309.          * Sets maximum memory cache size for {@link android.graphics.Bitmap bitmaps} (in bytes).<br />
  310.          * Default value - 1/8 of available app memory.<br />
  311.          * <b>NOTE:</b> If you use this method then
  312.          * {@link com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache LruMemoryCache} will be used as
  313.          * memory cache. You can use {@link #memoryCache(MemoryCache)} method to set your own implementation of
  314.          * {@link MemoryCache}.
  315.          */
  316.         public Builder memoryCacheSize(int memoryCacheSize) {
  317.             if (memoryCacheSize <= 0) throw new IllegalArgumentException("memoryCacheSize must be a positive number");

  318.             if (memoryCache != null) {
  319.                 L.w(WARNING_OVERLAP_MEMORY_CACHE);
  320.             }

  321.             this.memoryCacheSize = memoryCacheSize;
  322.             return this;
  323.         }

  324.         /**
  325.          * Sets maximum memory cache size (in percent of available app memory) for {@link android.graphics.Bitmap
  326.          * bitmaps}.<br />
  327.          * Default value - 1/8 of available app memory.<br />
  328.          * <b>NOTE:</b> If you use this method then
  329.          * {@link com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache LruMemoryCache} will be used as
  330.          * memory cache. You can use {@link #memoryCache(MemoryCache)} method to set your own implementation of
  331.          * {@link MemoryCache}.
  332.          */
  333.         public Builder memoryCacheSizePercentage(int availableMemoryPercent) {
  334.             if (availableMemoryPercent <= 0 || availableMemoryPercent >= 100) {
  335.                 throw new IllegalArgumentException("availableMemoryPercent must be in range (0 < % < 100)");
  336.             }

  337.             if (memoryCache != null) {
  338.                 L.w(WARNING_OVERLAP_MEMORY_CACHE);
  339.             }

  340.             long availableMemory = Runtime.getRuntime().maxMemory();
  341.             memoryCacheSize = (int) (availableMemory * (availableMemoryPercent / 100f));
  342.             return this;
  343.         }

  344.         /**
  345.          * Sets memory cache for {@link android.graphics.Bitmap bitmaps}.<br />
  346.          * Default value - {@link com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache LruMemoryCache}
  347.          * with limited memory cache size (size = 1/8 of available app memory)<br />
  348.          * <br />
  349.          * <b>NOTE:</b> If you set custom memory cache then following configuration option will not be considered:
  350.          * <ul>
  351.          * <li>{@link #memoryCacheSize(int)}</li>
  352.          * </ul>
  353.          */
  354.         public Builder memoryCache(MemoryCache memoryCache) {
  355.             if (memoryCacheSize != 0) {
  356.                 L.w(WARNING_OVERLAP_MEMORY_CACHE);
  357.             }

  358.             this.memoryCache = memoryCache;
  359.             return this;
  360.         }

  361.         /** @deprecated Use {@link #diskCacheSize(int)} instead */
  362.         @Deprecated
  363.         public Builder discCacheSize(int maxCacheSize) {
  364.             return diskCacheSize(maxCacheSize);
  365.         }

  366.         /**
  367.          * Sets maximum disk cache size for images (in bytes).<br />
  368.          * By default: disk cache is unlimited.<br />
  369.          * <b>NOTE:</b> If you use this method then
  370.          * {@link com.nostra13.universalimageloader.cache.disc.impl.ext.LruDiscCache LruDiscCache}
  371.          * will be used as disk cache. You can use {@link #diskCache(DiskCache)} method for introduction your own
  372.          * implementation of {@link DiskCache}
  373.          */
  374.         public Builder diskCacheSize(int maxCacheSize) {
  375.             if (maxCacheSize <= 0) throw new IllegalArgumentException("maxCacheSize must be a positive number");

  376.             if (diskCache != null) {
  377.                 L.w(WARNING_OVERLAP_DISK_CACHE_PARAMS);
  378.             }

  379.             this.diskCacheSize = maxCacheSize;
  380.             return this;
  381.         }

  382.         /** @deprecated Use {@link #diskCacheFileCount(int)} instead */
  383.         @Deprecated
  384.         public Builder discCacheFileCount(int maxFileCount) {
  385.             return diskCacheFileCount(maxFileCount);
  386.         }

  387.         /**
  388.          * Sets maximum file count in disk cache directory.<br />
  389.          * By default: disk cache is unlimited.<br />
  390.          * <b>NOTE:</b> If you use this method then
  391.          * {@link com.nostra13.universalimageloader.cache.disc.impl.ext.LruDiscCache LruDiscCache}
  392.          * will be used as disk cache. You can use {@link #diskCache(DiskCache)} method for introduction your own
  393.          * implementation of {@link DiskCache}
  394.          */
  395.         public Builder diskCacheFileCount(int maxFileCount) {
  396.             if (maxFileCount <= 0) throw new IllegalArgumentException("maxFileCount must be a positive number");

  397.             if (diskCache != null) {
  398.                 L.w(WARNING_OVERLAP_DISK_CACHE_PARAMS);
  399.             }

  400.             this.diskCacheFileCount = maxFileCount;
  401.             return this;
  402.         }

  403.         /** @deprecated Use {@link #diskCacheFileNameGenerator(com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator)} */
  404.         @Deprecated
  405.         public Builder discCacheFileNameGenerator(FileNameGenerator fileNameGenerator) {
  406.             return diskCacheFileNameGenerator(fileNameGenerator);
  407.         }

  408.         /**
  409.          * Sets name generator for files cached in disk cache.<br />
  410.          * Default value -
  411.          * {@link com.nostra13.universalimageloader.core.DefaultConfigurationFactory#createFileNameGenerator()
  412.          * DefaultConfigurationFactory.createFileNameGenerator()}
  413.          */
  414.         public Builder diskCacheFileNameGenerator(FileNameGenerator fileNameGenerator) {
  415.             if (diskCache != null) {
  416.                 L.w(WARNING_OVERLAP_DISK_CACHE_NAME_GENERATOR);
  417.             }

  418.             this.diskCacheFileNameGenerator = fileNameGenerator;
  419.             return this;
  420.         }

  421.         /** @deprecated Use {@link #diskCache(com.nostra13.universalimageloader.cache.disc.DiskCache)} */
  422.         @Deprecated
  423.         public Builder discCache(DiskCache diskCache) {
  424.             return diskCache(diskCache);
  425.         }

  426.         /**
  427.          * Sets disk cache for images.<br />
  428.          * Default value - {@link com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache
  429.          * BaseDiscCache}. Cache directory is defined by
  430.          * {@link com.nostra13.universalimageloader.utils.StorageUtils#getCacheDirectory(Context)
  431.          * StorageUtils.getCacheDirectory(Context)}.<br />
  432.          * <br />
  433.          * <b>NOTE:</b> If you set custom disk cache then following configuration option will not be considered:
  434.          * <ul>
  435.          * <li>{@link #diskCacheSize(int)}</li>
  436.          * <li>{@link #diskCacheFileCount(int)}</li>
  437.          * <li>{@link #diskCacheFileNameGenerator(FileNameGenerator)}</li>
  438.          * </ul>
  439.          */
  440.         public Builder diskCache(DiskCache diskCache) {
  441.             if (diskCacheSize > 0 || diskCacheFileCount > 0) {
  442.                 L.w(WARNING_OVERLAP_DISK_CACHE_PARAMS);
  443.             }
  444.             if (diskCacheFileNameGenerator != null) {
  445.                 L.w(WARNING_OVERLAP_DISK_CACHE_NAME_GENERATOR);
  446.             }

  447.             this.diskCache = diskCache;
  448.             return this;
  449.         }

  450.         /**
  451.          * Sets utility which will be responsible for downloading of image.<br />
  452.          * Default value -
  453.          * {@link com.nostra13.universalimageloader.core.DefaultConfigurationFactory#createImageDownloader(Context)
  454.          * DefaultConfigurationFactory.createImageDownloader()}
  455.          */
  456.         public Builder imageDownloader(ImageDownloader imageDownloader) {
  457.             this.downloader = imageDownloader;
  458.             return this;
  459.         }

  460.         /**
  461.          * Sets utility which will be responsible for decoding of image stream.<br />
  462.          * Default value -
  463.          * {@link com.nostra13.universalimageloader.core.DefaultConfigurationFactory#createImageDecoder(boolean)
  464.          * DefaultConfigurationFactory.createImageDecoder()}
  465.          */
  466.         public Builder imageDecoder(ImageDecoder imageDecoder) {
  467.             this.decoder = imageDecoder;
  468.             return this;
  469.         }

  470.         /**
  471.          * Sets default {@linkplain DisplayImageOptions display image options} for image displaying. These options will
  472.          * be used for every {@linkplain ImageLoader#displayImage(String, android.widget.ImageView) image display call}
  473.          * without passing custom {@linkplain DisplayImageOptions options}<br />
  474.          * Default value - {@link DisplayImageOptions#createSimple() Simple options}
  475.          */
  476.         public Builder defaultDisplayImageOptions(DisplayImageOptions defaultDisplayImageOptions) {
  477.             this.defaultDisplayImageOptions = defaultDisplayImageOptions;
  478.             return this;
  479.         }

  480.         /**
  481.          * Enables detail logging of {@link ImageLoader} work. To prevent detail logs don't call this method.
  482.          * Consider {@link com.nostra13.universalimageloader.utils.L#disableLogging()} to disable
  483.          * ImageLoader logging completely (even error logs)
  484.          */
  485.         public Builder writeDebugLogs() {
  486.             this.writeLogs = true;
  487.             return this;
  488.         }

  489.         /** Builds configured {@link ImageLoaderConfiguration} object */
  490.         public ImageLoaderConfiguration build() {
  491.             initEmptyFieldsWithDefaultValues();
  492.             return new ImageLoaderConfiguration(this);
  493.         }

  494.         private void initEmptyFieldsWithDefaultValues() {
  495.             if (taskExecutor == null) {
  496.                 taskExecutor = DefaultConfigurationFactory
  497.                         .createExecutor(threadPoolSize, threadPriority, tasksProcessingType);
  498.             } else {
  499.                 customExecutor = true;
  500.             }
  501.             if (taskExecutorForCachedImages == null) {
  502.                 taskExecutorForCachedImages = DefaultConfigurationFactory
  503.                         .createExecutor(threadPoolSize, threadPriority, tasksProcessingType);
  504.             } else {
  505.                 customExecutorForCachedImages = true;
  506.             }
  507.             if (diskCache == null) {
  508.                 if (diskCacheFileNameGenerator == null) {
  509.                     diskCacheFileNameGenerator = DefaultConfigurationFactory.createFileNameGenerator();
  510.                 }
  511.                 diskCache = DefaultConfigurationFactory
  512.                         .createDiskCache(context, diskCacheFileNameGenerator, diskCacheSize, diskCacheFileCount);
  513.             }
  514.             if (memoryCache == null) {
  515.                 memoryCache = DefaultConfigurationFactory.createMemoryCache(memoryCacheSize);
  516.             }
  517.             if (denyCacheImageMultipleSizesInMemory) {
  518.                 memoryCache = new FuzzyKeyMemoryCache(memoryCache, MemoryCacheUtils.createFuzzyKeyComparator());
  519.             }
  520.             if (downloader == null) {
  521.                 downloader = DefaultConfigurationFactory.createImageDownloader(context);
  522.             }
  523.             if (decoder == null) {
  524.                 decoder = DefaultConfigurationFactory.createImageDecoder(writeLogs);
  525.             }
  526.             if (defaultDisplayImageOptions == null) {
  527.                 defaultDisplayImageOptions = DisplayImageOptions.createSimple();
  528.             }
  529.         }
  530.     }

  531.     /**
  532.      * Decorator. Prevents downloads from network (throws {@link IllegalStateException exception}).<br />
  533.      * In most cases this downloader shouldn't be used directly.
  534.      *
  535.      * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
  536.      * @since 1.8.0
  537.      */
  538.     private static class NetworkDeniedImageDownloader implements ImageDownloader {

  539.         private final ImageDownloader wrappedDownloader;

  540.         public NetworkDeniedImageDownloader(ImageDownloader wrappedDownloader) {
  541.             this.wrappedDownloader = wrappedDownloader;
  542.         }

  543.         @Override
  544.         public InputStream getStream(String imageUri, Object extra) throws IOException {
  545.             switch (Scheme.ofUri(imageUri)) {
  546.                 case HTTP:
  547.                 case HTTPS:
  548.                     throw new IllegalStateException();
  549.                 default:
  550.                     return wrappedDownloader.getStream(imageUri, extra);
  551.             }
  552.         }
  553.     }

  554.     /**
  555.      * Decorator. Handles <a href="http://code.google.com/p/android/issues/detail?id=6066">this problem</a> on slow networks
  556.      * using {@link com.nostra13.universalimageloader.core.assist.FlushedInputStream}.
  557.      *
  558.      * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
  559.      * @since 1.8.1
  560.      */
  561.     private static class SlowNetworkImageDownloader implements ImageDownloader {

  562.         private final ImageDownloader wrappedDownloader;

  563.         public SlowNetworkImageDownloader(ImageDownloader wrappedDownloader) {
  564.             this.wrappedDownloader = wrappedDownloader;
  565.         }

  566.         @Override
  567.         public InputStream getStream(String imageUri, Object extra) throws IOException {
  568.             InputStream imageStream = wrappedDownloader.getStream(imageUri, extra);
  569.             switch (Scheme.ofUri(imageUri)) {
  570.                 case HTTP:
  571.                 case HTTPS:
  572.                     return new FlushedInputStream(imageStream);
  573.                 default:
  574.                     return imageStream;
  575.             }
  576.         }
  577.     }
  578. }
复制代码
Display Options

每一个ImageLoader.displayImage(...)都可以使用Display Options

  1. DisplayImageOptions options = new DisplayImageOptions.Builder()
  2.         .showImageOnLoading(R.drawable.ic_stub) // resource or drawable
  3.         .showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable
  4.         .showImageOnFail(R.drawable.ic_error) // resource or drawable
  5.         .resetViewBeforeLoading(false)  // default
  6.         .delayBeforeLoading(1000)
  7.         .cacheInMemory(false) // default
  8.         .cacheOnDisk(false) // default
  9.         .preProcessor(...)
  10.         .postProcessor(...)
  11.         .extraForDownloader(...)
  12.         .considerExifParams(false) // default
  13.         .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
  14.         .bitmapConfig(Bitmap.Config.ARGB_8888) // default
  15.         .decodingOptions(...)
  16.         .displayer(new SimpleBitmapDisplayer()) // default
  17.         .handler(new Handler()) // default
  18.         .build();
复制代码

Display Options的主要职责就是记录相关的配置,它的内部其实就是一些字段的集合(如下面的源代码)。它有一个builder的内部类,这个类中的字段跟DisplayOption中的字段完全一致,它有一些默认值,通过修改builder可以配置DisplayOptions。

  1. public final class DisplayImageOptions {

  2.     private final int imageResOnLoading;
  3.     private final int imageResForEmptyUri;
  4.     private final int imageResOnFail;
  5.     private final Drawable imageOnLoading;
  6.     private final Drawable imageForEmptyUri;
  7.     private final Drawable imageOnFail;
  8.     private final boolean resetViewBeforeLoading;
  9.     private final boolean cacheInMemory;
  10.     private final boolean cacheOnDisk;
  11.     private final ImageScaleType imageScaleType;
  12.     private final Options decodingOptions;
  13.     private final int delayBeforeLoading;
  14.     private final boolean considerExifParams;
  15.     private final Object extraForDownloader;
  16.     private final BitmapProcessor preProcessor;
  17.     private final BitmapProcessor postProcessor;
  18.     private final BitmapDisplayer displayer;
  19.     private final Handler handler;
  20.     private final boolean isSyncLoading;

  21.     private DisplayImageOptions(Builder builder) {
  22.         imageResOnLoading = builder.imageResOnLoading;
  23.         imageResForEmptyUri = builder.imageResForEmptyUri;
  24.         imageResOnFail = builder.imageResOnFail;
  25.         imageOnLoading = builder.imageOnLoading;
  26.         imageForEmptyUri = builder.imageForEmptyUri;
  27.         imageOnFail = builder.imageOnFail;
  28.         resetViewBeforeLoading = builder.resetViewBeforeLoading;
  29.         cacheInMemory = builder.cacheInMemory;
  30.         cacheOnDisk = builder.cacheOnDisk;
  31.         imageScaleType = builder.imageScaleType;
  32.         decodingOptions = builder.decodingOptions;
  33.         delayBeforeLoading = builder.delayBeforeLoading;
  34.         considerExifParams = builder.considerExifParams;
  35.         extraForDownloader = builder.extraForDownloader;
  36.         preProcessor = builder.preProcessor;
  37.         postProcessor = builder.postProcessor;
  38.         displayer = builder.displayer;
  39.         handler = builder.handler;
  40.         isSyncLoading = builder.isSyncLoading;
  41.     }

  42.     public boolean shouldShowImageOnLoading() {
  43.         return imageOnLoading != null || imageResOnLoading != 0;
  44.     }

  45.     public boolean shouldShowImageForEmptyUri() {
  46.         return imageForEmptyUri != null || imageResForEmptyUri != 0;
  47.     }

  48.     public boolean shouldShowImageOnFail() {
  49.         return imageOnFail != null || imageResOnFail != 0;
  50.     }

  51.     public boolean shouldPreProcess() {
  52.         return preProcessor != null;
  53.     }

  54.     public boolean shouldPostProcess() {
  55.         return postProcessor != null;
  56.     }

  57.     public boolean shouldDelayBeforeLoading() {
  58.         return delayBeforeLoading > 0;
  59.     }

  60.     public Drawable getImageOnLoading(Resources res) {
  61.         return imageResOnLoading != 0 ? res.getDrawable(imageResOnLoading) : imageOnLoading;
  62.     }

  63.     public Drawable getImageForEmptyUri(Resources res) {
  64.         return imageResForEmptyUri != 0 ? res.getDrawable(imageResForEmptyUri) : imageForEmptyUri;
  65.     }

  66.     public Drawable getImageOnFail(Resources res) {
  67.         return imageResOnFail != 0 ? res.getDrawable(imageResOnFail) : imageOnFail;
  68.     }

  69.     public boolean isResetViewBeforeLoading() {
  70.         return resetViewBeforeLoading;
  71.     }

  72.     public boolean isCacheInMemory() {
  73.         return cacheInMemory;
  74.     }

  75.     public boolean isCacheOnDisk() {
  76.         return cacheOnDisk;
  77.     }

  78.     public ImageScaleType getImageScaleType() {
  79.         return imageScaleType;
  80.     }

  81.     public Options getDecodingOptions() {
  82.         return decodingOptions;
  83.     }

  84.     public int getDelayBeforeLoading() {
  85.         return delayBeforeLoading;
  86.     }

  87.     public boolean isConsiderExifParams() {
  88.         return considerExifParams;
  89.     }

  90.     public Object getExtraForDownloader() {
  91.         return extraForDownloader;
  92.     }

  93.     public BitmapProcessor getPreProcessor() {
  94.         return preProcessor;
  95.     }

  96.     public BitmapProcessor getPostProcessor() {
  97.         return postProcessor;
  98.     }

  99.     public BitmapDisplayer getDisplayer() {
  100.         return displayer;
  101.     }

  102.     public Handler getHandler() {
  103.         return handler;
  104.     }

  105.     boolean isSyncLoading() {
  106.         return isSyncLoading;
  107.     }

  108.     /**
  109.      * Builder for {@link DisplayImageOptions}
  110.      *
  111.      * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
  112.      */
  113.     public static class Builder {
  114.         private int imageResOnLoading = 0;
  115.         private int imageResForEmptyUri = 0;
  116.         private int imageResOnFail = 0;
  117.         private Drawable imageOnLoading = null;
  118.         private Drawable imageForEmptyUri = null;
  119.         private Drawable imageOnFail = null;
  120.         private boolean resetViewBeforeLoading = false;
  121.         private boolean cacheInMemory = false;
  122.         private boolean cacheOnDisk = false;
  123.         private ImageScaleType imageScaleType = ImageScaleType.IN_SAMPLE_POWER_OF_2;
  124.         private Options decodingOptions = new Options();
  125.         private int delayBeforeLoading = 0;
  126.         private boolean considerExifParams = false;
  127.         private Object extraForDownloader = null;
  128.         private BitmapProcessor preProcessor = null;
  129.         private BitmapProcessor postProcessor = null;
  130.         private BitmapDisplayer displayer = DefaultConfigurationFactory.createBitmapDisplayer();
  131.         private Handler handler = null;
  132.         private boolean isSyncLoading = false;

  133.         public Builder() {
  134.             decodingOptions.inPurgeable = true;
  135.             decodingOptions.inInputShareable = true;
  136.         }

  137.         /**
  138.          * Stub image will be displayed in {@link com.nostra13.universalimageloader.core.imageaware.ImageAware
  139.          * image aware view} during image loading
  140.          *
  141.          * @param imageRes Stub image resource
  142.          * @deprecated Use {@link #showImageOnLoading(int)} instead
  143.          */
  144.         @Deprecated
  145.         public Builder showStubImage(int imageRes) {
  146.             imageResOnLoading = imageRes;
  147.             return this;
  148.         }

  149.         /**
  150.          * Incoming image will be displayed in {@link com.nostra13.universalimageloader.core.imageaware.ImageAware
  151.          * image aware view} during image loading
  152.          *
  153.          * @param imageRes Image resource
  154.          */
  155.         public Builder showImageOnLoading(int imageRes) {
  156.             imageResOnLoading = imageRes;
  157.             return this;
  158.         }

  159.         /**
  160.          * Incoming drawable will be displayed in {@link com.nostra13.universalimageloader.core.imageaware.ImageAware
  161.          * image aware view} during image loading.
  162.          * This option will be ignored if {@link DisplayImageOptions.Builder#showImageOnLoading(int)} is set.
  163.          */
  164.         public Builder showImageOnLoading(Drawable drawable) {
  165.             imageOnLoading = drawable;
  166.             return this;
  167.         }

  168.         /**
  169.          * Incoming image will be displayed in {@link com.nostra13.universalimageloader.core.imageaware.ImageAware
  170.          * image aware view} if empty URI (null or empty
  171.          * string) will be passed to <b>ImageLoader.displayImage(...)</b> method.
  172.          *
  173.          * @param imageRes Image resource
  174.          */
  175.         public Builder showImageForEmptyUri(int imageRes) {
  176.             imageResForEmptyUri = imageRes;
  177.             return this;
  178.         }

  179.         /**
  180.          * Incoming drawable will be displayed in {@link com.nostra13.universalimageloader.core.imageaware.ImageAware
  181.          * image aware view} if empty URI (null or empty
  182.          * string) will be passed to <b>ImageLoader.displayImage(...)</b> method.
  183.          * This option will be ignored if {@link DisplayImageOptions.Builder#showImageForEmptyUri(int)} is set.
  184.          */
  185.         public Builder showImageForEmptyUri(Drawable drawable) {
  186.             imageForEmptyUri = drawable;
  187.             return this;
  188.         }

  189.         /**
  190.          * Incoming image will be displayed in {@link com.nostra13.universalimageloader.core.imageaware.ImageAware
  191.          * image aware view} if some error occurs during
  192.          * requested image loading/decoding.
  193.          *
  194.          * @param imageRes Image resource
  195.          */
  196.         public Builder showImageOnFail(int imageRes) {
  197.             imageResOnFail = imageRes;
  198.             return this;
  199.         }

  200.         /**
  201.          * Incoming drawable will be displayed in {@link com.nostra13.universalimageloader.core.imageaware.ImageAware
  202.          * image aware view} if some error occurs during
  203.          * requested image loading/decoding.
  204.          * This option will be ignored if {@link DisplayImageOptions.Builder#showImageOnFail(int)} is set.
  205.          */
  206.         public Builder showImageOnFail(Drawable drawable) {
  207.             imageOnFail = drawable;
  208.             return this;
  209.         }

  210.         /**
  211.          * {@link com.nostra13.universalimageloader.core.imageaware.ImageAware
  212.          * image aware view} will be reset (set <b>null</b>) before image loading start
  213.          *
  214.          * @deprecated Use {@link #resetViewBeforeLoading(boolean) resetViewBeforeLoading(true)} instead
  215.          */
  216.         public Builder resetViewBeforeLoading() {
  217.             resetViewBeforeLoading = true;
  218.             return this;
  219.         }

  220.         /**
  221.          * Sets whether {@link com.nostra13.universalimageloader.core.imageaware.ImageAware
  222.          * image aware view} will be reset (set <b>null</b>) before image loading start
  223.          */
  224.         public Builder resetViewBeforeLoading(boolean resetViewBeforeLoading) {
  225.             this.resetViewBeforeLoading = resetViewBeforeLoading;
  226.             return this;
  227.         }

  228.         /**
  229.          * Loaded image will be cached in memory
  230.          *
  231.          * @deprecated Use {@link #cacheInMemory(boolean) cacheInMemory(true)} instead
  232.          */
  233.         @Deprecated
  234.         public Builder cacheInMemory() {
  235.             cacheInMemory = true;
  236.             return this;
  237.         }

  238.         /** Sets whether loaded image will be cached in memory */
  239.         public Builder cacheInMemory(boolean cacheInMemory) {
  240.             this.cacheInMemory = cacheInMemory;
  241.             return this;
  242.         }

  243.         /**
  244.          * Loaded image will be cached on disk
  245.          *
  246.          * @deprecated Use {@link #cacheOnDisk(boolean) cacheOnDisk(true)} instead
  247.          */
  248.         @Deprecated
  249.         public Builder cacheOnDisc() {
  250.             return cacheOnDisk(true);
  251.         }

  252.         /**
  253.          * Sets whether loaded image will be cached on disk
  254.          *
  255.          * @deprecated Use {@link #cacheOnDisk(boolean)} instead
  256.          */
  257.         @Deprecated
  258.         public Builder cacheOnDisc(boolean cacheOnDisk) {
  259.             return cacheOnDisk(cacheOnDisk);
  260.         }

  261.         /** Sets whether loaded image will be cached on disk */
  262.         public Builder cacheOnDisk(boolean cacheOnDisk) {
  263.             this.cacheOnDisk = cacheOnDisk;
  264.             return this;
  265.         }

  266.         /**
  267.          * Sets {@linkplain ImageScaleType scale type} for decoding image. This parameter is used while define scale
  268.          * size for decoding image to Bitmap. Default value - {@link ImageScaleType#IN_SAMPLE_POWER_OF_2}
  269.          */
  270.         public Builder imageScaleType(ImageScaleType imageScaleType) {
  271.             this.imageScaleType = imageScaleType;
  272.             return this;
  273.         }

  274.         /** Sets {@link Bitmap.Config bitmap config} for image decoding. Default value - {@link Bitmap.Config#ARGB_8888} */
  275.         public Builder bitmapConfig(Bitmap.Config bitmapConfig) {
  276.             if (bitmapConfig == null) throw new IllegalArgumentException("bitmapConfig can't be null");
  277.             decodingOptions.inPreferredConfig = bitmapConfig;
  278.             return this;
  279.         }

  280.         /**
  281.          * Sets options for image decoding.<br />
  282.          * <b>NOTE:</b> {@link Options#inSampleSize} of incoming options will <b>NOT</b> be considered. Library
  283.          * calculate the most appropriate sample size itself according yo {@link #imageScaleType(ImageScaleType)}
  284.          * options.<br />
  285.          * <b>NOTE:</b> This option overlaps {@link #bitmapConfig(android.graphics.Bitmap.Config) bitmapConfig()}
  286.          * option.
  287.          */
  288.         public Builder decodingOptions(Options decodingOptions) {
  289.             if (decodingOptions == null) throw new IllegalArgumentException("decodingOptions can't be null");
  290.             this.decodingOptions = decodingOptions;
  291.             return this;
  292.         }

  293.         /** Sets delay time before starting loading task. Default - no delay. */
  294.         public Builder delayBeforeLoading(int delayInMillis) {
  295.             this.delayBeforeLoading = delayInMillis;
  296.             return this;
  297.         }

  298.         /** Sets auxiliary object which will be passed to {@link ImageDownloader#getStream(String, Object)} */
  299.         public Builder extraForDownloader(Object extra) {
  300.             this.extraForDownloader = extra;
  301.             return this;
  302.         }

  303.         /** Sets whether ImageLoader will consider EXIF parameters of JPEG image (rotate, flip) */
  304.         public Builder considerExifParams(boolean considerExifParams) {
  305.             this.considerExifParams = considerExifParams;
  306.             return this;
  307.         }

  308.         /**
  309.          * Sets bitmap processor which will be process bitmaps before they will be cached in memory. So memory cache
  310.          * will contain bitmap processed by incoming preProcessor.<br />
  311.          * Image will be pre-processed even if caching in memory is disabled.
  312.          */
  313.         public Builder preProcessor(BitmapProcessor preProcessor) {
  314.             this.preProcessor = preProcessor;
  315.             return this;
  316.         }

  317.         /**
  318.          * Sets bitmap processor which will be process bitmaps before they will be displayed in
  319.          * {@link com.nostra13.universalimageloader.core.imageaware.ImageAware image aware view} but
  320.          * after they'll have been saved in memory cache.
  321.          */
  322.         public Builder postProcessor(BitmapProcessor postProcessor) {
  323.             this.postProcessor = postProcessor;
  324.             return this;
  325.         }

  326.         /**
  327.          * Sets custom {@link BitmapDisplayer displayer} for image loading task. Default value -
  328.          * {@link DefaultConfigurationFactory#createBitmapDisplayer()}
  329.          */
  330.         public Builder displayer(BitmapDisplayer displayer) {
  331.             if (displayer == null) throw new IllegalArgumentException("displayer can't be null");
  332.             this.displayer = displayer;
  333.             return this;
  334.         }

  335.         Builder syncLoading(boolean isSyncLoading) {
  336.             this.isSyncLoading = isSyncLoading;
  337.             return this;
  338.         }

  339.         /**
  340.          * Sets custom {@linkplain Handler handler} for displaying images and firing {@linkplain ImageLoadingListener
  341.          * listener} events.
  342.          */
  343.         public Builder handler(Handler handler) {
  344.             this.handler = handler;
  345.             return this;
  346.         }

  347.         /** Sets all options equal to incoming options */
  348.         public Builder cloneFrom(DisplayImageOptions options) {
  349.             imageResOnLoading = options.imageResOnLoading;
  350.             imageResForEmptyUri = options.imageResForEmptyUri;
  351.             imageResOnFail = options.imageResOnFail;
  352.             imageOnLoading = options.imageOnLoading;
  353.             imageForEmptyUri = options.imageForEmptyUri;
  354.             imageOnFail = options.imageOnFail;
  355.             resetViewBeforeLoading = options.resetViewBeforeLoading;
  356.             cacheInMemory = options.cacheInMemory;
  357.             cacheOnDisk = options.cacheOnDisk;
  358.             imageScaleType = options.imageScaleType;
  359.             decodingOptions = options.decodingOptions;
  360.             delayBeforeLoading = options.delayBeforeLoading;
  361.             considerExifParams = options.considerExifParams;
  362.             extraForDownloader = options.extraForDownloader;
  363.             preProcessor = options.preProcessor;
  364.             postProcessor = options.postProcessor;
  365.             displayer = options.displayer;
  366.             handler = options.handler;
  367.             isSyncLoading = options.isSyncLoading;
  368.             return this;
  369.         }

  370.         /** Builds configured {@link DisplayImageOptions} object */
  371.         public DisplayImageOptions build() {
  372.             return new DisplayImageOptions(this);
  373.         }
  374.     }

  375.     /**
  376.      * Creates options appropriate for single displaying:
  377.      * <ul>
  378.      * <li>View will <b>not</b> be reset before loading</li>
  379.      * <li>Loaded image will <b>not</b> be cached in memory</li>
  380.      * <li>Loaded image will <b>not</b> be cached on disk</li>
  381.      * <li>{@link ImageScaleType#IN_SAMPLE_POWER_OF_2} decoding type will be used</li>
  382.      * <li>{@link Bitmap.Config#ARGB_8888} bitmap config will be used for image decoding</li>
  383.      * <li>{@link SimpleBitmapDisplayer} will be used for image displaying</li>
  384.      * </ul>
  385.      * <p/>
  386.      * These option are appropriate for simple single-use image (from drawables or from Internet) displaying.
  387.      */
  388.     public static DisplayImageOptions createSimple() {
  389.         return new Builder().build();
  390.     }
  391. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|安卓论坛  

GMT+8, 2024-5-19 10:09 , Processed in 0.084547 second(s), 35 queries .

Powered by Discuz! X3.2

© 2001-2013 Design S!|ƽ̶

快速回复 返回顶部 返回列表