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

在上篇Glide文章中,我們介紹了Glide圖片加載框架的使用,通過(guò)之前的學(xué)習(xí),我們可能已經(jīng)能熟練的將Glide圖片加載框架運(yùn)用到我們的項(xiàng)目中,但是如果有人問(wèn)你它是如何加載,工作原理是怎樣的?為什么自定義GlideModule只需要在Manifest文件中加入meta-data即可?等等很多加載流程以及使用的注意事項(xiàng)。當(dāng)然要想搞明白這些問(wèn)題,就需要我們對(duì)Glide源碼有個(gè)大致的認(rèn)識(shí),去剖析源碼深處的奧秘。
接下來(lái)就讓我們一起去進(jìn)入Glide的源碼世界,本篇文章分析的是Glide 3.7.0版本。特別提醒,閱讀本片文章之前要對(duì)Glide的用法要先有一個(gè)了解,可以先閱讀上篇文章,詳談高大上的圖片加載框架Glide -應(yīng)用篇

此篇文章是自己學(xué)習(xí)的一個(gè)記錄,若對(duì)閱讀文章的你有一定幫助,很是高興,當(dāng)然文章如有不足或者錯(cuò)誤的地方,歡迎指正,避免我給其他讀者錯(cuò)誤引導(dǎo)。

如果你閱讀過(guò)上篇文章,或者你使用過(guò)Glide,就知道Glide加載圖片的最簡(jiǎn)單方式就是

Glide.with(context).load(url). placeholder(R.drawable.placeholder).into(imageView)。

那么這篇文章就以這句簡(jiǎn)單的代碼為主線,逐步深入Glide的源碼。

Glide.with(context)

   //獲取RequestManager對(duì)象,該類實(shí)現(xiàn)了LifeCycleListener接口,綁定Activity/Fragment生命周期,對(duì)請(qǐng)求進(jìn)行暫停,恢復(fù),清除操作
    public static RequestManager with(Context context) {
    //得到RequestManagerRetriever實(shí)例,該類注意將RequestManager和自定義Fragment(如RequestManagerFragment,SupportRequestManagerFragment)綁定,從而實(shí)現(xiàn)在生命周期管理回調(diào)
        RequestManagerRetriever retriever = RequestManagerRetriever.get();
        return retriever.get(context);
    }

Glide有四個(gè)靜態(tài)的重載方法with(),其內(nèi)部都通過(guò)RequestManagerRetriever相應(yīng)的get重載方法獲取一個(gè)RequestManager對(duì)象。RequestManagerRetriever提供各種重載方法的好處就是可以將Glide的加載請(qǐng)求與Activity/Fragment的生命周期綁定而自動(dòng)執(zhí)行請(qǐng)求,暫停操作。

接下來(lái)我們拿Activity參數(shù)分析Glide請(qǐng)求如何和綁定生命周期自動(dòng)請(qǐng)求,暫停,以及銷毀。


    public RequestManager get(Activity activity) {
        if (Util.isOnBackgroundThread() || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            return get(activity.getApplicationContext());
        } else {
        
           //判斷activity是否已經(jīng)是銷毀狀態(tài)
            assertNotDestroyed(activity);
            //獲取FragmentManager 對(duì)象
            android.app.FragmentManager fm = activity.getFragmentManager();
            //創(chuàng)建Fragment,RequestManager并將其綁定
            return fragmentGet(activity, fm);
        }
    }

assertNotDestroyed主要斷言Activity是否已經(jīng)Destroyed。若是沒(méi)有銷毀,或者Activity的FragmentManager ,然后通過(guò)fragmentGet返回RequestManager。

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    RequestManager fragmentGet(Context context, android.app.FragmentManager fm) {
      //*獲取RequestManagerFragment,主要利用Frament進(jìn)行請(qǐng)求的生命周期管理
        RequestManagerFragment current = getRequestManagerFragment(fm);
        RequestManager requestManager = current.getRequestManager();
        //requestManager 為空,即首次加載初始化requestManager ,并調(diào)用setRequestManager設(shè)置到RequestManagerFragment 
        if (requestManager == null) {
            requestManager = new RequestManager(context, current.getLifecycle(), current.getRequestManagerTreeNode());
            current.setRequestManager(requestManager);
        }
        return requestManager;
    }

 //獲取Fragment對(duì)象
    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;
    }

最終通過(guò)getRequestManagerFragment()方法獲取一個(gè)RequestManagerFragment 對(duì)象。

public class RequestManagerFragment extends Fragment {
    private final ActivityFragmentLifecycle lifecycle;
    //省略部分代碼...
    @Override
    public void onStart() {
        super.onStart();
        //關(guān)聯(lián)lifecycle相應(yīng)onStart方法
        lifecycle.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
         //關(guān)聯(lián)lifecycle相應(yīng)onStop方法
        lifecycle.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
         //關(guān)聯(lián)lifecycle相應(yīng)onDestroy方法
        lifecycle.onDestroy();
    }
}

此時(shí)我們看到RequestManagerFragment 繼承了Fragment.并且在其生命周期onStart(),onStop(),onDestory(),調(diào)用了ActivityFragmentLifecycle 相應(yīng)的方法,ActivityFragmentLifecycle實(shí)現(xiàn)了Lifecycle 接口,在其中通過(guò)addListener(LifecycleListener listener)回調(diào)相應(yīng)(LifecycleListener的 onStart(),onStop(),onDestory())周期方法。LifecycleListener是監(jiān)聽(tīng)生命周期時(shí)間接口。
再次回到fragmentGet方法里下面一句代碼


//創(chuàng)建RequestManager傳入Lifecycle實(shí)現(xiàn)類,如ActivityFragmentLifecycle
requestManager = new RequestManager(context, current.getLifecycle(), current.getRequestManagerTreeNode());

對(duì)于RequestManager類,該類實(shí)現(xiàn)了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;
        //通過(guò)Glide的靜態(tài)方法獲取Glide實(shí)例。單例模式
        this.glide = Glide.get(context);
        this.optionsApplier = new OptionsApplier();

//通過(guò)工廠類ConnectivityMonitorFactory的build方法獲取ConnectivityMonitor (一個(gè)用于監(jiān)控網(wǎng)絡(luò)連接事件的接口)
        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 {
        //設(shè)置監(jiān)聽(tīng)
            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();
    }
}

它將剛創(chuàng)建的fragment的lifeCycle傳入,并將RequestManager這個(gè)listener添加到lifeCycle中,從而實(shí)現(xiàn)綁定。在RequestManager的構(gòu)造方法里看到了requestTracker,該對(duì)象就是跟蹤請(qǐng)求取消,重啟,完成,失敗。RequestManagerFragment 主要是用來(lái)連接生命周期方法,RequestManager用來(lái)實(shí)現(xiàn)生命周期中請(qǐng)求方法,而RequestManagerRetriever綁定了RequestManager。

GlideModule實(shí)現(xiàn)

在構(gòu)造方法中還初始化了通過(guò)Glide.get(context);初始化了Glide對(duì)象

    /**
     * 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標(biāo)簽,返回一個(gè)GlideModule集合
                    List<GlideModule> modules = new ManifestParser(applicationContext).parse();

                    GlideBuilder builder = new GlideBuilder(applicationContext);
                    //循環(huán)集合,執(zhí)行GlideModule 實(shí)現(xiàn)類中的方法
                    for (GlideModule module : modules) {
                        module.applyOptions(applicationContext, builder);
                    }
                    glide = builder.createGlide();
                    for (GlideModule module : modules) {
                    //注冊(cè)組件
                        module.registerComponents(applicationContext, glide);
                    }
                }
            }
        }

        return glide;
    }

通過(guò)get方法單例方式獲取實(shí)例,并在初始化時(shí)實(shí)現(xiàn)了GlideModule配置功能。具體怎么實(shí)現(xiàn)的呢?接下來(lái)具體分析一下,在初始化時(shí)new 了一個(gè)ManifestParser對(duì)象并且調(diào)用了parse()方法返回一個(gè)GlideModule類型的List.

//解析metadata具體實(shí)現(xiàn)
    public List<GlideModule> parse() {
        List<GlideModule> modules = new ArrayList<GlideModule>();
        try {
        //通過(guò)PackageManager獲取metadata所有信息
            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
                    context.getPackageName(), PackageManager.GET_META_DATA);
             //清單文件含有metadata
            if (appInfo.metaData != null) {
            //通過(guò)key遍歷metadata(對(duì)于GlideModule,key就是GlideModule的實(shí)現(xiàn)類的全路徑類名)
                for (String key : appInfo.metaData.keySet()) {
                //過(guò)濾key對(duì)應(yīng)的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()方法中通過(guò)getApplicationInfo方法獲取metaData信息,若有metaData數(shù)據(jù)(appInfo.metaData != null),如果metaData的值為GlideModule則調(diào)用parseModule(key),方法返回GlideModule并add到返回的List中。

查看parseModule(String className)方法

//通過(guò)反射獲取GlideModule實(shí)例
    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;
    }

到此我們看到通過(guò)反射的方式獲取我們?cè)谇鍐挝募新暶鞯淖远x的GlideModule對(duì)象。在獲取到
GlideModule集合之后,遍歷了集合并調(diào)用相應(yīng)的applyOptions和registerComponents方法,而Glide對(duì)象的生成是通過(guò)GlideBuilder的createGlide方法創(chuàng)建。

 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);
         //設(shè)置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) {
        //內(nèi)部磁盤緩存
            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);
    }

看到這都是做的一些初始化操作,并將參數(shù)傳遞到Glide構(gòu)造方法。對(duì)于Glide構(gòu)造方法做的都是一些默認(rèn)的初始化操作,可以自己去查看源碼,此處不再貼出。

通過(guò)上面的分析,你就會(huì)理解,為什么之前提到配置信息只需要實(shí)現(xiàn)GlideModule接口,重寫其中的方法,并再清單文件配置metaData,并且metaData的key是自定義GlideModule的全路徑名,value值必須是GlideModule.會(huì)明白當(dāng)我們不想讓自定義的GlideModule生效時(shí)只需要?jiǎng)h除相應(yīng)的GlideModule。當(dāng)使用了混淆時(shí)為什么要配置...

-keep public class * implements com.bumptech.glide.module.GlideModule

requestManager.load##

對(duì)于load方法也是可以接收String,Url,Integer等類型的重載方法,在這里,我們拿String類型參數(shù)分析。

    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(
        // 創(chuàng)建DrawableTypeRequest,它是GenericRequestBuilder的子類
                new DrawableTypeRequest<T>(modelClass, streamModelLoader, fileDescriptorModelLoader, context,
                        glide, requestTracker, lifecycle, optionsApplier));
    }
    @Override
    public DrawableRequestBuilder<ModelType> load(ModelType model) {
    //調(diào)用弗雷loadd方法
        super.load(model);
        return this;
    }

返回的是DrawableTypeRequest對(duì)象,DrawableTypeRequest繼承關(guān)系如下


這里寫圖片描述

,而對(duì)于DrawableRequestBuilder類使用的是一個(gè)創(chuàng)建者模式,對(duì)于常用函數(shù)placeholder(),error(),transform等設(shè)置都是在此設(shè)置,

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

我們看到最終又調(diào)用了父類方法

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

通過(guò)查看父類(GenericRequestBuilder)源碼你會(huì)發(fā)現(xiàn)我們每次調(diào)用placeholder(),error()的等這些方法,其實(shí)都是給該類中的變量賦值。

經(jīng)過(guò)一系列操作后,最終調(diào)用into(imageView)方法來(lái)完成圖片的最終加載

創(chuàng)建請(qǐng)求

    /**
     * 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));
    }

由上面看到最終調(diào)用的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 對(duì)象
        Request previous = target.getRequest();
//requestTracker是請(qǐng)求跟蹤類對(duì)象,主要管理請(qǐng)求的發(fā)起,暫停,清除
        if (previous != null) {
            previous.clear();
            requestTracker.removeRequest(previous);
            previous.recycle();
        }

       //創(chuàng)建請(qǐng)求對(duì)象
        Request request = buildRequest(target);
        target.setRequest(request);
        //將target加入lifecycle
        lifecycle.addListener(target);
        //執(zhí)行請(qǐng)求
        requestTracker.runRequest(request);

        return target;
    }

上面都執(zhí)行都調(diào)用了 Util.assertMainThread();判斷只能在主線程中執(zhí)行。(更新View當(dāng)然需要在主線程),在Glide中Target我們可以理解成View,只是Glide對(duì)我們的View做了一層封裝。
之后通過(guò)buildRequest創(chuàng)建請(qǐng)求對(duì)象。

//創(chuàng)建請(qǐng)求對(duì)象
   private Request buildRequest(Target<TranscodeType> target) {
        if (priority == null) {
        //默認(rèn)加載優(yōu)先級(jí) NORMAL
            priority = Priority.NORMAL;
        }
        //創(chuàng)建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);
        }
    }

最后調(diào)用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);
    }

最終通過(guò)GenericRequest.obtain方法創(chuàng)建了

    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>();
        }
        //利用設(shè)置的參數(shù)初始化Request對(duì)象
        request.init(...);
        //返回Request對(duì)象
        return request;
    }

至此請(qǐng)求對(duì)象創(chuàng)建成功,在通過(guò)buildRequest創(chuàng)建請(qǐng)求成功后,使用了target.setRequest(request);將請(qǐng)求設(shè)置到target,并通過(guò)addListener將target加入到lifecycle。上面執(zhí)行了那么多都只是請(qǐng)求創(chuàng)建,請(qǐng)求的執(zhí)行時(shí)通過(guò)requestTracker.runRequest(request);開(kāi)始的。

發(fā)送請(qǐng)求

    /**
     * Starts tracking the given request.
     */
    public void runRequest(Request request) {
    //添加request對(duì)象到集合中
        requests.add(request);
        if (!isPaused) {
        //如果當(dāng)前狀態(tài)是非暫停的,調(diào)用begin方法發(fā)送請(qǐng)求
            request.begin();
        } else {
        //將請(qǐng)求加入到掛起的請(qǐng)求集合
            pendingRequests.add(request);
        }
    }

在上面幾句代碼,我們看到,每次提交請(qǐng)求都將請(qǐng)求加入了一個(gè)set中,用它來(lái)管理請(qǐng)求,然后通過(guò)request的實(shí)現(xiàn)類GenericRequest查看begin方法執(zhí)行的內(nèi)容

    /**
     * {@inheritDoc}
     */
    @Override
    public void begin() {
        startTime = LogTime.getLogTime();
        if (model == null) {
        //加載錯(cuò)誤占位圖設(shè)置
            onException(null);
            return;
        }

        status = Status.WAITING_FOR_SIZE;
        //驗(yàn)證寬高是否合法
        if (Util.isValidDimensions(overrideWidth, overrideHeight)) {
            //發(fā)送請(qǐng)求
            onSizeReady(overrideWidth, overrideHeight);
        } else {
            target.getSize(this);
        }

        if (!isComplete() && !isFailed() && canNotifyStatusChanged()) {
        //加載前默認(rèn)占位圖設(shè)置回調(diào)
            target.onLoadStarted(getPlaceholderDrawable());
        }
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            logV("finished run method in " + LogTime.getElapsedMillis(startTime));
        }
    }
    //獲取設(shè)置加載開(kāi)始時(shí)占位圖片的Drawable 對(duì)象
    private Drawable getPlaceholderDrawable() {
        if (placeholderDrawable == null && placeholderResourceId > 0) {
            placeholderDrawable = context.getResources().getDrawable(placeholderResourceId);
        }
        return placeholderDrawable;
    }

上面有一句!isComplete() && !isFailed() && canNotifyStatusChanged()判斷,如果都為真會(huì)回調(diào)target.onLoadStarted(getPlaceholderDrawable());我們可以看到Target的實(shí)現(xiàn)類ImageViewTarget中onLoadStarted的回調(diào)執(zhí)行語(yǔ)句

//給ImageView設(shè)置Drawable 
    @Override
    public void onLoadStarted(Drawable placeholder) {
        view.setImageDrawable(placeholder);
    }

現(xiàn)在你是不是有一種柳暗花明又一村的感覺(jué),終于明白為什么設(shè)置placeHolder后,會(huì)在加載前有一個(gè)占位圖,當(dāng)然設(shè)置加載錯(cuò)誤圖片占位圖的原理也是一樣的。只不過(guò)回調(diào)執(zhí)行時(shí)機(jī)不同。

    /**
     * A callback method that should never be invoked directly.
     */
    @Override
    public void onSizeReady(int width, int height) {
//省略部分代碼
        status = Status.RUNNING;//將請(qǐng)求狀態(tài)更新為運(yùn)行狀態(tài)
//省略部分代碼

// 進(jìn)入Engine的入口,請(qǐng)求執(zhí)行的核心方法
        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類封裝了數(shù)據(jù)獲取的重要入口方法,向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();
        //創(chuàng)建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) {
        // 獲取數(shù)據(jù)成功,會(huì)回調(diào)target的onResourceReady()
            cb.onResourceReady(cached);
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                logWithTimeAndKey("Loaded resource from cache", startTime, key);
            }
            return null;
        }
// 嘗試從活動(dòng)Resources 中獲取,它表示的是當(dāng)前正在使用的Resources,與內(nèi)存緩存不同之處是clear緩存時(shí)不會(huì)clear它。
        EngineResource<?> active = loadFromActiveResources(key, isMemoryCacheable);
        if (active != null) {
           //獲取成功回調(diào)
            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中是否已經(jīng)存在任務(wù),如果存在說(shuō)明任務(wù)之前已經(jīng)提交了
        if (current != null) {
            current.addCallback(cb);
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                logWithTimeAndKey("Added to existing load", startTime, key);
            }
            return new LoadStatus(cb, current);
        }

//緩存沒(méi)有獲取到,創(chuàng)建EngineJob 對(duì)象
        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 是任務(wù)執(zhí)行階段的入口
        EngineRunnable runnable = new EngineRunnable(engineJob, decodeJob, priority);
        jobs.put(key, engineJob);
        engineJob.addCallback(cb);
        // 開(kāi)始提交job
        engineJob.start(runnable);

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

我們看到先根據(jù)調(diào)用loadFromCache從內(nèi)存加載,若返回值為空再次從活動(dòng)的資源中加載,若再次為空查看jobs是否提交過(guò)任務(wù),若沒(méi)有提交則創(chuàng)建EngineRunnable,并將任務(wù)提交到engineJob中。我們先看下EngineJob中的start方法

   //提交任務(wù),將任務(wù)加入到線程池
    public void start(EngineRunnable engineRunnable) {
        this.engineRunnable = engineRunnable;
        //提交任務(wù)到diskCacheService線程池
        future = diskCacheService.submit(engineRunnable);
    }

接下來(lái)看線程類EngineRunnable的run方法,它是任務(wù)執(zhí)行的入口

//任務(wù)運(yùn)行入口
 @Override
    public void run() {
        if (isCancelled) {
            return;
        }

        Exception exception = null;
        Resource<?> resource = null;
        try {
        //數(shù)據(jù)的獲取,編解碼
            resource = decode();
        } catch (Exception e) {
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                Log.v(TAG, "Exception decoding", e);
            }
            exception = e;
        }
//如果當(dāng)前狀態(tài)是取消,則回收各種資源防止內(nèi)存泄露
        if (isCancelled) {
            if (resource != null) {
                resource.recycle();
            }
            return;
        }

        if (resource == null) {
        //加載失敗回調(diào)
            onLoadFailed(exception);
        } else {
        //加載成功回調(diào)
            onLoadComplete(resource);
        }
    }
    private Resource<?> decode() throws Exception {
        if (isDecodingFromCache()) {
        //// 從DiskLruCache中獲取數(shù)據(jù)并解碼
            return decodeFromCache();
        } else {
        // 從其他途徑獲取數(shù)據(jù)并解碼,如網(wǎng)絡(luò),本地File,數(shù)據(jù)流等
            return decodeFromSource();
        }
    }

DiskLruCache獲取數(shù)據(jù)

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

之后調(diào)用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 {
    //根據(jù)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;
    }

接下來(lái)我們分析decodeFromSource方法

// 調(diào)用decodeJob來(lái)完成數(shù)據(jù)獲取和編解碼
    private Resource<?> decodeFromSource() throws Exception {
        return decodeJob.decodeFromSource();
    }
    public Resource<Z> decodeFromSource() throws Exception {
    // 獲取數(shù)據(jù),解碼
        Resource<T> decoded = decodeSource();
        //編碼并保存
        return transformEncodeAndTranscode(decoded);
    }
 // 獲取數(shù)據(jù),解碼
    private Resource<T> decodeSource() throws Exception {
        Resource<T> decoded = null;
        try {
            long startTime = LogTime.getLogTime();
            //數(shù)據(jù)拉取
            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;
    }

在數(shù)據(jù)獲取時(shí)先調(diào)用DataFetcher的loadData()拉取數(shù)據(jù),對(duì)于DataFetcher的實(shí)現(xiàn)類有好幾個(gè),我們拿從url拉取數(shù)據(jù)為例,也就是HttpUrlFetcher類

   @Override
    public InputStream loadData(Priority priority) throws Exception {
        return loadDataWithRedirects(glideUrl.toURL(), 0 /*redirects*/, null /*lastUrl*/, glideUrl.getHeaders());
    }
//返回InputStream 對(duì)象
    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.
            }
        }
        // 靜態(tài)工廠模式創(chuàng)建HttpURLConnection對(duì)象
        urlConnection = connectionFactory.build(url);
        for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
          urlConnection.addRequestProperty(headerEntry.getKey(), headerEntry.getValue());
        }
        //設(shè)置請(qǐng)求參數(shù)
        //設(shè)置連接超時(shí)時(shí)間2500ms
        urlConnection.setConnectTimeout(2500);
        //設(shè)置讀取超時(shí)時(shí)間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) {
        //請(qǐng)求成功
            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());
        }
    }

看到這終于看到了網(wǎng)絡(luò)加載請(qǐng)求,我們也可以自定義DataFetcher,從而使用其他網(wǎng)絡(luò)庫(kù),如OkHttp,Volley.
最后我們?cè)倏聪聇ransformEncodeAndTranscode方法

 private Resource<Z> transformEncodeAndTranscode(Resource<T> decoded) {
        long startTime = LogTime.getLogTime();
        // 根據(jù)ImageView的scaleType等參數(shù)計(jì)算真正被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();
          // 轉(zhuǎn)碼,將源圖片轉(zhuǎn)碼為ImageView所需的圖片格式
        Resource<Z> result = transcode(transformed);
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            logWithTimeAndKey("Transcoded transformed from source", startTime);
        }
        return result;
    }

至此,圖片加載流程已經(jīng)介紹完畢,當(dāng)然還有很多的地方?jīng)]有提到,相信如果你在閱讀本文的同時(shí),自己跟蹤源碼會(huì)輕松很多,如果自己不跟著源碼走的話,可能這篇文章看幾遍對(duì)Glide原理理解的也是云里霧里,或者說(shuō)當(dāng)時(shí)看的懂,但是很快就不記得。所以切記自己要跟著源碼過(guò)一遍。

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在Android設(shè)備上,加載網(wǎng)絡(luò)圖片一直是一個(gè)頭疼的問(wèn)題,因?yàn)锳ndroid設(shè)備種類繁多(當(dāng)然最主要的是配置),處...
    Code4Android閱讀 17,929評(píng)論 5 96
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,928評(píng)論 25 709
  • 斷舍離 我生活狀態(tài)的轉(zhuǎn)變,都是歸功于這一本書(shū),從這里理清了自己的追求,同時(shí)還有整理干凈了自己的生活瑣事。斷舍離也是...
    酷酷的我閱讀 247評(píng)論 0 0
  • 如果讓我說(shuō)出一部最喜歡的周星馳的電影,我恐怕沒(méi)辦法排序?!断矂≈酢罚渡倭肿闱颉?,《逃學(xué)威龍系列》等我都很喜歡。...
    飛魚(yú)man閱讀 879評(píng)論 0 4
  • 文/thx2956 你若一幅畫(huà)面浮現(xiàn) 穿越我的眼簾 輕撫容顏 指尖詩(shī)意飄散 你若一首歌繞梁 掛我嘴邊輕唱 音符歡跳...
    神呼吸閱讀 587評(píng)論 10 10

友情鏈接更多精彩內(nèi)容