詳談高大上的圖片加載框架Glide -源碼篇

在上篇文章中,咱們介紹了Glide圖片加載框架的使用,經過以前的學習,咱們可能已經能熟練的將Glide圖片加載框架運用到咱們的項目中,可是若是有人問你它是如何加載,工做原理是怎樣的?爲何自定義GlideModule只須要在Manifest文件中加入meta-data便可?等等不少加載流程以及使用的注意事項。固然要想搞明白這些問題,就須要咱們對Glide源碼有個大體的認識,去剖析源碼深處的奧祕。 接下來就讓咱們一塊兒去進入Glide的源碼世界,本篇文章分析的是Glide 3.7.0版本。特別提醒,閱讀本片文章以前要對Glide的用法要先有一個瞭解,能夠先閱讀上篇文章,詳談高大上的圖片加載框架Glide -應用篇html

此篇文章是本身學習的一個記錄,若對閱讀文章的你有必定幫助,非常高興,固然文章若有不足或者錯誤的地方,歡迎指正,避免我給其餘讀者錯誤引導java

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

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請求如何和綁定生命週期自動請求,暫停,以及銷燬。app

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 對象。ide

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,以下代碼oop

/** * 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繼承關係以下 這裏寫圖片描述,而對於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原理理解的也是雲裏霧裏,或者說當時看的懂,可是很快就不記得。因此切記本身要跟着源碼過一遍。

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

歡迎你們關注個人CSDN微博

相關文章
相關標籤/搜索