在上一篇文章中讲到Glide是如何通过RequestManager来管理资源加载请求,这篇文章就来看下资源加载请求执行和资源加载完成后显示的具体过程
资源获取
整个过程可概括为:
- 创建RequestBuilder,然后根据要显示在的目标和配置创建获取资源的请求(Request)
- 运行资源获取请求
创建获取资源请求
以RequestBuilder.into(ImageView imageView)为例,看下Reqeust创建的过程
@NonNull
public ViewTarget<ImageView, TranscodeType> into(@NonNull ImageView view) {
Util.assertMainThread();
Preconditions.checkNotNull(view);
BaseRequestOptions<?> requestOptions = this;
if (!requestOptions.isTransformationSet()
&& requestOptions.isTransformationAllowed()
&& view.getScaleType() != null) {
// Clone in this method so that if we use this RequestBuilder to load into a View and then
// into a different target, we don't retain the transformation applied based on the previous
// View's scale type.
//
switch (view.getScaleType()) {
case CENTER_CROP:
requestOptions = requestOptions.clone().optionalCenterCrop();
break;
case CENTER_INSIDE:
requestOptions = requestOptions.clone().optionalCenterInside();
break;
case FIT_CENTER:
case FIT_START:
case FIT_END:
requestOptions = requestOptions.clone().optionalFitCenter();
break;
case FIT_XY:
requestOptions = requestOptions.clone().optionalCenterInside();
break;
case CENTER:
case MATRIX:
default:
// Do nothing.
}
}
return into(
glideContext.buildImageViewTarget(view, transcodeClass),
/*targetListener=*/ null,
requestOptions,
Executors.mainThreadExecutor());
}
private <Y extends Target<TranscodeType>> Y into(
@NonNull Y target,
@Nullable RequestListener<TranscodeType> targetListener,
BaseRequestOptions<?> options,
Executor callbackExecutor) {
Preconditions.checkNotNull(target);
if (!isModelSet) {
throw new IllegalArgumentException("You must call #load() before calling #into()");
}
//创建Request接口对象
Request request = buildRequest(target, targetListener, options, callbackExecutor);
Request previous = target.getRequest();
//判断新的请求和Target的当前请求是否相等,如果相等并且设置了内存缓存配置或者当前请求未完成,
//则判断当前请求是否在运行,如果没有运行则开始运行。
if (request.isEquivalentTo(previous)
&& !isSkipMemoryCacheWithCompletePreviousRequest(options, previous)) {
if (!Preconditions.checkNotNull(previous).isRunning()) {
previous.begin();
}
return target;
}
//如果上述if语句条件判断不成立,则通过RequestManager来运行请求。
requestManager.clear(target);
target.setRequest(request);
requestManager.track