使用就不介绍了
SupportRequestManagerFragment getSupportRequestManagerFragment(final FragmentManager fm) {
SupportRequestManagerFragment current = (SupportRequestManagerFragment) fm.findFragmentByTag(
FRAGMENT_TAG);
if (current == null) {
current = pendingSupportRequestManagerFragments.get(fm);
if (current == null) {
//主要看这个,最终呢new 一个ActivityFragmentLifecycle()
current = new SupportRequestManagerFragment();
pendingSupportRequestManagerFragments.put(fm, current);
fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();
handler.obtainMessage(ID_REMOVE_SUPPORT_FRAGMENT_MANAGER, fm).sendToTarget();
}
}
return current;
}
ActivityFragmentLifecycle它是继承于Lifecycle,所以会复写addListener的方法
@Override
public void addListener(LifecycleListener listener) {
lifecycleListeners.add(listener);
if (isDestroyed) {
listener.onDestroy();
} else if (isStarted) {
listener.onStart();
} else {
listener.onStop();
}
}
看第二步源码分析
RequestManager supportFragmentGet(Context context, FragmentManager fm) {
//这是第三步
SupportRequestManagerFragment current = getSupportRequestManagerFragment(fm);
RequestManager requestManager = current.getRequestManager();//第一次肯定是空
if (requestManager == null) {
//将刚才设置的ActivityFragmentLifecycle获取并设置到RequsetManager
requestManager = new RequestManager(context, current.getLifecycle(), current.getRequestManagerTreeNode());
current.setRequestManager(requestManager);
}
return requestManager;
}
requestManager中的构造方法
RequestManager(Context context, final Lifecycle lifecycle, RequestManagerTreeNode treeNode,
RequestTracker requestTracker, ConnectivityMonitorFactory factory) {
//设置参数
this.context = context.getApplicationContext();
this.lifecycle = lifecycle;
this.treeNode = treeNode;
this.requestTracker = requestTracker;
this.glide = Glide.get(context);
this.optionsApplier = new OptionsApplier();
ConnectivityMonitor connectivityMonitor = factory.build(context,
new RequestManagerConnectivityListener(requestTracker));
if (Util.isOnBackgroundThread()) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
//这个方法实际调用的是ActivityFragmentLifecycle中的addListener而RequstManager也是实现了LifecycleListener,它会调用自己的onstart,onDestory等方法
lifecycle.addListener(RequestManager.this);
}
});
} else {
lifecycle.addListener(this);
}
lifecycle.addListener(connectivityMonitor);
}
SupportRequestManagerFragment的onstart
@Override
public void onStart() {
super.onStart();
lifecycle.onStart();
}
@Override
public void onStop() {
super.onStop();
lifecycle.onStop();
}
@Override
public void onDestroy() {
super.onDestroy();
lifecycle.onDestroy();
}
Glide.with:最终会返回一个RequestManager对象,会去 创建一个 Fragment 绑定到 Activity 上面,Fragment 又会调用 setRequestManager() 方法,ActivityFragmentLifecycle会和Fragment生命周期绑定,即RequestManager和Fragment生命周期进行绑定,依次推到Glide.with会和Fragment进行生命周期绑定
loadProvider -> FixedLoadProvider
modelLoader -> ImageVideoModelLoader
dataFetcher -> ImageVideoFetcher
streamLoader -> StreamStringLoader -> getResourceFetcher()
uriLoader -> StreamUriLoader -> getResourceFetcher()
urlLoader -> HttpUrlGlideUrlLoader -> getResourceFetcher()
streamFetcher -> HttpUrlFetcher
dataLoadProvider -> ImageVideoGifDrawableLoadProvider -> getSourceDecoder()
sourceDecoder -> GifBitmapWrapperResourceDecoder
bitmapProvider -> ImageVideoDataLoadProvider -> getSourceDecoder() bitmapDecoder -> ImageVideoBitmapDecoder
图片:InputStream ,BitmapFactory.decodeStream() 不能直接解析,图片大小
Glide.into时序图,源码就不进行分析了,我之前写过一篇文章是Glide源码分析:
https://blog.csdn.net/qq_24675479/article/details/79498250