Glide加载流程
图片请求的接口定义:
public interface Request {
/**
* Starts an asynchronous load.
*/
void begin();
/**
* Identical to {@link #clear()} except that the request may later be restarted.
*/
void pause();
/**
* Prevents any bitmaps being loaded from previous requests, releases any resources held by this
* request, displays the current placeholder if one was provided, and marks the request as having
* been cancelled.
*/
void clear();
/**
* Returns true if this request is paused and may be restarted.
*/
boolean isPaused();
/**
* Returns true if this request is running and has not completed or failed.
*/
boolean isRunning();
/**
* Returns true if the request has completed successfully.
*/
boolean isComplete();
/**
* Returns true if a non-placeholder resource is put. For Requests that load more than one
* resource, isResourceSet may return true even if {@link #isComplete()}} returns false.
*/
boolean isResourceSet();
/**
* Returns true if the request has been cancelled.
*/
boolean isCancelled();
/**
* Returns true if the request has failed.
*/
boolean isFailed();
/**
* Recycles the request object and releases its resources.
*/
void recycle();
}
Request的主要实现是SingleRequest
。
加载的过程
public void begin() {
... ...
status = Status.WAITING_FOR_SIZE;
if (Util.isValidDimensions(overrideWidth, overrideHeight)) {
onSizeReady(overrideWidth, overrideHeight);
} else {
target.getSize(this);
}
// 加载占位符
if ((status == Status.RUNNING || status == Status.WAITING_FOR_SIZE)
&& canNotifyStatusChanged()) {
target.onLoadStarted(getPlaceholderDrawable());
}
}
主要的加载是在onSizeReady()
里完成,它调用了Engine.java
里的load方法来加载图片资源:
加载的过程就分为了2部分:
缓存中加载
Engine.load():
EngineKey key = keyFactory.buildKey(model, signature, width, height, transformations,
resourceClass, transcodeClass, options);
EngineResource<?> active = loadFromActiveResources(key, isMemoryCacheable);
if (active != null) {
cb.onResourceReady(active, DataSource.MEMORY_CACHE);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
logWithTimeAndKey("Loaded resource from active resources", startTime, key);
}
return null;
}
EngineResource<?> cached = loadFromCache(key, isMemoryCacheable);
if (cached != null) {
cb.onResourceReady(cached, DataSource.MEMORY_CACHE);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
logWithTimeAndKey("Loaded resource from cache", startTime, key);
}
return null;
}
这两个都是内存级别缓存,唯一的区别是ActiveResources
是软引用的缓存,而Cache就是强引用的缓存。仅仅这点差异。
??? 这样不会使内存的消耗变大了吗?
从硬盘或网络上异步加载
EngineJob<R> engineJob =
engineJobFactory.build(
key,
isMemoryCacheable,
useUnlimitedSourceExecutorPool,
useAnimationPool,
onlyRetrieveFromCache);
DecodeJob<R> decodeJob =
decodeJobFactory.build(
glideContext,
model,
key,
signature,
width,
height,
resourceClass,
transcodeClass,
priority,
diskCacheStrategy,
transformations,
isTransformationRequired,
isScaleOnlyOrNoTransform,
onlyRetrieveFromCache,
options,
engineJob);
jobs.put(key, engineJob);
engineJob.addCallback(cb);
engineJob.start(decodeJob);