Glide基本用法
基于glide4.7.1
Glide.with(context).load(obj).into(imageView)
- 先上图,with方法的流程图,看完图对基本流程有个了解,再细看代码
-
这是跟Glide初始化相关的类图
- 先看
Glide
的with(context)
方法的实现, 根据传入的参数有以下方法的重载,发现都会调用getRetriever(context).get(context)
方法,拆成两部分来看- 第一部分,
getRetriever(context)
负责Glide的初始化工作,并返回RequestManagerRetriever
对象。 - 第二部分,
requestManagerRetriever.get(context)
,request请求如何绑定生命周期。
- 第一部分,
public static RequestManager with(@NonNull Context context) {
return getRetriever(context).get(context);
}
public static RequestManager with(@NonNull Activity activity) {
return getRetriever(activity).get(activity);
}
public static RequestManager with(@NonNull FragmentActivity activity) {
return getRetriever(activity).get(activity);
}
public static RequestManager with(@NonNull Fragment fragment) {
return getRetriever(fragment.getActivity()).get(fragment);
}
public static RequestManager with(@NonNull android.app.Fragment fragment) {
return getRetriever(fragment.getActivity()).get(fragment);
}
public static RequestManager with(@NonNull View view) {
return getRetriever(view.getContext()).get(view);
}
Glide的初始化过程
-
getRetriever(context)
方法调用了Glide的get(context)
方法返回Glide
对象,然后调用getRequestManagerRetriever()
方法返回RequestManagerRetriever
对象,因此初始化工作主要在Glide.get(context)
中进行。
private static RequestManagerRetriever getRetriever(@Nullable Context context) {
......
return Glide.get(context).getRequestManagerRetriever();
}
- 进入
Glide
的get(Context)
方法,内部实现了一个单例模式,调用checkAndInitializeGlide(context)
检查并初始化Glide
对象。
public static Glide get(@NonNull Context context) {
if (glide == null) {
synchronized (Glide.class) {
if (glide == null) {
checkAndInitializeGlide(context);
}
}
}
return glide;
}
- 先判断初始化标志位是否已经被初始化了,如果不进行检查,可能出现多个类调用
get(context)
方法,同时调用初始化方法,这样就保证了单线程Glide
只会被初始化一次。
private static void checkAndInitializeGlide(@NonNull Context context) {
if (isInitializing) {
throw new IllegalStateException("You cannot call Glide.get() in registerComponents(),"
+ " use the provided Glide instance instead");
}
isInitializing = true;
initializeGlide(context);
isInitializing = false;
}
- 进入
initializeGlide(context)
方法,new了一个GlideBuilder
对象当做实参
private static void initializeGlide(@NonNull Context context) {
initializeGlide(context, new GlideBuilder());
}
- 这部分初始化主要进行了两部分工作
因为Glide4
支持自定义GlideModule
,第一部分就是先获取我们创建的module
,如果存在第二部分就会调用applyOptions
方法,就我们新建的builder
传入,从而可以让开发者自行配置参数,然后再调用builder.build(applicationContext)
方法创建出Glide,开发者在builder中配置的参数也会被应用。最后调用
annotationGeneratedModule.registerComponents(applicationContext, glide, glide.registry)
方法注册Glide组件。
private static void initializeGlide(@NonNull Context context, @NonNull GlideBuilder builder) {
Context applicationContext = context.getApplicationContext();
// 获取自定义GlideModule
GeneratedAppGlideModule annotationGeneratedModule = getAnnotationGeneratedGlideModules();
List<com.bumptech.glide.module.GlideModule> manifestModules = Collections.emptyList();
if (annotationGeneratedModule == null || annotationGeneratedModule.isManifestParsingEnabled()) {
manifestModules = new ManifestParser(applicationContext).parse();
}
if (annotationGeneratedModule != null
&& !annotationGeneratedModule.getExcludedModuleClasses().isEmpty()) {
Set<Class<?>> excludedModuleClasses =
annotationGeneratedModule.getExcludedModuleClasses();
Iterator<com.bumptech.glide.module.GlideModule> iterator = manifestModules.iterator();
while (iterator.hasNext()) {
com.bumptech.glide.module.GlideModule current = iterator.next();
if (!excludedModuleClasses.contains(current.getClass())) {
continue;
}
iterator.remove();
}
}
// 第二部分初始化Glide,为module注册组件和应用参数设置
RequestManagerRetriever.RequestManagerFactory factory =
annotationGeneratedModule != null
? annotationGeneratedModule.getRequestManagerFactory() : null;
builder.setRequestManagerFactory(factory);
for (com.bumptech.glide.module.GlideModule module : manifestModules) {
module.applyOptions(applicationContext, builder);
}
if (annotationGeneratedModule != null) {
annotationGeneratedModule.applyOptions(applicationContext, builder);
}
// 构建出glide对象
Glide glide = builder.build(applicationContext);
for (com.bumptech.glide.module.GlideModule module : manifestModules) {
module.registerComponents(applicationContext, glide, glide.registry);
}
if (annotationGeneratedModule != null) {
annotationGeneratedModule.registerComponents(applicationContext, glide, glide.registry);
}
applicationContext.registerComponentCallbacks(glide);
Glide.glide = glide;
}
- 从上面得知glide是通过调用
GlideBuilder
中的build
方法构建的。通过下面代码可以看出,如果我们没有在自定义的module中配置,在build中都会配置默认参数
下面这些配置的参数对应的类都可以对照上面的类图- 先创建了两个线程池一个用于根据数据源加载图片,一个从缓存中加载图片
- 计算缓存Bitmap缓存池内存大小,并且根据大小创建Bitamp缓存池
- 创建数组对象池
- 创建加载引擎
- 创建需要返回的
requestManagerRetriever
- 调用构造函数,new 一个Glide对象,初始化完毕。
public Glide build(Context context) {
if (sourceExecutor == null) {
sourceExecutor = GlideExecutor.newSourceExecutor();
}
if (diskCacheExecutor == null) {
diskCacheExecutor = GlideExecutor.newDiskCacheExecutor();
}
if (memorySizeCalculator == null) {
memorySizeCalculator = new MemorySizeCalculator.Builder(context).build();
}
// 自己深入了解看看
if (connectivityMonitorFactory == null) {
connectivityMonitorFactory = new DefaultConnectivityMonitorFactory();
}
if (bitmapPool == null) {
int size = memorySizeCalculator.getBitmapPoolSize();
if (size > 0) {
bitmapPool = new LruBitmapPool(size);
} else {
bitmapPool = new BitmapPoolAdapter();
}
}
if (arrayPool == null) {
arrayPool = new LruArrayPool(memorySizeCalculator.getArrayPoolSizeInBytes());
}
if (memoryCache == null) {
memoryCache = new LruResourceCache(memorySizeCalculator.getMemoryCacheSize());
}
if (diskCacheFactory == null) {
diskCacheFactory = new InternalCacheDiskCacheFactory(context);
}
if (engine == null) {
engine = new Engine(memoryCache, diskCacheFactory, diskCacheExecutor, sourceExecutor,
GlideExecutor.newUnlimitedSourceExecutor());
}
RequestManagerRetriever requestManagerRetriever = new RequestManagerRetriever(
requestManagerFactory);
return new Glide(
context,
engine,
memoryCache,
bitmapPool,
arrayPool,
requestManagerRetriever,
connectivityMonitorFactory,
logLevel,
defaultRequestOptions.lock(),
defaultTransitionOptions);
}
- 综上,Glide第一部分初始化已经完成。
Glide生命周期的绑定
- 在
RequestManagerRetriever
中get()
有多个方法重载- 共同点:判断是不是在后台线程,如果在后台线程直接走
Application
生命周期 - 如果传入的是
context
,会根据FragmentActivity
、Activity
的实例还是ContextWrapper
来调用对应的方法。 - 如果传入的是
FragmentActivity
或者Fragment
(来自v4包),调用supportFragmentGet
- 如果传入的是
Activity
(非v4包),调用fragmentGet
- 如果传入
View
通过调用view.getContext()
对象进行判断,决定是调用fragmentGet
还是supportFragmentGet
- 通过上面分析关键方法就是
supportFragmentGet
或fragmentGet
,两个方法实现逻辑基本一样,我们挑其中最常用的supportFragmentGet
来看
- 共同点:判断是不是在后台线程,如果在后台线程直接走
public RequestManager get(Context context) {
if (context == null) {
throw new IllegalArgumentException("You cannot start a load on a null Context");
} else if (Util.isOnMainThread() && !(context instanceof Application)) {
if (context instanceof FragmentActivity) {
return get((FragmentActivity) context);
} else if (context instanceof Activity) {
return get((Activity) context);
} else if (context instanceof ContextWrapper) {
return get(((ContextWrapper) context).getBaseContext());
}
}
return getApplicationManager(context);
}
public RequestManager get(FragmentActivity activity) {
if (Util.isOnBackgroundThread()) {
return get(activity.getApplicationContext());
} else {
assertNotDestroyed(activity);
FragmentManager fm = activity.getSupportFragmentManager();
return supportFragmentGet(activity, fm, null /*parentHint*/);
}
}
public RequestManager get(Fragment fragment) {
if (Util.isOnBackgroundThread()) {
return get(fragment.getActivity().getApplicationContext());
} else {
FragmentManager fm = fragment.getChildFragmentManager();
return supportFragmentGet(fragment.getActivity(), fm, fragment);
}
}
public RequestManager get(Activity activity) {
if (Util.isOnBackgroundThread()) {
return get(activity.getApplicationContext());
} else {
assertNotDestroyed(activity);
android.app.FragmentManager fm = activity.getFragmentManager();
return fragmentGet(activity, fm, null /*parentHint*/);
}
}
public RequestManager get(View view) {
if (Util.isOnBackgroundThread()) {
return get(view.getContext().getApplicationContext());
}
Activity activity = findActivity(view.getContext());
if (activity == null) {
return get(view.getContext().getApplicationContext());
}
if (activity instanceof FragmentActivity) {
Fragment fragment = findSupportFragment(view, (FragmentActivity) activity);
if (fragment == null) {
return get(activity);
}
return get(fragment);
}
android.app.Fragment fragment = findFragment(view, activity);
if (fragment == null) {
return get(activity);
}
return get(fragment);
}
- 干了两件事情
- 调用
getSupportRequestManagerFragment
获取Fragment - 调用
factory.build(glide, current.getGlideLifecycle(), current.getRequestManagerTreeNode())
创建 了RequestManager
对象
private RequestManager supportFragmentGet(Context context, FragmentManager fm,
Fragment parentHint) {
SupportRequestManagerFragment current = getSupportRequestManagerFragment(fm, parentHint);
RequestManager requestManager = current.getRequestManager();
if (requestManager == null) {
// TODO(b/27524013): Factor out this Glide.get() call.
Glide glide = Glide.get(context);
requestManager =
factory.build(glide, current.getGlideLifecycle(), current.getRequestManagerTreeNode());
current.setRequestManager(requestManager);
}
return requestManager;
}
- 通过
Tag
来查找Fragment
,如果找不到,就从等待队列中找,如果还是没有就创建一个,加入等待对列中,将Fragment
和Activity
绑定,并使用handler
发送消息从队列中移除Fragment
。
SupportRequestManagerFragment getSupportRequestManagerFragment(
final FragmentManager fm, Fragment parentHint) {
SupportRequestManagerFragment current =
(SupportRequestManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG);
if (current == null) {
current = pendingSupportRequestManagerFragments.get(fm);
if (current == null) {
current = new SupportRequestManagerFragment();
current.setParentFragmentHint(parentHint);
pendingSupportRequestManagerFragments.put(fm, current);
fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();
handler.obtainMessage(ID_REMOVE_SUPPORT_FRAGMENT_MANAGER, fm).sendToTarget();
}
}
return current;
}
-
SupportRequestManagerFragment
的构造函数创建了一个ActivityFragmentLifecycle
对象用来管理生命周期,在onStart
方法中调用ActivityFragmentLifecycle的onStart
方法,同理其他生命周期方法也是。
public class SupportRequestManagerFragment extends Fragment {
public SupportRequestManagerFragment() {
this(new ActivityFragmentLifecycle());
}
@VisibleForTesting
@SuppressLint("ValidFragment")
public SupportRequestManagerFragment(@NonNull ActivityFragmentLifecycle lifecycle) {
this.lifecycle = lifecycle;
}
@Override
public void onStart() {
super.onStart();
lifecycle.onStart();
}
@Override
public void onStop() {
super.onStop();
lifecycle.onStop();
}
@Override
public void onDestroy() {
super.onDestroy();
lifecycle.onDestroy();
unregisterFragmentWithRoot();
}
}
- 下面来看
RequestManger
是如何构造的并绑定了Fragment
的生命周期-
factory.build(glide, current.getGlideLifecycle(), current.getRequestManagerTreeNode())
,current就是我们上面的SupportRequestManagerFragment
对象,将和他绑定的生命周期对象Lifecycle
传给了RequestManger
构造函数
-
public RequestManager build(Glide glide, Lifecycle lifecycle,
RequestManagerTreeNode requestManagerTreeNode) {
return new RequestManager(glide, lifecycle, requestManagerTreeNode);
}
- 在
RequestManager
实现了LifecycleListener
接口,并且调用lifecycle.addListener(this)
添加监听器。接口生命周期函数onStart()
启动请求,onStop
暂停请求
public class RequestManager implements LifecycleListener {
RequestManager(
Glide glide,
Lifecycle lifecycle,
RequestManagerTreeNode treeNode,
RequestTracker requestTracker,
ConnectivityMonitorFactory factory) {
this.glide = glide;
this.lifecycle = lifecycle;
this.treeNode = treeNode;
this.requestTracker = requestTracker;
final Context context = glide.getGlideContext().getBaseContext();
connectivityMonitor =
factory.build(context, new RequestManagerConnectivityListener(requestTracker));
if (Util.isOnBackgroundThread()) {
mainHandler.post(addSelfToLifecycle);
} else {
lifecycle.addListener(this);
}
lifecycle.addListener(connectivityMonitor);
setRequestOptions(glide.getGlideContext().getDefaultRequestOptions());
glide.registerRequestManager(this);
}
@Override
public void onStart() {
resumeRequests();
targetTracker.onStart();
}
@Override
public void onStop() {
pauseRequests();
targetTracker.onStop();
}
}
- 在
ActivtyFragmentLifecycle
中如果添加了监听器,在onStart
调用监听器的onStart
方法,也就是调用RequsetManager
中的onStart
,同理onPause
也是。
class ActivityFragmentLifecycle implements Lifecycle {
@Override
public void addListener(LifecycleListener listener) {
lifecycleListeners.add(listener);
if (isDestroyed) {
listener.onDestroy();
} else if (isStarted) {
listener.onStart();
} else {
listener.onStop();
}
}
@Override
public void removeListener(LifecycleListener listener) {
lifecycleListeners.remove(listener);
}
void onStart() {
isStarted = true;
for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {
lifecycleListener.onStart();
}
}
void onStop() {
isStarted = false;
for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {
lifecycleListener.onStop();
}
}
void onDestroy() {
isDestroyed = true;
for (LifecycleListener lifecycleListener : Util.getSnapshot(lifecycleListeners)) {
lifecycleListener.onDestroy();
}
}
}
- 经过上面的分析发现整个过程就理顺了,先创建一个Fragment对象并在构造函数中创建Lifecycle对象
,在创建RequestManager的时候会把Lifecycle对象传递进去。 - RequestManger则在构造函数处为接收的Lifecycle添加监听器,而RequestManager自身实现了LifecycleListener,在实现的接口方法
onStop
和onPause
分别启动和暂停请求。 - Lifecycle对象在添加监听器后,会在生命周期方法
onStart
和onPauser
分别调用监听器对应的方法。 - 因此当Fragment会在生命周期方法中调用Lifecycle对应的生命周期方法,最终也就是在调用RequestManger中的生命周期方法,因此实现了请求和生命周期的绑定,从而可以根据生命周期来启动和暂停请求。
- 总结一下,with()方法主要初始化Glide和创建了
RequestManger
绑定SupportRequestManagerFragment
的生命周期函数,最后返回RequestManager
。