摘要:從這段代碼入手分析分析從這段代碼可以看出無論傳入的是還是或者干脆傳入或都會(huì)調(diào)用這個(gè)方法而這個(gè)方法生成兩個(gè)對(duì)象對(duì)象,并把它加到上對(duì)象這兩個(gè)對(duì)象擁有共同的對(duì)象對(duì)象,當(dāng)系統(tǒng)調(diào)用的生命周期,的生命周期隨之被調(diào)用來處理列表,將的生命周期與的生命周期聯(lián)
從這段代碼入手分析Glide
Glide.with(context) .load(url) .placeholder(R.drawable.loading) .error(R.drawable.avatar) .listener(new RequestListener1.Glide.with(context)分析() { @Override public boolean onException(Exception e, String model, Target target, boolean isFirstResource) { return false; } @Override public boolean onResourceReady(GlideDrawable resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource) { LogUtil.logV("loadPhotoDebug", "loadAvatar onResourceReady url = " + url); if (isSimpleImage(tagView.getTag(), url)) { return false; } mImageView.setImageResource(R.drawable.loading); return true; } }) .bitmapTransform(new CropCircleTransformation(context)) .into(mImageView);
Glide.java:
public static RequestManager with(Context context) { RequestManagerRetriever retriever = RequestManagerRetriever.get(); return retriever.get(context); }
RequestManagerRetriever.java:
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); } } public RequestManager get(Fragment fragment) { if (fragment.getActivity() == null) { throw new IllegalArgumentException("You cannot start a load on a fragment before it is attached"); } if (Util.isOnBackgroundThread()) { return get(fragment.getActivity().getApplicationContext()); } else { FragmentManager fm = fragment.getChildFragmentManager(); return supportFragmentGet(fragment.getActivity(), fm); } }
從這段代碼可以看出無論Glide.with傳入的Context是Fragment還是Activity或者干脆傳入Framgent或FragemtnActivity,都會(huì)調(diào)用supportFragmentGet這個(gè)方法
RequestManagerRetriever.java:
RequestManager supportFragmentGet(Context context, FragmentManager fm) { SupportRequestManagerFragment current = getSupportRequestManagerFragment(fm); RequestManager requestManager = current.getRequestManager(); if (requestManager == null) { requestManager = new RequestManager(context, current.getLifecycle(), current.getRequestManagerTreeNode()); current.setRequestManager(requestManager); } return requestManager; } SupportRequestManagerFragment getSupportRequestManagerFragment(final FragmentManager fm) { SupportRequestManagerFragment current = (SupportRequestManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG); if (current == null) { current = pendingSupportRequestManagerFragments.get(fm); if (current == null) { 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; }
而supportFragmentGet這個(gè)方法生成兩個(gè)對(duì)象:
1.SupportRequestManagerFragment對(duì)象, 并把它加到FragmentManager上;
2.RequestManager對(duì)象
這兩個(gè)對(duì)象擁有共同的對(duì)象LifeStyle對(duì)象,當(dāng)Android系統(tǒng)調(diào)用SupportRequestManagerFragment的生命周期,RequestManger的生命周期隨之被調(diào)用來處理Request列表,
將Fragment的生命周期與RequestManger的生命周期聯(lián)系起來:
SupportRequestManagerFragment.java:
@Override public void onStart() { super.onStart(); lifecycle.onStart(); } @Override public void onStop() { super.onStop(); lifecycle.onStop(); } @Override public void onDestroy() { super.onDestroy(); lifecycle.onDestroy(); }
RequestManager.java
public class RequestManager implements LifecycleListener { RequestManager(Context context, final Lifecycle lifecycle, RequestManagerTreeNode treeNode, RequestTracker requestTracker, ConnectivityMonitorFactory factory) { ... ... this.lifecycle = lifecycle; 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); } @Override public void onStart() { // onStart might not be called because this object may be created after the fragment/activity"s onStart method. resumeRequests(); } /** * Lifecycle callback that unregisters for connectivity events (if the android.permission.ACCESS_NETWORK_STATE * permission is present) and pauses in progress loads. */ @Override public void onStop() { pauseRequests(); } /** * Lifecycle callback that cancels all in progress requests and clears and recycles resources for all completed * requests. */ @Override public void onDestroy() { requestTracker.clearRequests(); }
如果傳入的是Applicaton Context,
private RequestManager getApplicationManager(Context context) { // Either an application context or we"re on a background thread. if (applicationManager == null) { synchronized (this) { if (applicationManager == null) { // Normally pause/resume is taken care of by the fragment we add to the fragment or activity. // However, in this case since the manager attached to the application will not receive lifecycle // events, we must force the manager to start resumed using ApplicationLifecycle. applicationManager = new RequestManager(context.getApplicationContext(), new ApplicationLifecycle(), new EmptyRequestManagerTreeNode()); } } } return applicationManager; }
會(huì)得到一個(gè)單例的RequestManager對(duì)象,RequestManager綁定的生命周期并不會(huì)調(diào)用onStop,onDestroy,
ApplicationLifecycle.java:
class ApplicationLifecycle implements Lifecycle { @Override public void addListener(LifecycleListener listener) { listener.onStart(); } }
總結(jié):Glide.with(Context)將Activity的生命周期與RequestManager針對(duì)的request的處理聯(lián)系起來,
傳入ApplicationContext,會(huì)得到一個(gè)單例的RequestManager對(duì)象; 傳入Activity Context,會(huì)得到一個(gè)屬于一個(gè)Activity的RequestManager對(duì)象;
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/70708.html
摘要:版本問題如何實(shí)現(xiàn)與生命周期的綁定如何實(shí)現(xiàn)緩存如何實(shí)現(xiàn)圖片壓縮如何實(shí)現(xiàn)與生命周期的綁定創(chuàng)建將其與傳入的生命周期綁定這樣做的好處是當(dāng)時(shí),也會(huì)做相應(yīng)操作,如停掉圖片加載綁定首先無論傳入的是什么,只要是在子線程中調(diào)用創(chuàng)建的與綁定,這樣創(chuàng)建的的生命周 版本4.9.0 問題 Glide如何實(shí)現(xiàn)與生命周期的綁定? Glide如何實(shí)現(xiàn)緩存? Glide如何實(shí)現(xiàn)圖片壓縮? Glide如何實(shí)現(xiàn)與生命周...
摘要:原文地址游客前言金三銀四,很多同學(xué)心里大概都準(zhǔn)備著年后找工作或者跳槽。最近有很多同學(xué)都在交流群里求大廠面試題。 最近整理了一波面試題,包括安卓JAVA方面的,目前大廠還是以安卓源碼,算法,以及數(shù)據(jù)結(jié)構(gòu)為主,有一些中小型公司也會(huì)問到混合開發(fā)的知識(shí),至于我為什么傾向于混合開發(fā),我的一句話就是走上編程之路,將來你要學(xué)不僅僅是這些,豐富自己方能與世接軌,做好全棧的裝備。 原文地址:游客kutd...
閱讀 1172·2023-04-26 01:35
閱讀 2567·2021-11-02 14:44
閱讀 7710·2021-09-22 15:38
閱讀 2248·2021-09-06 15:11
閱讀 3740·2019-08-30 15:53
閱讀 843·2019-08-29 16:54
閱讀 670·2019-08-26 13:48
閱讀 1787·2019-08-26 13:47