Glide 源码解析 之 关键类简介

Glide关键类介绍

Glide

单例,负责全局配置

/**
 * A singleton to present a simple static interface for building requests with
 * {@link RequestBuilder} and maintaining an {@link Engine}, {@link BitmapPool},
 * {@link com.bumptech.glide.load.engine.cache.DiskCache} and {@link MemoryCache}.
 */

RequestManager

根据Activity、Fragment的生命周期去管理Request的开始、结束、重试。页面生命周期是如何传递过来的呢?请看:Glide 源码解析 之 生命周期传递

/**
 * A class for managing and starting requests for Glide. Can use activity, fragment and connectivity
 * lifecycle events to intelligently stop, start, and restart requests. Retrieve either by
 * instantiating a new object, or to take advantage built in Activity and Fragment lifecycle
 * handling, use the static Glide.load methods with your Fragment or Activity.
 */

RequestBuilder

对各类加载参数进行封装。

在这里,图片的url会被封装成Request,imageView会被封装成Target,方便后期处理。

/**
 * A generic class that can handle setting options and staring loads for generic resource types.
 */

Request

接口,封装加载请求

/**
 * A request that loads a resource for an {@link com.bumptech.glide.request.target.Target}.
 */
image

Target

接口,封装加载目的地

/**
 * An interface that Glide can load a resource into and notify of relevant lifecycle events during a
 * load.
 */
image

Engine

负责启动加载任务,管理cache。

/**
 * Responsible for starting loads and managing active and cached resources.
 */

EngineJob

EngineJob 是 Engine 和 DecodeJob 中间的桥梁,负责了将 DecodeJob 丢到线程池中,响应 DecodeJob 的回调,并通过hanlder 将回调 切换到 MainThread,把结果传递到Engine中。

/**
 * A class that manages a load by adding and removing callbacks for for the load and notifying
 * callbacks when the load completes.
 */

DecodeJob

负责从cache文件和原始数据源(local source,remote source)中加载数据。
实现了Runnable接口,在线程池中执行。

/**
 * A class responsible for decoding resources either from cached data or from the original source
 * and applying transformations and transcodes.
 */

ActiveResources

一级内存缓存

LruResourceCache

二级内存缓存

/**
 * An LRU in memory cache for {@link com.bumptech.glide.load.engine.Resource}s.
 */

DataFetcher

接口,负责从数据源中取出数据。

/**
 * Lazily retrieves data that can be used to load a resource.
 */
image

ResourceDecoder

接口,负责图片数据的解码

/**
 * An interface for decoding resources.
 */
image

Glide 类整体分析

Glide的类,按分工可以分为2类:

  1. 调度配置型:全部配置,根据不同的输入输出,调度不同的实现类去处理;
  2. 具体实施型:处理(fetch,decode)特定类型的数据;

从上文关键类的介绍可以看到,Glide中有几个关键接口有非常多的实现。之所以有这么多实现类,就是因为Glide支持非常多的加载类型(见下图),不同的实现类在调度类的调度下,处理各自对应的类型,有条不紊。

RequestBuilder的load()方法有非常多的重载方法。


RequestBuilder

图片加载框架的技术要点:

  1. 生命周期管理;Glide 源码解析 之 生命周期传递
  2. 缓存管理;Glide 源码解析 之 缓存
  3. 支持各种输入类型(本地文件、远程文件、Resource资源)、输出类型(Bitmap、Drawable);
  4. 图片后期处理(裁切、圆角、滤镜);
  5. 自定义配置(缓存类型配置、线程池配置、etc);
  6. 外部Module拓展;

你可能感兴趣的:(Glide 源码解析 之 关键类简介)