Glide源码剖析-load

本篇将分析源码Glide.load()方法

Glide源码剖析-load

将分析源码Glide.load()

load()

1
Glide.with(this).load("图片资源")

load()一共有9个重载方法,都返回RequestBuilder<Drawable>

1
2
3
4
5
6
7
8
9
public RequestBuilder<Drawable> load(@Nullable Object model)
public RequestBuilder<Drawable> load(@Nullable Bitmap bitmap)
public RequestBuilder<Drawable> load(@Nullable Drawable drawable)
public RequestBuilder<Drawable> load(@Nullable String string)
public RequestBuilder<Drawable> load(@Nullable Uri uri)
public RequestBuilder<Drawable> load(@Nullable File file)
public RequestBuilder<Drawable> load(@RawRes @DrawableRes @Nullable Integer resourceId)
public RequestBuilder<Drawable> load(@Nullable URL url)
public RequestBuilder<Drawable> load(@Nullable byte[] model)

这些重载方法内部都会调用asDrawable().load(),这里就拿最常用的load(String string)来作为例子

我们先来看一下asDrawable()

asDrawable()

1
2
3
4
5
6
7
8
9
10
11
12
13
#RequestManager
@NonNull
@CheckResult
public RequestBuilder<Drawable> asDrawable() {
return as(Drawable.class);
}

@NonNull
@CheckResult
public <ResourceType> RequestBuilder<ResourceType> as(
@NonNull Class<ResourceType> resourceClass) {
return new RequestBuilder<>(glide, this, resourceClass, context);
}

上述代码就是通过使用传递进来的参数来创建一个RequestBuilder(),所以asDrawable()方法最终创建了 RequestBuilder 的实例并返回,查看RequestBuilder的构造函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#RequestBuilder
protected RequestBuilder(
@NonNull Glide glide,
RequestManager requestManager,
Class<TranscodeType> transcodeClass,
Context context) {
this.glide = glide;
this.requestManager = requestManager;
this.transcodeClass = transcodeClass;
this.context = context;
this.transitionOptions = requestManager.getDefaultTransitionOptions(transcodeClass);
this.glideContext = glide.getGlideContext();

initRequestListeners(requestManager.getDefaultRequestListeners());
apply(requestManager.getDefaultRequestOptions());
}

构造函数里也是将参数赋值给了一些常量,之后初始化了请求监听

1
initRequestListeners(requestManager.getDefaultRequestListeners());

最后调用了apply()将默认选择应用于请求

1
2
3
4
5
6
7
8
#RequestBuilder
@NonNull
@CheckResult
@Override
public RequestBuilder<TranscodeType> apply(@NonNull BaseRequestOptions<?> requestOptions) {
Preconditions.checkNotNull(requestOptions);
return super.apply(requestOptions);
}

代码很少,就执行了两个操作。先判断传入及拿来的Options是否是为空,其次调用父类的apply,将Options传入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#BaseRequestOptions
public T apply(@NonNull BaseRequestOptions<?> o) {
// 基本上就是设置一些默认配置项
if (isAutoCloneEnabled) {
return clone().apply(o);
}
BaseRequestOptions<?> other = o;

if (isSet(other.fields, SIZE_MULTIPLIER)) {
sizeMultiplier = other.sizeMultiplier;
}
if (isSet(other.fields, USE_UNLIMITED_SOURCE_GENERATORS_POOL)) {
useUnlimitedSourceGeneratorsPool = other.useUnlimitedSourceGeneratorsPool;
}
if (isSet(other.fields, USE_ANIMATION_POOL)) {
useAnimationPool = other.useAnimationPool;
}
if (isSet(other.fields, DISK_CACHE_STRATEGY)) {
diskCacheStrategy = other.diskCacheStrategy;
}
if (isSet(other.fields, PRIORITY)) {
priority = other.priority;
}
if (isSet(other.fields, ERROR_PLACEHOLDER)) {
errorPlaceholder = other.errorPlaceholder;
errorId = 0;
fields &= ~ERROR_ID;
}
if (isSet(other.fields, ERROR_ID)) {
errorId = other.errorId;
errorPlaceholder = null;
fields &= ~ERROR_PLACEHOLDER;
}
if (isSet(other.fields, PLACEHOLDER)) {
placeholderDrawable = other.placeholderDrawable;
placeholderId = 0;
fields &= ~PLACEHOLDER_ID;
}
if (isSet(other.fields, PLACEHOLDER_ID)) {
placeholderId = other.placeholderId;
placeholderDrawable = null;
fields &= ~PLACEHOLDER;
}
if (isSet(other.fields, IS_CACHEABLE)) {
isCacheable = other.isCacheable;
}
if (isSet(other.fields, OVERRIDE)) {
overrideWidth = other.overrideWidth;
overrideHeight = other.overrideHeight;
}
if (isSet(other.fields, SIGNATURE)) {
signature = other.signature;
}
if (isSet(other.fields, RESOURCE_CLASS)) {
resourceClass = other.resourceClass;
}
if (isSet(other.fields, FALLBACK)) {
fallbackDrawable = other.fallbackDrawable;
fallbackId = 0;
fields &= ~FALLBACK_ID;
}
if (isSet(other.fields, FALLBACK_ID)) {
fallbackId = other.fallbackId;
fallbackDrawable = null;
fields &= ~FALLBACK;
}
if (isSet(other.fields, THEME)) {
theme = other.theme;
}
if (isSet(other.fields, TRANSFORMATION_ALLOWED)) {
isTransformationAllowed = other.isTransformationAllowed;
}
if (isSet(other.fields, TRANSFORMATION_REQUIRED)) {
isTransformationRequired = other.isTransformationRequired;
}
if (isSet(other.fields, TRANSFORMATION)) {
transformations.putAll(other.transformations);
isScaleOnlyOrNoTransform = other.isScaleOnlyOrNoTransform;
}
if (isSet(other.fields, ONLY_RETRIEVE_FROM_CACHE)) {
onlyRetrieveFromCache = other.onlyRetrieveFromCache;
}

// Applying options with dontTransform() is expected to clear our transformations.
if (!isTransformationAllowed) {
transformations.clear();
fields &= ~TRANSFORMATION;
isTransformationRequired = false;
fields &= ~TRANSFORMATION_REQUIRED;
isScaleOnlyOrNoTransform = true;
}

fields |= other.fields;
options.putAll(other.options);

return selfOrThrowIfLocked();
}

isSet():将两个Int类型参数进行与运算。(就是比较二进制数,是1为1,否则为0)

1
2
3
private static boolean isSet(int fields, int flag) {
return (fields & flag) != 0;
}

上面基本上都是在配置配置项,包括磁盘缓存策略,加载中的占位图,加载失败的占位图等。 到这里 asDrawable() 方法完成了。接下来查看load部分

asDrawable().load()

1
2
3
4
5
6
7
#RequestBuilder
@NonNull
@Override
@CheckResult
public RequestBuilder<TranscodeType> load(@Nullable String string) {
return loadGeneric(string);
}

里面也就调用了loadGeneric()

1
2
3
4
5
6
7
8
9
10
#RequestBuilder
@NonNull
private RequestBuilder<TranscodeType> loadGeneric(@Nullable Object model) {
if (isAutoCloneEnabled()) {
return clone().loadGeneric(model);
}
this.model = model;
isModelSet = true;
return selfOrThrowIfLocked();
}
  1. 先判断需不需要进行克隆,需要的话则会将克隆一个RequestBuilder,包括目前为止的所有配置项

    返回此请求生成器的副本,其中包含到目前为止在此生成器上放置的所有选项。

    该方法返回一个“深度”副本,其中所有非不可变参数都被复制,这样对一个构建器的更改不会影响到另一个构建器。 然而,除了不可变参数外,当前模型不会被复制,因此对模型的更改将影响两个构建器。

    [^Ps]: 以上内容来自有道翻译

  2. 将传入进来的图片资源赋值给成员变量model,在设置isModelSet为true,代表着已经设置过了

总结

load()方法就比较简单了,主要就是通过之前创建的Glide和RequestManager来构建RequestBuilder,将参数赋值给成员变量model。还要就是设置一些默认配置项