Glide.with(上下文).load(图片资源).into(控件)
Glide.with()
with(Context)
with(Activity activity)
with(FragmentActivity activity)
with(Fragment fragment)
With(Fragment fragment)
RequestManager.load(String url)
false
RequestManager
true
RequestManagerRetriever.get()
arg为
Application
getApplicationManager
RequestManager
arg为
activity/fragment
fragmentGet
RequestManager
getRequestManagerFragment
创建一个RequestManagerFragment,
并添加到Activity或Fragment上
supportFragmentGet
return
new RequestManager(new ActivityFragmentLifecycle)
getSupportRequestManagerFragment
return
new RequestManager(new ApplicationLifecycle)
true
false
RequestManager
fromString() loadGeneric(String.class)
DrawableTypeRequest
DrawableTypeRequest
OptionsApplier.apply(DrawbaleTypeRequest)
Glide.buildStreamModelLoader
Glide.buildFileDescriptorModeLoader
StreamStringLoader
ModelLoader
DrawableTypeRequest
创建ModelLoader对象
用于加载图片
没实现
DrawableRequestBuilder.load(url)
GenericRequestBuilder.load(url)
super.load(url)
this
DrawableTypeRequest.load(url)
DrawableRequestBuilder
DrawableRequestBuilder
DrawableRequestBuilder.into(ImageView) super.into(ImageView) GenericRequestBuilder.into(ImageView)
Glide.buildImageViewTarget
(ImageView,Class)
ImageViewTargetFactory.buildTarget
(ImageView, Class)
调用过asBitmap()
return new
BitmapImageViewTarget(view)
return new
GlideDrawableImageViewTarget(view)
true
false
BitmapImageViewTarget
或
GlideDrawableImageViewTarget
BitmapImageViewTarget
或
GlideDrawableImageViewTarget
into(tagert)
buildRequest(Tagert)
Request
(GenericRequest)
buildRequestRecursive(Tagert) obtainRequest(Tagert)
GenericRequest.obtain
return
new GenericRequest(targert,placeholderId,errorId,diskCacheStrategy...)
用于加载图片请求
GenericRequestGenericRequest(GenericRequest)
RequestTracker.runRequest(Request) GenericRequest.begin()
Url是否为null
true
onException setErrorPlaceholder
GlideDrawableImageViewTarget.
onLoadFailed(Drawable)
没有,调用父类
ImageViewTarget.onLoadFailed(Drawable)
View.setImageDrawable(drawable)
1
2
1
2
onSizeReady
false
FixedLoadProvider.getModelLoader
return
ImageVideoModelLoader
ImageVidioModelLoader.getResourceFetcher
ImageVideoModelLoader
StreamStringLoader.getResourceFetcher
HttpUrlFetcher
return
ImageVideoFetcher(HttpUrlFetcher)
ImageVideoFetcher
1
2
FixedLoadProvider.getTranscoder
return
GifBitmapWrapperDrawableTranscoder
GifBitmapWrapperDrawableTranscoder
3
Engine.load
(ImageVideoFetcher,FixedLoadProvider,GifBitmapWrapperDrawableTranscoder,GenericRequest...)
4
EngineJobFactory.build()
return
new EngineJob()
EngineJob
1
new EngineRunable
(EngineJob,DecodeJob)
new DecodeJob
(ImageVideoFetcher,
FixedLoadProvider,
GifBitmapWrapperDrawa
bleTranscoder)
EngineJob.start(EngineRunable)
2
3
5
ExecutorService.submit(EngineRunable)
用线程池去运行EngineRunable
则调用其run方法
EngineRunnable.run()
decode()
1
decodeFromSource()
DecodeJob.decodeFromSource()
decodeSource()
transfromEncodeAndTranscode
(GifBitmapWrapperResource<GifBitmapWrapper>)
ImageVideoFetcher.loadData
ImageVideoWrapper
decodeFromSourceData(ImageVideoWrapper)
GifBitmapWrapperResource<GifBitmapWrapper>
HttpUrlFetcher.loadData
1 2
InputStream
1
loadDataWithRedirects(url)
InputStream
利用HttpUrlConnection
进行网络通信
InputStream
new
ImageVideoWrapper(InputStream)
FixedLoadProvider.getSourceDecoder
GifBitmapWrapperResourceDecoder
2
1
GifBitmapWrapperResourceDecoder.decode
(ImageVideoWrapper)
2
GifBitmapWrapper
decode(ImageVideoWrapper)
1
return new
GifBitmapWrapperResource(GifBitmapWrapper)
2
GifBitmapWrapperResource<GifBitmapWrapper>
decodeStream(ImageVideoWrapper)
decodeBitmapWrapper(ImageVideoWrapper)
GifBitmapWrapper
GifBitmapWrapper
ImageVideoBitmapDecoder.decode(ImageVideoWrapper)
return
new GifBitmapWrapper(Resource<Bitmap>)
BitmapResource
1
2
StreamBitmapDecoder.decode(InputStream)
DownSampler.decode(InputStream)
downsampleWithSize(InputStream)
downsampleWithSize(InputStream)
decodeStream(InputStream)
调用BitmapFactory.decodeStream(InputStream, null,options)方法,
从输入流中解出图片
Bitmap
Bitmap
Bitmap
Bitmap
Bitmap
1
2
return
new BitmapResource(Bitmap)
BitmapResource
GifBitmapWrapperResource<GifBitmapWrapper>
1
transcode(GifBitmapWrapperResource<GifBitmapWrapper>)
GifBitmapWrapperDrawableTranscoder.transcode
(GifBitmapWrapperResource<GifBitmapWrapper>)
GlideBitmapDrawableTranscoder.
transcode(GifBitmapWrapperResource<Bitmap>)
把GifBitmapWrapperResource<GifBitmapWrapper>
中的Resource<Bitmap>取出来
1
GifBitmapWrapperResource<Bitmap>
2
GlideBitmapDrawableResource<GlideBitmapDrawable>
new
GlideBitmapDrawableResource(GlideBitmapDrawable)
new
GlideBitmapDrawable(Resource<Bitmap>.get())
1
2
GlideBitmapDrawableResource<GlideBitmapDrawable>
GlideBitmapDrawableResource<GlideBitmapDrawable>
GlideBitmapDrawableResource<GlideBitmapDrawable>
2
GlideBitmapDrawable
GlideBitmapDrawableResource<GlideBitmapDrawable>
GlideBitmapDrawableResource<GlideBitmapDrawable>
GlideBitmapDrawableResource<GlideBitmapDrawable>
GlideBitmapDrawableResource<GlideBitmapDrawable>
GlideBitmapDrawableResource
<GlideBitmapDrawable>
是否为空
OnLoadFailed(Exception)
onLoadComplete
(GlideBitmapDrawableResource<GlideBitmapDrawable>)
EngineJob.onResourceRead
((GlideBitmapDrawableResource<GlideBitmapDrawable>))
用EngineJob中的
Hander发送MSG_COMPLETE消息,表示加载图片完成
注意:该Handler对应的Looper为主线程的Looper
EngineJob中的内部类MainThreadCallback
handlerMessage(Msg message)
what为MSG_COMPLETE
EngineJob.handleResultOnMainThread() EngineJob.handleExceptionOnMainThread()
true false
EngineJob.addCallback
(GenericRequest)
4
GenericRequest.onResourceReady
(GlideBitmapDrawableResource<GlideBitmapDrawable>)
onResourceReady
(GlideBitmapDrawableResource<GlideBitmapDrawable>,GlideBitmapDrawable)
GlideDrawableImageViewTarget.
onResourceReady(GlideDrawable)
ImageViewTarget.
onResourceReady(GlideDrawable)
GlideDrawableImageViewTarget.
setResource(GlideDrawable)
View.setImageDrawable(drawable)
EngineJob.onException(Exception)
用EngineJob中的
Hander发送MSG_EXCEPTION消息,表示加载图片失败
注意:该Handler对应的Looper为主线程的Looper
GenericRequest.onException(Exception)
setErrorPlaceholder(Exception)
GlideDrawableImageViewTarget.
onLoadFailed(Drawable)
没有,调用父类
ImageViewTarget.onLoadFailed(Drawable)
基于郭神Glide3.7.0源码分析博客流程图--Josan
RequestManagerRetriever.get(arg)
RequestManagerRetriever
返回一个单例的
RequestManagerRetriever对象
1
2
1
2
3
1
2
1
2
如果with的参数是Application,则直接创建RequestManager对象,并返回,则图片
的加载和整个App的生命周期一致,并该RequestManager是单例的;
如果with的参数不是Application,则先创建一个RequestManagerFragement,添加到
Activity或者Fragment上,用于保证图片加载与Activity或Fragment生命周期同步,
最后创建一个RequestManager对象并返回。
return
new RequestManager(new ActivityFragmentLifecycle)
创建用于加载图片的ModelLoader,并构建出DrawableTypeRequest对象,在其构建方
法中,会初始化其FixLoadProvider对象,最后调用DrawableTypeRequest对象的load
方法,返回DrawableRequestBuilder对象
return new
DrawbaleTypeRequest(StreamStringLoader))
4
DrawableTypeRequest.buildProvider()
Glide.buildDataProvider()
new
ImageVideoModelLoader(StreamStringLoader)
return new FixLoadProvider
(ImageVideoModelLoader,ResourceTranscoder,ImageVideoGifDrawableLoadProvider)
Glide.buildTranscoder
FixLoadProvider
1
2
3
4
new
GifBitmapWrapperDrawableTranscoder
使用HttpUrlConnection进行网络通信,得到
InputStream,并包装成ImageVideoWrapper返回
从ImageVideoWrapper得到InputStream,并解析成图片,最后包装成
GifBitmapWrapperResource<GifBitmapWrapper>返回
完成GifBitmapWrapperResource<GifBitmapWrapper>到
GlideBitmapDrawableResource<GlideBitmapDrawable>格式的转换
根据判断返回的
GlideBitmapDrawableResource<GlideBitmapDrawable>对
象是否为空,Handler发不同的消息,切换到主线程,最后
更新UI
GifBitmapWrapperDrawableTranscoder
2
1
RequestManager
true
false
ImageVideoGifDrawableLoadProvider