Glide 源码分析(二)

本文深入分析了Glide图片加载库的源代码,详细解读了with和load方法的工作流程,阐述了RequestManager和RequestBuilder在资源加载过程中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值