这一篇是在上一篇的基础上加深我们对glide的理解,如果您对glide还没有一个感性的认识建议看一下。
Glide的加载流程分析:https://blog.csdn.net/qq_35561554/article/details/100618455
这篇主要回答上篇文章的几个问题:
RequestManager如何与Activity&Fragment进行生命周期的绑定
由上一篇文章我们知道RequestManager实际上是由RequestManagerRetriever来进行获取的。当传入对象是Fragment时
@NonNull public RequestManager get(@NonNull Fragment fragment) { Preconditions.checkNotNull(fragment.getActivity(), "You cannot start a load on a fragment before it is attached or after it is destroyed"); if (Util.isOnBackgroundThread()) { return get(fragment.getActivity().getApplicationContext()); } else { FragmentManager fm = fragment.getChildFragmentManager(); return supportFragmentGet(fragment.getActivity(), fm, fragment, fragment.isVisible()); } }
supportFragmentGet方法:
@NonNull private RequestManager supportFragmentGet( @NonNull Context context, @NonNull FragmentManager fm, @Nullable Fragment parentHint, boolean isParentVisible) { SupportRequestManagerFragment current = getSupportRequestManagerFragment(fm, parentHint, isParentVisible); 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(), context); current.setRequestManager(requestManager); } return requestManager; }
可以看到supportFragmentGet方法首先通过传入Fragment获取的FragmentManager来获取一个
SupportRequestManagerFragment对象,如果这个对象的RequestManager为空就创建一个,并且将它设置个这个SupportRequestManagerFragment。
接下来我们来看看SupportRequestManagerFragment是如何创建的
@NonNull private SupportRequestManagerFragment getSupportRequestManagerFragment( @NonNull final FragmentManager fm, @Nullable Fragment parentHint, boolean isParentVisible) { SupportRequestManagerFragment current = (SupportRequestManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG); if (current == null) { current = pendingSupportRequestManagerFragments.get(fm); if (current == null) { current = new SupportRequestManagerFragment(); current.setParentFragmentHint(parentHint); if (isParentVisible) { current.getGlideLifecycle().onStart(); } pendingSupportRequestManagerFragments.put(fm, current); fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss(); handler.obtainMessage(ID_REMOVE_SUPPORT_FRAGMENT_MANAGER, fm).sendToTarget(); } } return current; }
上面的代码的意思是:给当前的fragment添加一个tag为FRAGMENT_TAG并且空白透明的子fragment,并且根据这个空白透明fragmnet来对这个requestManager进行生命周期管理,这样fragment就和requestManager进行生命周期关联。
类似的当传入的对象是activity的时候就给它设置透明的fragment,将activity和requestmanager进行生命周期的关联。
glide的加载&缓存如何实现
我们知道Glide的正真开始处理图片加载相关开始于Engine的load方法。
图片的加载顺序是:
-
活动资源
-
lru内存缓存
-
资源类型
-
数据来源
相关的分析直接注释在代码中:
public synchronized <R> LoadStatus load( GlideContext glideContext, Object model, Key signature, int width, int height, Class<?> resourceClass, Class<R> transcodeClass, Priority priority, DiskCacheStrategy diskCacheStrategy, Map<Class<?>, Transformation<?>> transformations, boolean isTransformationRequired, boolean isScaleOnlyOrNoTransform, Options options, boolean isMemoryCacheable, boolean useUnlimitedSourceExecutorPool, boolean useAnimationPool, boolean onlyRetrieveFromCache, ResourceCallback cb, Executor c