Glide解析二:Glide的初始化
2019-02-21 本文已影响17人
jxiang112
在Glide解析一:Glide整体流程中,Glide的初始通过Glide.get()方法实现,我们看下其源码:
1、Glide.get获取Glide单例的实现
public static Glide get(@NonNull Context context) {
if (glide == null) {
synchronized (Glide.class) {
if (glide == null) {
checkAndInitializeGlide(context);
}
}
}
return glide;
}
private static void checkAndInitializeGlide(@NonNull Context context) {
// In the thread running initGlide(), one or more classes may call Glide.get(context).
// Without this check, those calls could trigger infinite recursion.
if (isInitializing) {
//如果正在初始化中,则抛出异常
throw new IllegalStateException("You cannot call Glide.get() in registerComponents(),"
+ " use the provided Glide instance instead");
}
//标识Glide正在初始化中
isInitializing = true;
//初始化Glide
initializeGlide(context);
//标识Glide初始化完成
isInitializing = false;
}
Glide通过checkAndInitializeGlide方法,检测是否已经初始化,如果还未初始化,则先构建Glide并初始化Glide
2、Glide.initializeGlide初始化Glide
private static void initializeGlide(@NonNull Context context) {
initializeGlide(context, new GlideBuilder());
}
@SuppressWarnings("deprecation")
private static void initializeGlide(@NonNull Context context, @NonNull GlideBuilder builder) {
Context applicationContext = context.getApplicationContext();
//获取通过注解动态生成的的AppGlideModule的实现类
GeneratedAppGlideModule annotationGeneratedModule = getAnnotationGeneratedGlideModules();
//从Manifest获取定义的GlideModule列表,新版本已经废弃
List<com.bumptech.glide.module.GlideModule> manifestModules = Collections.emptyList();
if (annotationGeneratedModule == null || annotationGeneratedModule.isManifestParsingEnabled()) {
manifestModules = new ManifestParser(applicationContext).parse();
}
//从注解动态生成的的AppGlideModule的实现类中去掉Manifest定义的GlideMoldue
if (annotationGeneratedModule != null
&& !annotationGeneratedModule.getExcludedModuleClasses().isEmpty()) {
Set<Class<?>> excludedModuleClasses =
annotationGeneratedModule.getExcludedModuleClasses();
Iterator<com.bumptech.glide.module.GlideModule> iterator = manifestModules.iterator();
while (iterator.hasNext()) {
com.bumptech.glide.module.GlideModule current = iterator.next();
if (!excludedModuleClasses.contains(current.getClass())) {
continue;
}
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "AppGlideModule excludes manifest GlideModule: " + current);
}
iterator.remove();
}
}
if (Log.isLoggable(TAG, Log.DEBUG)) {
for (com.bumptech.glide.module.GlideModule glideModule : manifestModules) {
Log.d(TAG, "Discovered GlideModule from manifest: " + glideModule.getClass());
}
}
//设置RequestManager的工厂类
RequestManagerRetriever.RequestManagerFactory factory =
annotationGeneratedModule != null
? annotationGeneratedModule.getRequestManagerFactory() : null;
builder.setRequestManagerFactory(factory);
//将Manifest定义的GlideModule中的配置选项设置到glideBuilder中
for (com.bumptech.glide.module.GlideModule module : manifestModules) {
module.applyOptions(applicationContext, builder);
}
//将注解动态生成的的AppGlideModule的实现类中的配置选项设置到glideBuilder中
if (annotationGeneratedModule != null) {
annotationGeneratedModule.applyOptions(applicationContext, builder);
}
//使用glideBuilde构建生成Glide
Glide glide = builder.build(applicationContext);
//将Manifest定义的GlideModule中定义的Glide组件注册到Glide中
for (com.bumptech.glide.module.GlideModule module : manifestModules) {
module.registerComponents(applicationContext, glide, glide.registry);
}
//将注解动态生成的的AppGlideModule的实现类中定义的Glide组件注册到Glide中
if (annotationGeneratedModule != null) {
annotationGeneratedModule.registerComponents(applicationContext, glide, glide.registry);
}
//glide监听Application的组件生命周期
applicationContext.registerComponentCallbacks(glide);
Glide.glide = glide;
}
Glide的初始化主要有以下步骤:
- 使用反射动态创建注解定义AppGildeModule的实现类或者反射动态创建Manifest定义的GlideModule的实现类
- 将AppGlideModule的实现类或者GlideModule的实现类中的Glide的配置信息设置到GlideBuilder中
- 使用GlideBuilder根据配置信息构建Glide对象
- 将AppGlideModule的实现类或者GlideModule的实现类的Glide组件注册到glide中
- glide监听Application的组件生命周期
1.1、Glide.getAnnotationGeneratedGlideModules获取注解已创建的glide模块
private static GeneratedAppGlideModule getAnnotationGeneratedGlideModules() {
GeneratedAppGlideModule result = null;
try {
//反射创建GeneratedAppGlideModuleImpl
Class<GeneratedAppGlideModule> clazz =
(Class<GeneratedAppGlideModule>)
Class.forName("com.bumptech.glide.GeneratedAppGlideModuleImpl");
result = clazz.getDeclaredConstructor().newInstance();
}
//...
return result;
}
其核心就是反射创建GeneratedAppGlideModuleImpl实现类,而GeneratedAppGlideModuleImpl只是我们自定义的AppGlideModule的,其他内部都是调用自定义的AppGlideModule的方法。
1.2、ManifestParser.parse从Manifest解析出GlideModule
public List<GlideModule> parse() {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Loading Glide modules");
}
List<GlideModule> modules = new ArrayList<>();
try {
//获取应用信息
ApplicationInfo appInfo = context.getPackageManager()
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
if (appInfo.metaData == null) {
//应用的metaData为空,即Manifest中定义的metadata
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Got null app info metadata");
}
return modules;
}
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "Got app info metadata: " + appInfo.metaData);
}
for (String key : appInfo.metaData.keySet()) {
//遍历Manifest中定义的metadata
if (GLIDE_MODULE_VALUE.equals(appInfo.metaData.get(key))) {
//如果是glide_module
//解析并反射创建定义glidemodule,然后加入GlideModule列表中
modules.add(parseModule(key));
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Loaded Glide module: " + key);
}
}
}
}
//...
return modules;
}
@SuppressWarnings("deprecation")
private static GlideModule parseModule(String className) {
Class<?> clazz;
try {
//根据className创建Class对象
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Unable to find GlideModule implementation", e);
}
Object module = null;
try {
//反射创建GlideModule的实现类
module = clazz.getDeclaredConstructor().newInstance();
// These can't be combined until API minimum is 19.
}
//...
return (GlideModule) module;
}
动态生成Manifest定义的GlideModule显示解析Manifest并创建定义的GlideModule,相比之下注解的方式少了解析Manifest的步骤,所以注解的方式相对比较高效。
2、GlideBuilder.build构建Glide对象
步骤1中,Glide是通过GlideBuilder进行构建的,我们看下GlideBuilder的build实现
Glide build(@NonNull Context context) {
if (sourceExecutor == null) {
//默认不自定义网络加载线程池的情况下,使用默认的网络加载线程池
//创建网络加载线程池对象
sourceExecutor = GlideExecutor.newSourceExecutor();
}
if (diskCacheExecutor == null) {
//默认不自定义磁盘加载线程池的情况下,使用默认的磁盘加载线程池
//创建磁盘加载线程池对象
diskCacheExecutor = GlideExecutor.newDiskCacheExecutor();
}
if (animationExecutor == null) {
//默认不自定义动画加载线程池的情况下,使用默认的动画加载线程池
//创建动画加载线程池对象
animationExecutor = GlideExecutor.newAnimationExecutor();
}
if (memorySizeCalculator == null) {
//创建内存大小计算器
memorySizeCalculator = new MemorySizeCalculator.Builder(context).build();
}
if (connectivityMonitorFactory == null) {
//创建默认的网络连接监听工厂类
connectivityMonitorFactory = new DefaultConnectivityMonitorFactory();
}
if (bitmapPool == null) {
//获取Bitmap池的大小
int size = memorySizeCalculator.getBitmapPoolSize();
if (size > 0) {
//如果Bitmap池的大小大于0,采用LruBitmapPool
bitmapPool = new LruBitmapPool(size);
} else {
//如果Bitmap池的大小于小于或等于0,采用BitmapPoolAdapter
bitmapPool = new BitmapPoolAdapter();
}
}
if (arrayPool == null) {
//arrayPool是对数组池,主要用于图片解析时存储临时数据用
//根据数组池的大小创建LruArrayPool
arrayPool = new LruArrayPool(memorySizeCalculator.getArrayPoolSizeInBytes());
}
if (memoryCache == null) {
//创建内存缓存实现类
memoryCache = new LruResourceCache(memorySizeCalculator.getMemoryCacheSize());
}
if (diskCacheFactory == null) {
//创建磁盘缓存工厂类
diskCacheFactory = new InternalCacheDiskCacheFactory(context);
}
if (engine == null) {
//创建图片加载引起对象
engine =
new Engine(
memoryCache,
diskCacheFactory,
diskCacheExecutor,
sourceExecutor,
GlideExecutor.newUnlimitedSourceExecutor(),
GlideExecutor.newAnimationExecutor(),
isActiveResourceRetentionAllowed);
}
//创建RequestManagerRetriever对象
RequestManagerRetriever requestManagerRetriever =
new RequestManagerRetriever(requestManagerFactory);
//创建Glide对象
return new Glide(
context,
engine,
memoryCache,
bitmapPool,
arrayPool,
requestManagerRetriever,
connectivityMonitorFactory,
logLevel,
defaultRequestOptions.lock(),
defaultTransitionOptions);
}
GlideBuilder在不指定线程池、缓存策略等的情况下,默认为我们创建了网络加载线程池、磁盘缓存加载线程池、动画线程池、Bitmap池(用于复用)、数组池(用于复用)、内存缓存、磁盘缓存、图片加载引擎等。然后再创建Glide。
一些相关的点请具体看:
Glide解析九:MemorySizeCalculator的用途
Glide解析十:Bitmap是如何复用的
Glide解析十一:数组是如何复用的
3、创建Glide时,都做了哪些事情
步骤2中创建Glide是在GlideBuilder的build方法new处理的,我们看下new Glide时,Glide的构造方法都做了哪些事情:
Glide(
@NonNull Context context,
@NonNull Engine engine,
@NonNull MemoryCache memoryCache,
@NonNull BitmapPool bitmapPool,
@NonNull ArrayPool arrayPool,
@NonNull RequestManagerRetriever requestManagerRetriever,
@NonNull ConnectivityMonitorFactory connectivityMonitorFactory,
int logLevel,
@NonNull RequestOptions defaultRequestOptions,
@NonNull Map<Class<?>, TransitionOptions<?, ?>> defaultTransitionOptions) {
//设置图片加载引起
this.engine = engine;
//设置bitmap池
this.bitmapPool = bitmapPool;
//设置数组池
this.arrayPool = arrayPool;
//设置内存缓存
this.memoryCache = memoryCache;
//设置requestManagerRetriever
this.requestManagerRetriever = requestManagerRetriever;
//设置网络连接监听工厂类
this.connectivityMonitorFactory = connectivityMonitorFactory;
//获取图片解码格式
DecodeFormat decodeFormat = defaultRequestOptions.getOptions().get(Downsampler.DECODE_FORMAT);
//创建Bitmap预填充对象
bitmapPreFiller = new BitmapPreFiller(memoryCache, bitmapPool, decodeFormat);
//获取资源对象
final Resources resources = context.getResources();
//创建注册对象
registry = new Registry();
// Right now we're only using this parser for HEIF images, which are only supported on OMR1+.
// If we need this for other file types, we should consider removing this restriction.
// Note that order here matters. We want to check the ExifInterface parser first for orientation
// and then fall back to DefaultImageHeaderParser for other fields.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
//android8.1之后支持heif图片格式,注册ExifInterfaceImageHeaderParser用来解析heif图片的方向信息
registry.register(new ExifInterfaceImageHeaderParser());
}
//注册默认DefaultImageHeaderParser用来解析图片的格式、方向等信息
registry.register(new DefaultImageHeaderParser());
//创建图片采样Downsamples,用与解析图片并根据图片的方向进行相应的旋转处理
Downsampler downsampler = new Downsampler(registry.getImageHeaderParsers(),
resources.getDisplayMetrics(), bitmapPool, arrayPool);
//创建gif图片解析器ByteBufferGifDecoder,用于从输入流中解析gif
ByteBufferGifDecoder byteBufferGifDecoder =
new ByteBufferGifDecoder(context, registry.getImageHeaderParsers(), bitmapPool, arrayPool);
//创建视频解析器VideoDecoder,用于将assetfile或parcelfile的视频文件解析成bitmap
ResourceDecoder<ParcelFileDescriptor, Bitmap> parcelFileDescriptorVideoDecoder =
VideoDecoder.parcel(bitmapPool);
//创建字节缓冲图片解析器,用于从字节缓冲中解析出bitmap
ByteBufferBitmapDecoder byteBufferBitmapDecoder = new ByteBufferBitmapDecoder(downsampler);
//创建输入流图片解析器,用于从输入流中解析出bitmap
StreamBitmapDecoder streamBitmapDecoder = new StreamBitmapDecoder(downsampler, arrayPool);
//创建Drawable图片解析器, 用于从uri中解析出drawable
ResourceDrawableDecoder resourceDrawableDecoder =
new ResourceDrawableDecoder(context);
//创建app资源图片加载器的工厂类,用于创建app资源图片加载器,并指明从资源图片id中加载资源图片的输入流
ResourceLoader.StreamFactory resourceLoaderStreamFactory =
new ResourceLoader.StreamFactory(resources);
//创建app自身资源图片加载器的工厂类,用于创建app资源图片加载器,并指明从资源图片id中加载资源图片的uri
ResourceLoader.UriFactory resourceLoaderUriFactory =
new ResourceLoader.UriFactory(resources);
//创建app自身资源图片加载器的工厂类,用于创建app资源图片加载器,并指明从资源图片id中加载资源图片的ParcelFileDescriptor
ResourceLoader.FileDescriptorFactory resourceLoaderFileDescriptorFactory =
new ResourceLoader.FileDescriptorFactory(resources);
//创建app自身资源图片加载器的工厂类,用于创建app资源图片加载器,并指明从资源图片id中加载资源图片的AssetFileDescriptor
ResourceLoader.AssetFileDescriptorFactory resourceLoaderAssetFileDescriptorFactory =
new ResourceLoader.AssetFileDescriptorFactory(resources);
//创建bitmap编码器,用于向输出流写bitmap
BitmapEncoder bitmapEncoder = new BitmapEncoder(arrayPool);
//创建bitmap转换器,用于将bitmap转换成字节流
BitmapBytesTranscoder bitmapBytesTranscoder = new BitmapBytesTranscoder();
//创建gif转换器,用于将gif转换成字节流
GifDrawableBytesTranscoder gifDrawableBytesTranscoder = new GifDrawableBytesTranscoder();
ContentResolver contentResolver = context.getContentResolver();
registry
//注册基于字节缓冲的图片编码器,用于将字节缓冲写到文件中
.append(ByteBuffer.class, new ByteBufferEncoder())
//注册基于输入流的图片编码器,用于将输入流写到磁盘中
.append(InputStream.class, new StreamEncoder(arrayPool))
/* Bitmaps */
//注册将字节缓冲解码成Bitmap的图片解码器
.append(Registry.BUCKET_BITMAP, ByteBuffer.class, Bitmap.class, byteBufferBitmapDecoder)
//注册将输入流解码成Bitmap的图片解码器
.append(Registry.BUCKET_BITMAP, InputStream.class, Bitmap.class, streamBitmapDecoder)
//注册将ParcelFileDescriptor解码成Bitmap的图片解码器
.append(
Registry.BUCKET_BITMAP,
ParcelFileDescriptor.class,
Bitmap.class,
parcelFileDescriptorVideoDecoder)
//注册将AssetFileDescriptor解码成Bitmap的图片解码器
.append(
Registry.BUCKET_BITMAP,
AssetFileDescriptor.class,
Bitmap.class,
VideoDecoder.asset(bitmapPool))
//注册将Bitmap解码成Bitmap的图片解码器
.append(Bitmap.class, Bitmap.class, UnitModelLoader.Factory.<Bitmap>getInstance())
.append(
Registry.BUCKET_BITMAP, Bitmap.class, Bitmap.class, new UnitBitmapDecoder())
.append(Bitmap.class, bitmapEncoder)
/* BitmapDrawables */
//注册各种转换成BitmapDrawable的图片解码器
.append(
Registry.BUCKET_BITMAP_DRAWABLE,
ByteBuffer.class,
BitmapDrawable.class,
new BitmapDrawableDecoder<>(resources, byteBufferBitmapDecoder))
.append(
Registry.BUCKET_BITMAP_DRAWABLE,
InputStream.class,
BitmapDrawable.class,
new BitmapDrawableDecoder<>(resources, streamBitmapDecoder))
.append(
Registry.BUCKET_BITMAP_DRAWABLE,
ParcelFileDescriptor.class,
BitmapDrawable.class,
new BitmapDrawableDecoder<>(resources, parcelFileDescriptorVideoDecoder))
.append(BitmapDrawable.class, new BitmapDrawableEncoder(bitmapPool, bitmapEncoder))
/* GIFs */
//注册各种转换成gif的图片解码器
.append(
Registry.BUCKET_GIF,
InputStream.class,
GifDrawable.class,
new StreamGifDecoder(registry.getImageHeaderParsers(), byteBufferGifDecoder, arrayPool))
.append(Registry.BUCKET_GIF, ByteBuffer.class, GifDrawable.class, byteBufferGifDecoder)
.append(GifDrawable.class, new GifDrawableEncoder())
/* GIF Frames */
// Compilation with Gradle requires the type to be specified for UnitModelLoader here.
.append(
GifDecoder.class, GifDecoder.class, UnitModelLoader.Factory.<GifDecoder>getInstance())
.append(
Registry.BUCKET_BITMAP,
GifDecoder.class,
Bitmap.class,
new GifFrameResourceDecoder(bitmapPool))
/* Drawables */
.append(Uri.class, Drawable.class, resourceDrawableDecoder)
.append(
Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitmapPool))
/* Files */
注册各种解析图片文件的图片加载器和图片解码器
.register(new ByteBufferRewinder.Factory())
.append(File.class, ByteBuffer.class, new ByteBufferFileLoader.Factory())
.append(File.class, InputStream.class, new FileLoader.StreamFactory())
.append(File.class, File.class, new FileDecoder())
.append(File.class, ParcelFileDescriptor.class, new FileLoader.FileDescriptorFactory())
// Compilation with Gradle requires the type to be specified for UnitModelLoader here.
//注册各种图片加载器
.append(File.class, File.class, UnitModelLoader.Factory.<File>getInstance())
/* Models */
.register(new InputStreamRewinder.Factory(arrayPool))
//注册将Integer转成InputStream的图片加载器工厂类
.append(int.class, InputStream.class, resourceLoaderStreamFactory)
//注册将int转成ParcelFileDescriptor的图片加载器工厂类
.append(
int.class,
ParcelFileDescriptor.class,
resourceLoaderFileDescriptorFactory)
//注册将Integer转成InputStream的图片加载器工厂类
.append(Integer.class, InputStream.class, resourceLoaderStreamFactory)
//注册将Integer转成ParcelFileDescriptor的图片加载器工厂类
.append(
Integer.class,
ParcelFileDescriptor.class,
resourceLoaderFileDescriptorFactory)
.append(Integer.class, Uri.class, resourceLoaderUriFactory)
//注册将int转成AssetFileDescriptor的图片加载器工厂类
.append(
int.class,
AssetFileDescriptor.class,
resourceLoaderAssetFileDescriptorFactory)
//注册将Integer转成AssetFileDescriptor的图片加载器工厂类
.append(
Integer.class,
AssetFileDescriptor.class,
resourceLoaderAssetFileDescriptorFactory)
//注册将int转成Uri的图片加载器工厂类
.append(int.class, Uri.class, resourceLoaderUriFactory)
//注册将String转成输入流的图片加载器工厂类
.append(String.class, InputStream.class, new DataUrlLoader.StreamFactory<String>())
//注册将Uri转成输入流的图片加载器工厂类
.append(Uri.class, InputStream.class, new DataUrlLoader.StreamFactory<Uri>())
.append(String.class, InputStream.class, new StringLoader.StreamFactory())
//注册将String转成ParcelFileDescriptor的图片加载器工厂类
.append(String.class, ParcelFileDescriptor.class, new StringLoader.FileDescriptorFactory())
//注册将String转成AssetFileDescriptor的图片加载器工厂类
.append(
String.class, AssetFileDescriptor.class, new StringLoader.AssetFileDescriptorFactory())
.append(Uri.class, InputStream.class, new HttpUriLoader.Factory())
//注册将Uri转成输入流的图片加载器工厂类
.append(Uri.class, InputStream.class, new AssetUriLoader.StreamFactory(context.getAssets()))
//注册将Uri转成ParcelFileDescriptor的图片加载器工厂类
.append(
Uri.class,
ParcelFileDescriptor.class,
new AssetUriLoader.FileDescriptorFactory(context.getAssets()))
//注册将Uri转成输入流的图片加载器工厂类
.append(Uri.class, InputStream.class, new MediaStoreImageThumbLoader.Factory(context))
//注册将Uri转成输入流的图片加载器工厂类
.append(Uri.class, InputStream.class, new MediaStoreVideoThumbLoader.Factory(context))
//注册将Uri转成输入流的图片加载器工厂类
.append(
Uri.class,
InputStream.class,
new UriLoader.StreamFactory(contentResolver))
//注册将Uri转成ParcelFileDescriptor的图片加载器工厂类
.append(
Uri.class,
ParcelFileDescriptor.class,
new UriLoader.FileDescriptorFactory(contentResolver))
//注册将Uri转成AssetFileDescriptor的图片加载器工厂类
.append(
Uri.class,
AssetFileDescriptor.class,
new UriLoader.AssetFileDescriptorFactory(contentResolver))
//注册将Uri转成输入流的图片加载器工厂类
.append(Uri.class, InputStream.class, new UrlUriLoader.StreamFactory())
//注册将URL转成输入流的图片加载器工厂类
.append(URL.class, InputStream.class, new UrlLoader.StreamFactory())
//注册将Uri转成文件的图片加载器工厂类
.append(Uri.class, File.class, new MediaStoreFileLoader.Factory(context))
//注册将GlideUrl转成输入流的图片加载器工厂类
.append(GlideUrl.class, InputStream.class, new HttpGlideUrlLoader.Factory())
//注册将byte[]转成字节缓冲的图片加载器工厂类
.append(byte[].class, ByteBuffer.class, new ByteArrayLoader.ByteBufferFactory())
//注册将byte[]转成输入流的图片加载器工厂类
.append(byte[].class, InputStream.class, new ByteArrayLoader.StreamFactory())
//注册将Uri转成Uri的图片加载器工厂类
.append(Uri.class, Uri.class, UnitModelLoader.Factory.<Uri>getInstance())
//注册将Drawable转成Drawable的图片加载器工厂类
.append(Drawable.class, Drawable.class, UnitModelLoader.Factory.<Drawable>getInstance())
//注册将Drawable转成Drawable的解码器
.append(Drawable.class, Drawable.class, new UnitDrawableDecoder())
/* Transcoders */
//注册将Bitmap转换成BitmapDrawable的转换器
.register(
Bitmap.class,
BitmapDrawable.class,
new BitmapDrawableTranscoder(resources))
//注册将bitmap转换成byte[]的转码器
.register(Bitmap.class, byte[].class, bitmapBytesTranscoder)
//注册将Drawable转成byte[]的转码器
.register(
Drawable.class,
byte[].class,
new DrawableBytesTranscoder(
bitmapPool, bitmapBytesTranscoder, gifDrawableBytesTranscoder))转换器
//注册将gift转换成byte[]的转码器
.register(GifDrawable.class, byte[].class, gifDrawableBytesTranscoder);
//创建ImageView包裹类,主要用于图片加载成功设置图片、获取图片大小等
ImageViewTargetFactory imageViewTargetFactory = new ImageViewTargetFactory();
//创建专属Glide的上下文
glideContext =
new GlideContext(
context,
arrayPool,
registry,
imageViewTargetFactory,
defaultRequestOptions,
defaultTransitionOptions,
engine,
logLevel);
}
总结一下Glide初始化的流程:
- 解析注解定义的AppGlideModule或者Manifest定义的GlideModule,将其对应的配置信息设置给GlideBuilder
- GlideBuilder设置一些默认的配置信息,比如网络线程池、磁盘线程池、动画线程池、内存缓存、磁盘缓存等
- GlideBuilder构建Glide,在创建Glide时注册了各种用于图片加载、解码、编码的图片加载器、图片解码器、图片编码器等
- 将AppGlideModule和GlideModule自定义的图片加载、解码器、编码器注册到Glide中
- 监听Application的生命周期
这里Glide的初始化主要使用单例、构建等设计模式