詳談高大上的圖片載入框架Glide -原始碼篇| 掘金技術徵文

Code4Android發表於2016-12-21

在上篇文章中,我們介紹了Glide圖片載入框架的使用,通過之前的學習,我們可能已經能熟練的將Glide圖片載入框架運用到我們的專案中,但是如果有人問你它是如何載入,工作原理是怎樣的?為什麼自定義GlideModule只需要在Manifest檔案中加入meta-data即可?等等很多載入流程以及使用的注意事項。當然要想搞明白這些問題,就需要我們對Glide原始碼有個大致的認識,去剖析原始碼深處的奧祕。

接下來就讓我們一起去進入Glide的原始碼世界,本篇文章分析的是Glide 3.7.0版本。特別提醒,閱讀本片文章之前要對Glide的用法要先有一個瞭解,可以先閱讀上篇文章,詳談高大上的圖片載入框架Glide -應用篇

此篇文章是自己學習的一個記錄,若對閱讀文章的你有一定幫助,很是高興,當然文章如有不足或者錯誤的地方,歡迎指正,避免我給其他讀者錯誤引導

如果你閱讀過上篇文章,或者你使用過Glide,就知道Glide載入圖片的最簡單方式就是

Glide.with(context).load(url). placeholder(R.drawable.placeholder).into(imageView)。複製程式碼

那麼這篇文章就以這句簡單的程式碼為主線,逐步深入Glide的原始碼。

Glide.with(context)

   //獲取RequestManager物件,該類實現了LifeCycleListener介面,繫結Activity/Fragment生命週期,對請求進行暫停,恢復,清除操作
    public static RequestManager with(Context context) {
    //得到RequestManagerRetriever例項,該類注意將RequestManager和自定義Fragment(如RequestManagerFragment,SupportRequestManagerFragment)繫結,從而實現在生命週期管理回撥
        RequestManagerRetriever retriever = RequestManagerRetriever.get();
        return retriever.get(context);
    }複製程式碼

Glide有四個靜態的過載方法with(),其內部都通過RequestManagerRetriever相應的get過載方法獲取一個RequestManager物件。RequestManagerRetriever提供各種過載方法的好處就是可以將Glide的載入請求與Activity/Fragment的生命週期繫結而自動執行請求,暫停操作。

接下來我們拿Activity引數分析Glide請求如何和繫結生命週期自動請求,暫停,以及銷燬。


    public RequestManager get(Activity activity) {
        if (Util.isOnBackgroundThread() || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            return get(activity.getApplicationContext());
        } else {

           //判斷activity是否已經是銷燬狀態
            assertNotDestroyed(activity);
            //獲取FragmentManager 物件
            android.app.FragmentManager fm = activity.getFragmentManager();
            //建立Fragment,RequestManager並將其繫結
            return fragmentGet(activity, fm);
        }
    }複製程式碼

assertNotDestroyed主要斷言Activity是否已經Destroyed。若是沒有銷燬,或者Activity的FragmentManager ,然後通過fragmentGet返回RequestManager。

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    RequestManager fragmentGet(Context context, android.app.FragmentManager fm) {
      //*獲取RequestManagerFragment,主要利用Frament進行請求的生命週期管理
        RequestManagerFragment current = getRequestManagerFragment(fm);
        RequestManager requestManager = current.getRequestManager();
        //requestManager 為空,即首次載入初始化requestManager ,並呼叫setRequestManager設定到RequestManagerFragment 
        if (requestManager == null) {
            requestManager = new RequestManager(context, current.getLifecycle(), current.getRequestManagerTreeNode());
            current.setRequestManager(requestManager);
        }
        return requestManager;
    }

 //獲取Fragment物件
    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);
                fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();
                handler.obtainMessage(ID_REMOVE_FRAGMENT_MANAGER, fm).sendToTarget();
            }
        }
        return current;
    }複製程式碼

最終通過getRequestManagerFragment()方法獲取一個RequestManagerFragment 物件。

public class RequestManagerFragment extends Fragment {
    private final ActivityFragmentLifecycle lifecycle;
    //省略部分程式碼...
    @Override
    public void onStart() {
        super.onStart();
        //關聯lifecycle相應onStart方法
        lifecycle.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
         //關聯lifecycle相應onStop方法
        lifecycle.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
         //關聯lifecycle相應onDestroy方法
        lifecycle.onDestroy();
    }
}複製程式碼

此時我們看到RequestManagerFragment 繼承了Fragment.並且在其生命週期onStart(),onStop(),onDestory(),呼叫了ActivityFragmentLifecycle 相應的方法,ActivityFragmentLifecycle實現了Lifecycle 介面,在其中通過addListener(LifecycleListener listener)回撥相應(LifecycleListener的 onStart(),onStop(),onDestory())週期方法。LifecycleListener是監聽生命週期時間介面。
再次回到fragmentGet方法裡下面一句程式碼


//建立RequestManager傳入Lifecycle實現類,如ActivityFragmentLifecycle
requestManager = new RequestManager(context, current.getLifecycle(), current.getRequestManagerTreeNode());複製程式碼

對於RequestManager類,該類實現了LifecycleListener,如下程式碼

/**
 * A class for managing and starting requests for Glide. Can use activity, fragment and connectivity lifecycle events to
 * intelligently stop, start, and restart requests. Retrieve either by instantiating a new object, or to take advantage
 * built in Activity and Fragment lifecycle handling, use the static Glide.load methods with your Fragment or Activity.
 */
public class RequestManager implements LifecycleListener {
  //An interface for listening to Activity/Fragment lifecycle events.
    private final Lifecycle lifecycle;
 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;
        //A class for tracking, canceling, and restarting in progress, completed, and failed requests.
        this.requestTracker = requestTracker;
        //通過Glide的靜態方法獲取Glide例項。單例模式
        this.glide = Glide.get(context);
        this.optionsApplier = new OptionsApplier();

//通過工廠類ConnectivityMonitorFactory的build方法獲取ConnectivityMonitor (一個用於監控網路連線事件的介面)
        ConnectivityMonitor connectivityMonitor = factory.build(context,
                new RequestManagerConnectivityListener(requestTracker));

        // If we're the application level request manager, we may be created on a background thread. In that case we
        // cannot risk synchronously pausing or resuming requests, so we hack around the issue by delaying adding
        // ourselves as a lifecycle listener by posting to the main thread. This should be entirely safe.
        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);
    }
    /**
     * Lifecycle callback that registers for connectivity events (if the android.permission.ACCESS_NETWORK_STATE
     * permission is present) and restarts failed or paused requests.
     */
    @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();
    }
}複製程式碼

它將剛建立的fragment的lifeCycle傳入,並將RequestManager這個listener新增到lifeCycle中,從而實現繫結。在RequestManager的構造方法裡看到了requestTracker,該物件就是跟蹤請求取消,重啟,完成,失敗。RequestManagerFragment 主要是用來連線生命週期方法,RequestManager用來實現生命週期中請求方法,而RequestManagerRetriever繫結了RequestManager。

GlideModule實現

在構造方法中還初始化了通過Glide.get(context);初始化了Glide物件

    /**
     * Get the singleton.
     *
     * @return the singleton
     */
    public static Glide get(Context context) {

        if (glide == null) {
        //同步Glide
            synchronized (Glide.class) {
                if (glide == null) {
                    Context applicationContext = context.getApplicationContext();
                    //解析清單檔案配置的自定義GlideModule的metadata標籤,返回一個GlideModule集合
                    List<GlideModule> modules = new ManifestParser(applicationContext).parse();

                    GlideBuilder builder = new GlideBuilder(applicationContext);
                    //迴圈集合,執行GlideModule 實現類中的方法
                    for (GlideModule module : modules) {
                        module.applyOptions(applicationContext, builder);
                    }
                    glide = builder.createGlide();
                    for (GlideModule module : modules) {
                    //註冊元件
                        module.registerComponents(applicationContext, glide);
                    }
                }
            }
        }

        return glide;
    }複製程式碼

通過get方法單例方式獲取例項,並在初始化時實現了GlideModule配置功能。具體怎麼實現的呢?接下來具體分析一下,在初始化時new 了一個ManifestParser物件並且呼叫了parse()方法返回一個GlideModule型別的List.

//解析metadata具體實現
    public List<GlideModule> parse() {
        List<GlideModule> modules = new ArrayList<GlideModule>();
        try {
        //通過PackageManager獲取metadata所有資訊
            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
                    context.getPackageName(), PackageManager.GET_META_DATA);
             //清單檔案含有metadata
            if (appInfo.metaData != null) {
            //通過key遍歷metadata(對於GlideModule,key就是GlideModule的實現類的全路徑類名)
                for (String key : appInfo.metaData.keySet()) {
                //過濾key對應的value等於GLIDE_MODULE_VALUE(字串GlideModule)
                    if (GLIDE_MODULE_VALUE.equals(appInfo.metaData.get(key))) {
                        //符合條件加入集合中
                        modules.add(parseModule(key));
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException("Unable to find metadata to parse GlideModules", e);
        }

        return modules;
    }複製程式碼

在parse()方法中通過getApplicationInfo方法獲取metaData資訊,若有metaData資料(appInfo.metaData != null),如果metaData的值為GlideModule則呼叫parseModule(key),方法返回GlideModule並add到返回的List中。

檢視parseModule(String className)方法

//通過反射獲取GlideModule例項
    private static GlideModule parseModule(String className) {
        Class<?> clazz;
        try {
            clazz = Class.forName(className);
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("Unable to find GlideModule implementation", e);
        }

        Object module;
        try {
            module = clazz.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException("Unable to instantiate GlideModule implementation for " + clazz, e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Unable to instantiate GlideModule implementation for " + clazz, e);
        }

        if (!(module instanceof GlideModule)) {
            throw new RuntimeException("Expected instanceof GlideModule, but found: " + module);
        }
        return (GlideModule) module;
    }複製程式碼

到此我們看到通過反射的方式獲取我們在清單檔案中宣告的自定義的GlideModule物件。在獲取到
GlideModule集合之後,遍歷了集合並呼叫相應的applyOptions和registerComponents方法,而Glide物件的生成是通過GlideBuilder的createGlide方法建立。

 Glide createGlide() {
        if (sourceService == null) {
            final int cores = Math.max(1, Runtime.getRuntime().availableProcessors());
            //初始化執行緒池
            sourceService = new FifoPriorityThreadPoolExecutor(cores);
        }
        if (diskCacheService == null) {
            diskCacheService = new FifoPriorityThreadPoolExecutor(1);
        }

        MemorySizeCalculator calculator = new MemorySizeCalculator(context);
         //設定Bitmap池
        if (bitmapPool == null) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                int size = calculator.getBitmapPoolSize();
                bitmapPool = new LruBitmapPool(size);
            } else {
                bitmapPool = new BitmapPoolAdapter();
            }
        }

        if (memoryCache == null) {
            memoryCache = new LruResourceCache(calculator.getMemoryCacheSize());
        }

        if (diskCacheFactory == null) {
        //內部磁碟快取
            diskCacheFactory = new InternalCacheDiskCacheFactory(context);
        }

        if (engine == null) {
        //初始化引擎類
            engine = new Engine(memoryCache, diskCacheFactory, diskCacheService, sourceService);
        }

        if (decodeFormat == null) {
            decodeFormat = DecodeFormat.DEFAULT;
        }

        return new Glide(engine, memoryCache, bitmapPool, context, decodeFormat);
    }複製程式碼

看到這都是做的一些初始化操作,並將引數傳遞到Glide構造方法。對於Glide構造方法做的都是一些預設的初始化操作,可以自己去檢視原始碼,此處不再貼出。

通過上面的分析,你就會理解,為什麼之前提到配置資訊只需要實現GlideModule介面,重寫其中的方法,並再清單檔案配置metaData,並且metaData的key是自定義GlideModule的全路徑名,value值必須是GlideModule.會明白當我們不想讓自定義的GlideModule生效時只需要刪除相應的GlideModule。當使用了混淆時為什麼要配置...

-keep public class * implements com.bumptech.glide.module.GlideModule複製程式碼

requestManager.load

對於load方法也是可以接收String,Url,Integer等型別的過載方法,在這裡,我們拿String型別引數分析。

    public DrawableTypeRequest<String> load(String string) {
        return (DrawableTypeRequest<String>) fromString().load(string);
    }
    public DrawableTypeRequest<String> fromString() {
        return loadGeneric(String.class);
    }
   private <T> DrawableTypeRequest<T> loadGeneric(Class<T> modelClass) {
        // 省略一段程式碼

        return optionsApplier.apply(
        // 建立DrawableTypeRequest,它是GenericRequestBuilder的子類
                new DrawableTypeRequest<T>(modelClass, streamModelLoader, fileDescriptorModelLoader, context,
                        glide, requestTracker, lifecycle, optionsApplier));
    }複製程式碼
    @Override
    public DrawableRequestBuilder<ModelType> load(ModelType model) {
    //呼叫弗雷loadd方法
        super.load(model);
        return this;
    }複製程式碼

返回的是DrawableTypeRequest物件,DrawableTypeRequest繼承關係如下

詳談高大上的圖片載入框架Glide   -原始碼篇| 掘金技術徵文
這裡寫圖片描述
,而對於DrawableRequestBuilder類使用的是一個建立者模式,對於常用函式placeholder(),error(),transform等設定都是在此設定,

    /**
     * {@inheritDoc}
     */
    @Override
    public DrawableRequestBuilder<ModelType> placeholder(Drawable drawable) {
        super.placeholder(drawable);
        return this;
    }複製程式碼

我們看到最終又呼叫了父類方法

    /**
     * Sets an Android resource id for a {@link android.graphics.drawable.Drawable} resourceto display while a resource
     * is loading.
     *
     * @param resourceId The id of the resource to use as a placeholder
     * @return This request builder.
     */
    public GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> placeholder(
            int resourceId) {
        this.placeholderId = resourceId;

        return this;
    }複製程式碼

通過檢視父類(GenericRequestBuilder)原始碼你會發現我們每次呼叫placeholder(),error()的等這些方法,其實都是給該類中的變數賦值。

經過一系列操作後,最終呼叫into(imageView)方法來完成圖片的最終載入

建立請求

    /**
     * Sets the {@link ImageView} the resource will be loaded into, cancels any existing loads into the view, and frees
     * any resources Glide may have previously loaded into the view so they may be reused.
     *
     * @see Glide#clear(android.view.View)
     *
     * @param view The view to cancel previous loads for and load the new resource into.
     * @return The {@link com.bumptech.glide.request.target.Target} used to wrap the given {@link ImageView}.
     */
    public Target<TranscodeType> into(ImageView view) {
        Util.assertMainThread();
        if (view == null) {
            throw new IllegalArgumentException("You must pass in a non null View");
        }

        if (!isTransformationSet && view.getScaleType() != null) {
            switch (view.getScaleType()) {
                case CENTER_CROP:
                    applyCenterCrop();
                    break;
                case FIT_CENTER:
                case FIT_START:
                case FIT_END:
                    applyFitCenter();
                    break;
                //$CASES-OMITTED$
                default:
                    // Do nothing.
            }
        }

        return into(glide.buildImageViewTarget(view, transcodeClass));
    }複製程式碼

由上面看到最終呼叫的into方法是

    /**
     * Set the target the resource will be loaded into.
     *
     * @see Glide#clear(com.bumptech.glide.request.target.Target)
     *
     * @param target The target to load the resource into.
     * @return The given target.
     */
    public <Y extends Target<TranscodeType>> Y into(Y target) {
        Util.assertMainThread();
        if (target == null) {
            throw new IllegalArgumentException("You must pass in a non null Target");
        }
        if (!isModelSet) {
            throw new IllegalArgumentException("You must first set a model (try #load())");
        }

//獲取Request 物件
        Request previous = target.getRequest();
//requestTracker是請求跟蹤類物件,主要管理請求的發起,暫停,清除
        if (previous != null) {
            previous.clear();
            requestTracker.removeRequest(previous);
            previous.recycle();
        }

       //建立請求物件
        Request request = buildRequest(target);
        target.setRequest(request);
        //將target加入lifecycle
        lifecycle.addListener(target);
        //執行請求
        requestTracker.runRequest(request);

        return target;
    }複製程式碼

上面都執行都呼叫了 Util.assertMainThread();判斷只能在主執行緒中執行。(更新View當然需要在主執行緒),在Glide中Target我們可以理解成View,只是Glide對我們的View做了一層封裝。
之後通過buildRequest建立請求物件。

//建立請求物件
   private Request buildRequest(Target<TranscodeType> target) {
        if (priority == null) {
        //預設載入優先順序 NORMAL
            priority = Priority.NORMAL;
        }
        //建立Request 
        return buildRequestRecursive(target, null);
    }

    private Request buildRequestRecursive(Target<TranscodeType> target, ThumbnailRequestCoordinator parentCoordinator) {
        if (thumbnailRequestBuilder != null) {
            if (isThumbnailBuilt) {
                throw new IllegalStateException("You cannot use a request as both the main request and a thumbnail, "
                        + "consider using clone() on the request(s) passed to thumbnail()");
            }
            // Recursive case: contains a potentially recursive thumbnail request builder.
            if (thumbnailRequestBuilder.animationFactory.equals(NoAnimation.getFactory())) {
                thumbnailRequestBuilder.animationFactory = animationFactory;
            }

            if (thumbnailRequestBuilder.priority == null) {
                thumbnailRequestBuilder.priority = getThumbnailPriority();
            }

            if (Util.isValidDimensions(overrideWidth, overrideHeight)
                    && !Util.isValidDimensions(thumbnailRequestBuilder.overrideWidth,
                            thumbnailRequestBuilder.overrideHeight)) {
              thumbnailRequestBuilder.override(overrideWidth, overrideHeight);
            }

            ThumbnailRequestCoordinator coordinator = new ThumbnailRequestCoordinator(parentCoordinator);
            Request fullRequest = obtainRequest(target, sizeMultiplier, priority, coordinator);
            // Guard against infinite recursion.
            isThumbnailBuilt = true;
            // Recursively generate thumbnail requests.
            Request thumbRequest = thumbnailRequestBuilder.buildRequestRecursive(target, coordinator);
            isThumbnailBuilt = false;
            coordinator.setRequests(fullRequest, thumbRequest);
            return coordinator;
        } else if (thumbSizeMultiplier != null) {
            // Base case: thumbnail multiplier generates a thumbnail request, but cannot recurse.
            ThumbnailRequestCoordinator coordinator = new ThumbnailRequestCoordinator(parentCoordinator);
            Request fullRequest = obtainRequest(target, sizeMultiplier, priority, coordinator);
            Request thumbnailRequest = obtainRequest(target, thumbSizeMultiplier, getThumbnailPriority(), coordinator);
            coordinator.setRequests(fullRequest, thumbnailRequest);
            return coordinator;
        } else {
            // Base case: no thumbnail.
            return obtainRequest(target, sizeMultiplier, priority, parentCoordinator);
        }
    }複製程式碼

最後呼叫obtainRequest方法

    private Request obtainRequest(Target<TranscodeType> target, float sizeMultiplier, Priority priority,
            RequestCoordinator requestCoordinator) {
        return GenericRequest.obtain(
                loadProvider,
                model,
                signature,
                context,
                priority,
                target,
                sizeMultiplier,
                placeholderDrawable,
                placeholderId,
                errorPlaceholder,
                errorId,
                fallbackDrawable,
                fallbackResource,
                requestListener,
                requestCoordinator,
                glide.getEngine(),
                transformation,
                transcodeClass,
                isCacheable,
                animationFactory,
                overrideWidth,
                overrideHeight,
                diskCacheStrategy);
    }複製程式碼

最終通過GenericRequest.obtain方法建立了

    public static <A, T, Z, R> GenericRequest<A, T, Z, R> obtain(...) {
        @SuppressWarnings("unchecked")
        GenericRequest<A, T, Z, R> request = (GenericRequest<A, T, Z, R>) REQUEST_POOL.poll();
        if (request == null) {
            request = new GenericRequest<A, T, Z, R>();
        }
        //利用設定的引數初始化Request物件
        request.init(...);
        //返回Request物件
        return request;
    }複製程式碼

至此請求物件建立成功,在通過buildRequest建立請求成功後,使用了target.setRequest(request);將請求設定到target,並通過addListener將target加入到lifecycle。上面執行了那麼多都只是請求建立,請求的執行時通過requestTracker.runRequest(request);開始的。

傳送請求

    /**
     * Starts tracking the given request.
     */
    public void runRequest(Request request) {
    //新增request物件到集合中
        requests.add(request);
        if (!isPaused) {
        //如果當前狀態是非暫停的,呼叫begin方法傳送請求
            request.begin();
        } else {
        //將請求加入到掛起的請求集合
            pendingRequests.add(request);
        }
    }複製程式碼

在上面幾句程式碼,我們看到,每次提交請求都將請求加入了一個set中,用它來管理請求,然後通過request的實現類GenericRequest檢視begin方法執行的內容

    /**
     * {@inheritDoc}
     */
    @Override
    public void begin() {
        startTime = LogTime.getLogTime();
        if (model == null) {
        //載入錯誤佔點陣圖設定
            onException(null);
            return;
        }

        status = Status.WAITING_FOR_SIZE;
        //驗證寬高是否合法
        if (Util.isValidDimensions(overrideWidth, overrideHeight)) {
            //傳送請求
            onSizeReady(overrideWidth, overrideHeight);
        } else {
            target.getSize(this);
        }

        if (!isComplete() && !isFailed() && canNotifyStatusChanged()) {
        //載入前預設佔點陣圖設定回撥
            target.onLoadStarted(getPlaceholderDrawable());
        }
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            logV("finished run method in " + LogTime.getElapsedMillis(startTime));
        }
    }
    //獲取設定載入開始時佔點陣圖片的Drawable 物件
    private Drawable getPlaceholderDrawable() {
        if (placeholderDrawable == null && placeholderResourceId > 0) {
            placeholderDrawable = context.getResources().getDrawable(placeholderResourceId);
        }
        return placeholderDrawable;
    }複製程式碼

上面有一句!isComplete() && !isFailed() && canNotifyStatusChanged()判斷,如果都為真會回撥target.onLoadStarted(getPlaceholderDrawable());我們可以看到Target的實現類ImageViewTarget中onLoadStarted的回撥執行語句

//給ImageView設定Drawable 
    @Override
    public void onLoadStarted(Drawable placeholder) {
        view.setImageDrawable(placeholder);
    }複製程式碼

現在你是不是有一種柳暗花明又一村的感覺,終於明白為什麼設定placeHolder後,會在載入前有一個佔點陣圖,當然設定載入錯誤圖片佔點陣圖的原理也是一樣的。只不過回撥執行時機不同。

    /**
     * A callback method that should never be invoked directly.
     */
    @Override
    public void onSizeReady(int width, int height) {
//省略部分程式碼
        status = Status.RUNNING;//將請求狀態更新為執行狀態
//省略部分程式碼

// 進入Engine的入口,請求執行的核心方法
        loadStatus = engine.load(signature, width, height, dataFetcher, loadProvider, transformation, transcoder,
                priority, isMemoryCacheable, diskCacheStrategy, this);
        loadedFromMemoryCache = resource != null;
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            logV("finished onSizeReady in " + LogTime.getElapsedMillis(startTime));
        }
    }複製程式碼

Engine類封裝了資料獲取的重要入口方法,向request層提供這些API,比如load(), release(), clearDiskCache()等方法

    public <T, Z, R> LoadStatus load(Key signature, int width, int height, DataFetcher<T> fetcher,
            DataLoadProvider<T, Z> loadProvider, Transformation<Z> transformation, ResourceTranscoder<Z, R> transcoder,
            Priority priority, boolean isMemoryCacheable, DiskCacheStrategy diskCacheStrategy, ResourceCallback cb) {
            //斷言是否在主執行緒
        Util.assertMainThread();
        long startTime = LogTime.getLogTime();

        final String id = fetcher.getId();
        //建立Enginekey
        EngineKey key = keyFactory.buildKey(id, signature, width, height, loadProvider.getCacheDecoder(),
                loadProvider.getSourceDecoder(), transformation, loadProvider.getEncoder(),
                transcoder, loadProvider.getSourceEncoder());
//從快取載入圖片
        EngineResource<?> cached = loadFromCache(key, isMemoryCacheable);
        if (cached != null) {
        // 獲取資料成功,會回撥target的onResourceReady()
            cb.onResourceReady(cached);
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                logWithTimeAndKey("Loaded resource from cache", startTime, key);
            }
            return null;
        }
// 嘗試從活動Resources 中獲取,它表示的是當前正在使用的Resources,與記憶體快取不同之處是clear快取時不會clear它。
        EngineResource<?> active = loadFromActiveResources(key, isMemoryCacheable);
        if (active != null) {
           //獲取成功回撥
            cb.onResourceReady(active);
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                logWithTimeAndKey("Loaded resource from active resources", startTime, key);
            }
            return null;
        }

        EngineJob current = jobs.get(key);
        //判斷jobs中是否已經存在任務,如果存在說明任務之前已經提交了
        if (current != null) {
            current.addCallback(cb);
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                logWithTimeAndKey("Added to existing load", startTime, key);
            }
            return new LoadStatus(cb, current);
        }

//快取沒有獲取到,建立EngineJob 物件
        EngineJob engineJob = engineJobFactory.build(key, isMemoryCacheable);
        DecodeJob<T, Z, R> decodeJob = new DecodeJob<T, Z, R>(key, width, height, fetcher, loadProvider, transformation,
                transcoder, diskCacheProvider, diskCacheStrategy, priority);
         //EngineRunnable 是任務執行階段的入口
        EngineRunnable runnable = new EngineRunnable(engineJob, decodeJob, priority);
        jobs.put(key, engineJob);
        engineJob.addCallback(cb);
        // 開始提交job
        engineJob.start(runnable);

        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            logWithTimeAndKey("Started new load", startTime, key);
        }
        return new LoadStatus(cb, engineJob);
    }複製程式碼

我們看到先根據呼叫loadFromCache從記憶體載入,若返回值為空再次從活動的資源中載入,若再次為空檢視jobs是否提交過任務,若沒有提交則建立EngineRunnable,並將任務提交到engineJob中。我們先看下EngineJob中的start方法

   //提交任務,將任務加入到執行緒池
    public void start(EngineRunnable engineRunnable) {
        this.engineRunnable = engineRunnable;
        //提交任務到diskCacheService執行緒池
        future = diskCacheService.submit(engineRunnable);
    }複製程式碼

接下來看執行緒類EngineRunnable的run方法,它是任務執行的入口

//任務執行入口
 @Override
    public void run() {
        if (isCancelled) {
            return;
        }

        Exception exception = null;
        Resource<?> resource = null;
        try {
        //資料的獲取,編解碼
            resource = decode();
        } catch (Exception e) {
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                Log.v(TAG, "Exception decoding", e);
            }
            exception = e;
        }
//如果當前狀態是取消,則回收各種資源防止記憶體洩露
        if (isCancelled) {
            if (resource != null) {
                resource.recycle();
            }
            return;
        }

        if (resource == null) {
        //載入失敗回撥
            onLoadFailed(exception);
        } else {
        //載入成功回撥
            onLoadComplete(resource);
        }
    }
    private Resource<?> decode() throws Exception {
        if (isDecodingFromCache()) {
        //// 從DiskLruCache中獲取資料並解碼
            return decodeFromCache();
        } else {
        // 從其他途徑獲取資料並解碼,如網路,本地File,資料流等
            return decodeFromSource();
        }
    }複製程式碼

##DiskLruCache獲取資料 ##

    private Resource<?> decodeFromCache() throws Exception {
        Resource<?> result = null;
        try {
            result = decodeJob.decodeResultFromCache();
        } catch (Exception e) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Exception decoding result from cache: " + e);
            }
        }

        if (result == null) {
            result = decodeJob.decodeSourceFromCache();
        }
        return result;
    }複製程式碼

之後呼叫decodeJob類中的decodeResultFromCache

 public Resource<Z> decodeResultFromCache() throws Exception {
        if (!diskCacheStrategy.cacheResult()) {
            return null;
        }

        long startTime = LogTime.getLogTime();
        //從DiskCache中獲取資源
        Resource<T> transformed = loadFromCache(resultKey);
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            logWithTimeAndKey("Decoded transformed from cache", startTime);
        }
        startTime = LogTime.getLogTime();
        Resource<Z> result = transcode(transformed);
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            logWithTimeAndKey("Transcoded transformed from cache", startTime);
        }
        return result;
    }
    //從DiskCache中獲取資源
    private Resource<T> loadFromCache(Key key) throws IOException {
    //根據key從DiskCache獲取檔案
        File cacheFile = diskCacheProvider.getDiskCache().get(key);
        if (cacheFile == null) {
            return null;
        }

        Resource<T> result = null;
        try {
            result = loadProvider.getCacheDecoder().decode(cacheFile, width, height);
        } finally {
            if (result == null) {
                diskCacheProvider.getDiskCache().delete(key);
            }
        }
        return result;
    }複製程式碼

接下來我們分析decodeFromSource方法

// 呼叫decodeJob來完成資料獲取和編解碼
    private Resource<?> decodeFromSource() throws Exception {
        return decodeJob.decodeFromSource();
    }
    public Resource<Z> decodeFromSource() throws Exception {
    // 獲取資料,解碼
        Resource<T> decoded = decodeSource();
        //編碼並儲存
        return transformEncodeAndTranscode(decoded);
    }
 // 獲取資料,解碼
    private Resource<T> decodeSource() throws Exception {
        Resource<T> decoded = null;
        try {
            long startTime = LogTime.getLogTime();
            //資料拉取
            final A data = fetcher.loadData(priority);
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                logWithTimeAndKey("Fetched data", startTime);
            }
            if (isCancelled) {
                return null;
            }
            //編碼
            decoded = decodeFromSourceData(data);
        } finally {
            fetcher.cleanup();
        }
        return decoded;
    }複製程式碼

在資料獲取時先呼叫DataFetcher的loadData()拉取資料,對於DataFetcher的實現類有好幾個,我們拿從url拉取資料為例,也就是HttpUrlFetcher類

   @Override
    public InputStream loadData(Priority priority) throws Exception {
        return loadDataWithRedirects(glideUrl.toURL(), 0 /*redirects*/, null /*lastUrl*/, glideUrl.getHeaders());
    }
//返回InputStream 物件
    private InputStream loadDataWithRedirects(URL url, int redirects, URL lastUrl, Map<String, String> headers)
            throws IOException {
        if (redirects >= MAXIMUM_REDIRECTS) {
            throw new IOException("Too many (> " + MAXIMUM_REDIRECTS + ") redirects!");
        } else {
            // Comparing the URLs using .equals performs additional network I/O and is generally broken.
            // See http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html.
            try {
                if (lastUrl != null && url.toURI().equals(lastUrl.toURI())) {
                    throw new IOException("In re-direct loop");
                }
            } catch (URISyntaxException e) {
                // Do nothing, this is best effort.
            }
        }
        // 靜態工廠模式建立HttpURLConnection物件
        urlConnection = connectionFactory.build(url);
        for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
          urlConnection.addRequestProperty(headerEntry.getKey(), headerEntry.getValue());
        }
        //設定請求引數
        //設定連線超時時間2500ms
        urlConnection.setConnectTimeout(2500);
        //設定讀取超時時間2500ms
        urlConnection.setReadTimeout(2500);
        //不使用http快取
        urlConnection.setUseCaches(false);
        urlConnection.setDoInput(true);

        // Connect explicitly to avoid errors in decoders if connection fails.
        urlConnection.connect();
        if (isCancelled) {
            return null;
        }
        final int statusCode = urlConnection.getResponseCode();
        if (statusCode / 100 == 2) {
        //請求成功
            return getStreamForSuccessfulRequest(urlConnection);
        } else if (statusCode / 100 == 3) {
        //
            String redirectUrlString = urlConnection.getHeaderField("Location");
            if (TextUtils.isEmpty(redirectUrlString)) {
                throw new IOException("Received empty or null redirect url");
            }
            URL redirectUrl = new URL(url, redirectUrlString);
            return loadDataWithRedirects(redirectUrl, redirects + 1, url, headers);
        } else {
            if (statusCode == -1) {
                throw new IOException("Unable to retrieve response code from HttpUrlConnection.");
            }
            throw new IOException("Request failed " + statusCode + ": " + urlConnection.getResponseMessage());
        }
    }複製程式碼

看到這終於看到了網路載入請求,我們也可以自定義DataFetcher,從而使用其他網路庫,如OkHttp,Volley.
最後我們再看下transformEncodeAndTranscode方法

 private Resource<Z> transformEncodeAndTranscode(Resource<T> decoded) {
        long startTime = LogTime.getLogTime();
        // 根據ImageView的scaleType等引數計算真正被ImageView使用的圖片寬高,並儲存真正寬高的圖片。
        Resource<T> transformed = transform(decoded);
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            logWithTimeAndKey("Transformed resource from source", startTime);
        }
  // 寫入到DiskLruCache中,下次就可以直接從DiskLruCache獲取使用
        writeTransformedToCache(transformed);

        startTime = LogTime.getLogTime();
          // 轉碼,將源圖片轉碼為ImageView所需的圖片格式
        Resource<Z> result = transcode(transformed);
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            logWithTimeAndKey("Transcoded transformed from source", startTime);
        }
        return result;
    }複製程式碼

至此,圖片載入流程已經介紹完畢,當然還有很多的地方沒有提到,相信如果你在閱讀本文的同時,自己跟蹤原始碼會輕鬆很多,如果自己不跟著原始碼走的話,可能這篇文章看幾遍對Glide原理理解的也是雲裡霧裡,或者說當時看的懂,但是很快就不記得。所以切記自己要跟著原始碼過一遍。

本片文章實在是長,能讀完本文章也是需要一定毅力的...若文章有不足或者錯誤的地方,歡迎指正,以防止給其他讀者錯誤引導。

本次徵文活動的連結:
gold.xitu.io/post/58522d…

相關文章