Glide源码分析(二)
写在前面
文章是本人阅读Glide源代码写下的,既当笔记,也有分享给各位学习用的意思。如有错误,欢迎指正。
可以点击这里查看with的流程Glide 源码分析(一)-with的流程
基本步骤
Glide.with().load().into();
接下来看load的流程
load
with 返回的是RequestManager实例,我们看的load就在RequestManager中
public RequestBuilder<Drawable> load(@Nullable Bitmap bitmap) {
return asDrawable().load(bitmap);
}
public RequestBuilder<Drawable> load(@Nullable Drawable drawable) {
return asDrawable().load(drawable);
}
public RequestBuilder<Drawable> load(@Nullable String string) {
return asDrawable().load(string);
}
public RequestBuilder<Drawable> load(@Nullable Uri uri) {
return asDrawable().load(uri);
}
public RequestBuilder<Drawable> load(@Nullable File file) {
return asDrawable().load(file);
}
public RequestBuilder<Drawable> load(@RawRes @DrawableRes @Nullable Integer resourceId) {
return asDrawable().load(resourceId);
}
public RequestBuilder<Drawable> load(@Nullable URL url) {
return asDrawable().load(url);
}
public RequestBuilder<Drawable> load(@Nullable byte[] model) {
return asDrawable().load(model);
}
public RequestBuilder<Drawable> load(@Nullable Object model) {
return asDrawable().load(model);
}
可以看到load是重载的方法,返回了RequestBuilder,所以可以先知道第三步into操作是在RequestBuilder中执行。
在load方法内部都调用了asDrawable(),接着又调用了load方法。
public RequestBuilder<Drawable> asDrawable() {
return as(Drawable.class);
}
public <ResourceType> RequestBuilder<ResourceType> as(
@NonNull Class<ResourceType> resourceClass) {
return new RequestBuilder<>(glide, this, resourceClass, context);
}
asDrawable内部调用了as方法,并传入了Drawable类对象,在as方法中可以看到new了一个RequestBuilder对象并返回。
接下来回到之前的asDrawable().load()的load方法看看。
public RequestBuilder<TranscodeType> load(@Nullable String string) {
return loadGeneric(string);
}
private RequestBuilder<TranscodeType> loadGeneric(@Nullable Object model) {
this.model = model;
isModelSet = true;
return this;
}
可以看到load方法内部调用了loadGeneric()方法,在loadGeneric方法内部将传入的model赋值给RequestBuilder的成员变量,并设置赋值的标志位为true,然后返回RequestBuilder对象,用来在接下来的into方法中调用。
总结一下,相比较with方法,load方法比较简单,因为with方法中不仅完成了Glide的创建和生命周期的绑定,还有后续加载网络资源的准备工作,load方法只是简单的创建了RequestBuilder用来在into中加载资源到view上。