图片加载框架Glide是一个非常强大、优秀的图片加载框架,不但使用简单,而且加入了Activity和Fragment生命周期的管理。知其然,知其所以然,我们有必要学习下它的源码
下图是Glide的总体流程:

1.首先我们从Glide.with(Context con)点进去看到

可以看到RequestManagerRetriever这个类,



根据传入的不同的Context、Activity得到RequestManagerFragment,这是个没有界面的Fragment,在这里面ActivityFragmentLifecycle实现了Lifecycle进行了生命周期的关联

在构造方法中看到RequestManager类,用于 Glide 管理request请求及关联生命周期,点进去看到

在构造方法中看到传入了Lifecycle接口,RequestManager实现了LifecycleListener接口,在这里会做出生命周期与request请求的关联


Request Manager 通过 ActivityFragmentLifecycle 的 addListener 方法注册 LifecycleListener。当 RequestManagerFragment 生命周期方法执行的时候,触发 ActivityFragmentLifecycle 的相应方法,然后会遍历所有注册的 LifecycleListener 并执行相应生命周期方法。这样就完成了生命周期和Glide的request请求的完整关联。
2.而后Glide.into(View view)方法跟进看到,DrawableRequestBuilder,根据ImageView等设置资源的 Target(request 的载体,各种资源对应的加载类),并创建,绑定,跟踪,发起请求


首先,会获取target是否有缓存的请求,如果有会将请求清空。
然后再根据target构建新的请求,包含了一系列的参数,图片大小,Url,缓存策略,签名等等。然后添加生命周期绑定,发起请求

在这里可以看到RequestTracker,用来发起,取消,暂停请求的类,在构造方法中可以看到RequestTracker类内部维护了一个Map集合,用来存放request请求,方便管理

在这里发起具体的请求,我们找到前面构建的GenericRequest,找到begin方法


可以看到onSizeReady()方法,在这个方法里根据Url,width,height等参数,使用 Engine发起load()请求。


Engine类是一个任务创建,发起,回调,管理存活和缓存的资源的类,这Engine的load方法里,loadFromCache(key, isMemoryCacheable)从缓存里面查找资源,loadFromActiveResources(key, isMemoryCacheable)从存活资源里面查找,找到后会将缓存数据放到一个 value 为软引用的 activeResources的map集合中去,然后会将ResourceCallback添加到EngineJob中去。EngineJob是一个调度 DecodeJob任务,添加,移除资源回调,并 notify 回调的类,而DecodeJob是处理来自缓存或者原始的资源,应用转换动画以及 transcode。

在DecodeJob里面,会将资源通过IO流写入磁盘缓存中,再对文件流进行编码,编码时通过设置采样率缩放图片,同时利用Transformation去处理资源,在里面创建了一个BitmapPool的对象池,达到了bitmap的复用节约资源。
如果加载失败,就会创建EngineRunnable执行Runnable去请求资源、处理资源、缓存资源。


如果decode成功,就会调用onResourceReady(),将EngineResource资源返回。
如果目标资源没有在缓存中找到,就会将这个runnable继续交给EngineRunnableManager进行网络请求,然后会调用EngineJob的onResourceReady(),将EngineResource资源返回。
这样就基本完成了一整个请求。当然还有很多蒙蔽的地方,后续再更了
网友评论