基于Glide v4.9,调用Glide.with(Context context)的时候,会触发Glide的初始化,内部是一个单例静态变量glide,初始化方法:initializeGlide(context, new GlideBuilder())。如果不希望使用Glide的默认配置,那么可以调用Glide的静态方法:public static synchronized void init(@NonNull Context context, @NonNull GlideBuilder builder),自己构造好GlideBuilder,否则会用默认的new GlideBuilder()。
void initializeGlide(@NonNull Context context, @NonNull GlideBuilder builder)方法中涉及annotationGeneratedModule,manifestModules,RequestManagerRetriever.RequestManagerFactory factory,registerComponents,glide.registry……看起来是会读取AndroidManifest.xml获取一些配置,这块还不熟悉,后续补充。如果RequestManagerFactory为空,那么会使用默认的DEFAULT_FACTORY作为RequestManagerRetriever的成员变量,构造函数:
public RequestManagerRetriever(@Nullable RequestManagerFactory factory) { this.factory = factory != null ? factory : DEFAULT_FACTORY; handler = new Handler(Looper.getMainLooper(), this /* Callback */); }
再回过头来看调用链:Glide.with(context) ->
public static RequestManager with(@NonNull Context context) {
return getRetriever(context).get(context);
}
(retriever [rɪˈtriːvər],寻回犬,取回的人,取组织器)
->
private static RequestManagerRetriever getRetriever(@Nullable Context context) {
return Glide.get(context).getRequestManagerRetriever();
}
最终就走到了RequestManagerRetriever的get(Context context)方法来获取当前需要的RequestManager,关于RequestManager放到另一篇写。