[0] Universal-Image-loader 架构图

429 阅读2分钟

ImageLoader架构图

下面的这张图,来自互联网,是Universal-Image-Loader最常见的一张架构图。本文以此图为基础,进行分析说明。

image.png

Universal-Image-Loader结构划分

在上面的图中,以颜色不同划分为4个部分:

  1. ImageLoader:框架对用户暴露的API,用户通过调用公开的API使用框架提供的功能;
  2. ImageLoaderEngine:对图片加载进程过程的控制,包括:不同缓存数据的优先次序、待加载数据的有效性控制、加载过程同步-异步控制等;
  3. BitmapDisplayer:对待加载图片数据的处理,例如:圆角、渐变控制等;
  4. Data Interface & Data:图片数据缓存相关的接口定义和实现

Universal-Image-loader参数配置

使用Universal-Image-loader框架只需要对框架进行一些参数设置,就可以使用框架了。就算不对框架进行任何参数设置,框架也可以使用默认的参数设置进行工作,但是我们还是要知道这些参数。

DisplayImageOptions

图片加载配置,框架支持为每个图片加载设置独立的 DisplayImageOptions对象,不设置则使用 ImageLoaderConfiguration 对象中的值作为默认值。

// 下面代码中的默认值在源码中是在Builder中定义的
public final class DisplayImageOptions {
   // 图片加载中占位图:memoryCache命中失败时有效
   private final int imageResOnLoading = 0;
   private final Drawable imageOnLoading = null;
   // 空连接占位图
   private final int imageResForEmptyUri = 0;
   private final Drawable imageForEmptyUri = null;
   // 图片加载失败占位图
   private final int imageResOnFail = 0;
   private final Drawable imageOnFail = null;
   
   // 加载图片前是否清空:memoryCache命中失败时有效
   private final boolean resetViewBeforeLoading = false;
   
   // 启用MemoryCache
   private final boolean cacheInMemory = false;
   // 启用本地缓存DiskCache
   private final boolean cacheOnDisk = false;
   // 异步加载控制
   private final boolean isSyncLoading = false;
   private final int delayBeforeLoading = 0;
   
   // 图片Exif信息处理
   private final boolean considerExifParams = false;
   
   // 图片下载引擎的个性化配置,自定义ImageDownloader使用
   private final Object extraForDownloader = null;
   
   // 图片文件加载到内存中的设置
   private final Options decodingOptions = new Options();
   private final ImageScaleType imageScaleType = ImageScaleType.IN_SAMPLE_POWER_OF_2;
   
   // Bitmap加载到View前预处理过程
   private final BitmapProcessor preProcessor;
   private final BitmapProcessor postProcessor;
   
   // 图片加载处理器
   private final BitmapDisplayer displayer = new SimpleBitmapDisplayer();
   // 异步加载图片的Post工具
   private final Handler handler;
}

ImageLoaderConfiguration

框架配置,包含缓存实现类、图片下载控制、线程池等。

public final class ImageLoaderConfiguration {
   // 资源管理器
   final Resources resources;
   // MemoryCache图片大小限制,小于等于0时,按图片默认属性取值
   final int maxImageWidthForMemoryCache = 0;
   final int maxImageHeightForMemoryCache = 0;
   // DiskCache图片大小限制,同时为0时不做处理
   final int maxImageWidthForDiskCache;
   final int maxImageHeightForDiskCache;
   // DiskCache图片大小限制生效时,Bitmap对象同步到DiskCache前的处理
   final BitmapProcessor processorForDiskCache;

   final Executor taskExecutor;
   final Executor taskExecutorForCachedImages;
   
   final boolean customExecutor;
   final boolean customExecutorForCachedImages;

   final int threadPoolSize;
   final int threadPriority;
   final QueueProcessingType tasksProcessingType;

   // MemoryCache实现类
   final MemoryCache memoryCache; // LruMemoryCache
   // DiskCache实现类
   final DiskCache diskCache; // UnlimitedDiskCache
   final ImageDecoder decoder; // BaseImageDecoder

   // 图片下载:包含弱网和断网的处理
   final ImageDownloader downloader; // BaseImageDownloader
   final ImageDownloader networkDeniedDownloader; 
   final ImageDownloader slowNetworkDownloader;
   
   // 默认的展示显示设置
   final DisplayImageOptions defaultDisplayImageOptions;
}