前言
近期研究了一下Glide的圖片加載框架,在這里和大家分享一下。由于代碼研讀有限,難免有錯誤的地方,了解的童鞋還望指正。如果這篇文章對大家學習Glide有幫助,還望大家多多轉載。學習小組QQ群: 193765960。
本篇是Glide框架及源碼解析的第一篇,更多文章敬請關注后續文章。版權歸作者所有,如有轉發,請注明文章出處:http://www.lxweimin.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生命周期回調示意圖
Glide生命周期回調示意圖
3. Glide生命周期綁定機制類圖
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生命周期綁定機制時序圖
(本篇是Glide框架及源碼解析的第一篇,更多文章敬請關注后續文章。版權歸作者所有,如有轉發,請注明文章出處:原文鏈接)