国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

Glide的源碼分析(一) Activity的生命周期與圖片加載的關系

Kosmos / 1678人閱讀

摘要:從這段代碼入手分析分析從這段代碼可以看出無論傳入的是還是或者干脆傳入或都會調用這個方法而這個方法生成兩個對象對象,并把它加到上對象這兩個對象擁有共同的對象對象,當系統調用的生命周期,的生命周期隨之被調用來處理列表,將的生命周期與的生命周期聯

從這段代碼入手分析Glide

    Glide.with(context)
       .load(url)
       .placeholder(R.drawable.loading)
       .error(R.drawable.avatar)
       .listener(new RequestListener() {
            @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);
1.Glide.with(context)分析

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,都會調用supportFragmentGet這個方法

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這個方法生成兩個對象:
1.SupportRequestManagerFragment對象, 并把它加到FragmentManager上;
2.RequestManager對象
這兩個對象擁有共同的對象LifeStyle對象,當Android系統調用SupportRequestManagerFragment的生命周期,RequestManger的生命周期隨之被調用來處理Request列表,
將Fragment的生命周期與RequestManger的生命周期聯系起來:

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;
    }

會得到一個單例的RequestManager對象,RequestManager綁定的生命周期并不會調用onStop,onDestroy,

ApplicationLifecycle.java:

    class ApplicationLifecycle implements Lifecycle {
    @Override
    public void addListener(LifecycleListener listener) {
        listener.onStart();
    }
}

總結:Glide.with(Context)將Activity的生命周期與RequestManager針對的request的處理聯系起來,

 傳入ApplicationContext,會得到一個單例的RequestManager對象;
 傳入Activity Context,會得到一個屬于一個Activity的RequestManager對象;

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/70708.html

相關文章

  • Glide源碼分析

    摘要:版本問題如何實現與生命周期的綁定如何實現緩存如何實現圖片壓縮如何實現與生命周期的綁定創建將其與傳入的生命周期綁定這樣做的好處是當時,也會做相應操作,如停掉圖片加載綁定首先無論傳入的是什么,只要是在子線程中調用創建的與綁定,這樣創建的的生命周 版本4.9.0 問題 Glide如何實現與生命周期的綁定? Glide如何實現緩存? Glide如何實現圖片壓縮? Glide如何實現與生命周...

    youkede 評論0 收藏0
  • 金三銀四,2019大廠Android高級工程師面試題整理

    摘要:原文地址游客前言金三銀四,很多同學心里大概都準備著年后找工作或者跳槽。最近有很多同學都在交流群里求大廠面試題。 最近整理了一波面試題,包括安卓JAVA方面的,目前大廠還是以安卓源碼,算法,以及數據結構為主,有一些中小型公司也會問到混合開發的知識,至于我為什么傾向于混合開發,我的一句話就是走上編程之路,將來你要學不僅僅是這些,豐富自己方能與世接軌,做好全棧的裝備。 原文地址:游客kutd...

    tracymac7 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<