前言
近期研究了一下Glide的图片加载框架,在这里和大家分享一下。由于代码研读有限,难免有错误的地方,了解的童鞋还望指正。如果这篇文章对大家学习Glide有帮助,还望大家多多转载。学习小组QQ群: 193765960。
本篇是Glide框架及源码解析的第一篇,更多文章敬请关注后续文章。版权归作者所有,如有转发,请注明文章出处:http://www.jianshu.com/u/d43d948bef39
相关文章:
跟着源码学设计:Glide框架及源码解析(一)
跟着源码学设计:Glide框架及源码解析(二)
跟着源码学设计:Glide框架及源码解析(三)
跟着源码学设计:Glide框架及源码解析(四)
跟着源码学设计:Glide框架及源码解析(五)
1. Glide 简介
Glide是一个性能优良的第三方网络图片加载框架,在节省内存和快速流畅加载方面具有较好体现。究其内部机制,发现其优良性能得益于以下几点:
- 与使用环境生命周期相绑定:RequestManagerFragment & SupportRequestManagerFragment
- 内存的三级缓存池:LruMemoryResources, ActiveResources, BitmapPool
- 内存复用机制:BitmapPool
更多的关于Glide的介绍网上资料很多,在这里不再赘述,下文中将针对Glide的内部机制展开说明。
1.1 为什么要绑定生命周期(有什么优点)?
- 可以实现网络请求根据生命周期而暂停、执行、恢复、释放等
- 可以实现资源比如图片的自动释放
- 降低了内存的压力
- 降低了内存泄漏的风险
1.2 绑定原理
- 原理的知识基础:FragmentManager(简称fm)中的所有fragment(通过fm.add()添加进来)都与fm所处的context生命周期绑定。例如:我们的activity中的fragment的生命周期自动通过activity的fm和activity的生命周期绑定。
- Glide定义了RequestManagerFragment 和 SupportRequestManagerFragment两种fragment。该两类Fragment不具有任何的界面和其他功能,通过入口传入的context获取到的fm绑定生命周期到context上。
- Glide内部的生命周期绑定机制进一步通过基于xxxxRequestManagerFragment 的生命周期接口的回调实现。
2. Glide生命周期回调示意图
3. Glide生命周期绑定机制类图
3.1 RequestManagerRetriever(单例模式)
- 根据context获取fm;
- 获取xxxxRequestManagerFragment实例
- 获取RequestManager实例
- 相互绑定xxxxRequestManagerFragment和RequestManager
- xxxxRequestManagerFragment绑定到context生命周期
/**
* 获取RequestManager实例
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public RequestManager get(Activity activity) {
if (Util.isOnBackgroundThread()
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
return get(activity.getApplicationContext());
} else {
assertNotDestroyed(activity);
//获取fm
android.app.FragmentManager fm = activity.getFragmentManager();
return fragmentGet(activity, fm);
}
}
/**
* Note: new RequestManager(context,lifecycle,RequestManagerTreeNode)中,绑定了lifecycle;
* current.setRequestManager(requestManager):requestManager绑定到fragment
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
RequestManager fragmentGet(Context context, FragmentManager fm) {
//获取fragment实例:与fm绑定生命周期
RequestManagerFragment current = getRequestManagerFragment(fm);
//获取之前绑定的RequestManager
RequestManager requestManager = current.getRequestManager();
if (requestManager == null) {
requestManager = new RequestManager(context, current.getLifecycle(),current.getRequestManagerTreeNode());
//requestManager绑定到fragment
current.setRequestManager(requestManager);
}
return requestManager;
}
/**
* Note: fm.beginTransaction().add(current, FRAGMENT_TAG): fragment绑定context生命周期
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
RequestManagerFragment getRequestManagerFragment(final android.app.FragmentManager fm) {
RequestManagerFragment current = (RequestManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG);
if (current == null) {
current = pendingRequestManagerFragments.get(fm);
if (current == null) {
current = new RequestManagerFragment();
pendingRequestManagerFragments.put(fm, current);
//将fragment与fm绑定
fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();
handler.obtainMessage(ID_REMOVE_FRAGMENT_MANAGER, fm).sendToTarget();
}
}
return current;
}
3.2 xxxxRequestManagerFragment(绑定context的生命周期)
- 内部绑定一个RequestManager对象;
- 内部绑定一个ActivityFragmentLifecycle对象
- 在生命周期的回调中调用ActivityFragmentLifecycle的相应生命周期方法
- RequestManager通过注册到ActivityFragmentLifecycle的lifecycleListeners集合实现生命周期绑定
private final ActivityFragmentLifecycle lifecycle;
private RequestManager requestManager;
public RequestManagerFragment() {
this(new ActivityFragmentLifecycle());
}
RequestManagerFragment(ActivityFragmentLifecycle lifecycle) {
this.lifecycle = lifecycle;
}
/**
* Sets the current {@link com.bumptech.glide.RequestManager}.
* @param requestManager The request manager to use.
*/
public void setRequestManager(RequestManager requestManager) {
this.requestManager = requestManager;
}
/**
* 生命周期:Glide通过xxxxRequestManagerFragment的生命周期回调实现内部生命周期回调
*/
@Override
public void onStart() {
super.onStart();
lifecycle.onStart();
}
@Override
public void onStop() {
super.onStop();
lifecycle.onStop();
}
@Override
public void onDestroy() {
super.onDestroy();
lifecycle.onDestroy();
}
3.3 ActivityFragmentLifecycle
- 管理同一个xxxxRequestManagerFragment分支下的所有LifecycleListener(具有生命周期)
- 被xxxxRequestManagerFragment生命周期接口回调
- 遍历回调所有LifecycleListener生命周期接口
/**
* A {@link com.bumptech.glide.manager.Lifecycle} implementation for tracking and notifying listeners of
* {@link android.app.Fragment} and {@link android.app.Activity} lifecycle events.
*/
class ActivityFragmentLifecycle implements Lifecycle {
private final Set<LifecycleListener> lifecycleListeners =
Collections.newSetFromMap(new WeakHashMap<LifecycleListener, Boolean>());
private boolean isStarted;
private boolean isDestroyed;
@Override
public void addListener(LifecycleListener listener) {
lifecycleListeners.add(listener);
if (isDestroyed) {
listener.onDestroy();
} else if (isStarted) {
listener.onStart();
} else {
listener.onStop();
}
}
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();
}
}
}
3.4 RequestManager
- RequestManager被绑定于xxxxRequestManagerFragment
- RequestManager实现了LifecycleListener接口
- RequestManager注册给xxxxRequestManagerFragment的ActivityFragmentLifecycle
public RequestManager(Context context, Lifecycle lifecycle, RequestManagerTreeNode treeNode) {
this(context, lifecycle, treeNode, new RequestTracker(), new ConnectivityMonitorFactory());
}
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() {
lifecycle.addListener(RequestManager.this);
}
});
} else {
lifecycle.addListener(this);
}
lifecycle.addListener(connectivityMonitor);
}
4. Glide生命周期绑定机制时序图
(本篇是Glide框架及源码解析的第一篇,更多文章敬请关注后续文章。版权归作者所有,如有转发,请注明文章出处:原文链接)