Glide的load()过程源码分析

  上一次文章中分析了Glide的with()过程,接着来分析load()过程。
  在with()方法结束时我们得到了RequestManager对象,所以这个load()方法就是RequestManager类中的方法。

public DrawableTypeRequest load(String string) {
        return (DrawableTypeRequest) fromString().load(string);
    }
public DrawableTypeRequest load(Uri uri) {
        return (DrawableTypeRequest) fromUri().load(uri);
    }
 public DrawableTypeRequest load(File file) {
        return (DrawableTypeRequest) fromFile().load(file);
    }
 public DrawableTypeRequest load(Integer resourceId) {
        return (DrawableTypeRequest) fromResource().load(resourceId);
    }

  load()方法有很多重载的方法。这里只贴出来了一部分。可以从网络Url,本地文件等加载资源,所以load()方法有很多的重载形式。
这次我们从加载String形式的Url字符串的get()方法开始分析。

load(string)方法中调用的方法

public DrawableTypeRequest fromString() {
        return loadGeneric(String.class);
    }
private  DrawableTypeRequest loadGeneric(Class modelClass) {
        ModelLoader streamModelLoader = Glide.buildStreamModelLoader(modelClass, context);
        ModelLoader fileDescriptorModelLoader =
                Glide.buildFileDescriptorModelLoader(modelClass, context);
        if (modelClass != null && streamModelLoader == null && fileDescriptorModelLoader == null) {
            throw new IllegalArgumentException("Unknown type " + modelClass + ". You must provide a Model of a type for"
                    + " which there is a registered ModelLoader, if you are using a custom model, you must first call"
                    + " Glide#register with a ModelLoaderFactory for your custom model class");
        }

        return optionsApplier.apply(
                new DrawableTypeRequest(modelClass, streamModelLoader, fileDescriptorModelLoader, context,
                        glide, requestTracker, lifecycle, optionsApplier));
    }

  在fromString()方法中调用了loadGeneric()方法。这个方法的泛型的类型是String.class类型。在loadGeneric()方法中,

ModelLoader streamModelLoader = Glide.buildStreamModelLoader(modelClass, context);

首先是这行代码,我们来分析一下这行代码。分析一下得到这个ModelLoader对象的过程。
在这行代码中,传入的泛型的类型分别为String.class类型InputStream类型,然后调用了Glide的buildStreamModelLoader()方法来生成了一个ModelLoader对象,因为传入的泛型的类型分别为String.class类型,所以最后生成的是StreamStringLoader对象,它是实现了ModelLoader接口的。

  • ModelLoader对象是用于加载图片的

Glide类里面的方法

public static  ModelLoader buildStreamModelLoader(Class modelClass, Context context) {
        return buildModelLoader(modelClass, InputStream.class, context);
    }
public static  ModelLoader buildModelLoader(Class modelClass, Class resourceClass,
            Context context) {
         if (modelClass == null) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Unable to load null model, setting placeholder only");
            }
            return null;
        }
        return Glide.get(context).getLoaderFactory().buildModelLoader(modelClass, resourceClass);
    }

modelClass类型为String.class,resourceClass的类型为InputStream.class。然后在buildModelLoader()方法中调用了

Glide.get(context).getLoaderFactory().buildModelLoader(modelClass, resourceClass);

一步一步来分析,先看Glide的 get(Context context)方法

public static Glide get(Context context) {
        if (glide == null) {
            synchronized (Glide.class) {
                if (glide == null) {
                    Context applicationContext = context.getApplicationContext();
                    List modules = new ManifestParser(applicationContext).parse();

                    GlideBuilder builder = new GlideBuilder(applicationContext);
                    for (GlideModule module : modules) {
                        module.applyOptions(applicationContext, builder);
                    }
                   //通过GlideBuilder类的createGlide()方法来创建Glide对象
                    glide = builder.createGlide();
                    for (GlideModule module : modules) {
                        module.registerComponents(applicationContext, glide);
                    }
                }
            }
        }

        return glide;
    }

可以看到在这个方法中,通过GlideBuilder类的createGlide()方法来创建Glide对象,来看看创建Glide对象的过程。

GlideBuilder类createGlide()方法

private final Context context;

    private Engine engine;
    private BitmapPool bitmapPool;
    private MemoryCache memoryCache;
    private ExecutorService sourceService;
    private ExecutorService diskCacheService;
    private DecodeFormat decodeFormat;
    private DiskCache.Factory diskCacheFactory;

    public GlideBuilder(Context context) {
        this.context = context.getApplicationContext();
    }
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);
        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) {
           //Glide实现内存缓存所使用的
            memoryCache = new LruResourceCache(calculator.getMemoryCacheSize());
        }

        if (diskCacheFactory == null) {
           //Glide实现磁盘缓存所使用的
            diskCacheFactory = new InternalCacheDiskCacheFactory(context);
        }

        if (engine == null) {
            engine = new Engine(memoryCache, diskCacheFactory, diskCacheService, sourceService);
        }

        if (decodeFormat == null) {
            decodeFormat = DecodeFormat.DEFAULT;
        }
       //创建Glide对象
        return new Glide(engine, memoryCache, bitmapPool, context, decodeFormat);
    }

里面初始化了很多对象,并把初始化的东西传入了Glide的构造器中。

Glide的构造器中

private final GenericLoaderFactory loaderFactory;
Glide(Engine engine, MemoryCache memoryCache, BitmapPool bitmapPool, Context context, DecodeFormat decodeFormat) {
        ......
        loaderFactory = new GenericLoaderFactory(context);
        ......
        register(String.class, InputStream.class, new StreamStringLoader.Factory());
    }

在这个Glide的这个构造器中就是进行一些初始化的操作,这里我只贴出了我们要分析的部分。在Glide的构造器中初始化了GenericLoaderFactory类,并调用了Glide的register()方法

  • GenericLoaderFactory类:相当于一个工厂。这个类里面保存了各种ModelLoader和各种ModelLoaderFactory,并且通过保存的各种ModelLoaderFactory来创建各种ModelLoader。这个类之后还要来分析。

看一下Glide的register()方法的参数中传入的new StreamStringLoader.Factory()。

StreamStringLoader.Factory()

 public static class Factory implements ModelLoaderFactory {
        @Override
        public ModelLoader build(Context context, GenericLoaderFactory factories) {
            return new StreamStringLoader(factories.buildModelLoader(Uri.class, InputStream.class));
        }

        @Override
        public void teardown() {
            // Do nothing.
        }
    }

这个静态内部类就是用来生成StreamStringLoader()对象的。那就刚好也看一下ModelLoaderFactory接口。

ModelLoaderFactory接口

public interface ModelLoaderFactory {
    ModelLoader build(Context context, GenericLoaderFactory factories);
    void teardown();
}

可以看到这个接口的build()方法就是专门用来生成ModelLoader对象的。

Glide的register()方法

public  void register(Class modelClass, Class resourceClass, ModelLoaderFactory factory) {
        ModelLoaderFactory removed = loaderFactory.register(modelClass, resourceClass, factory);
        if (removed != null) {
            removed.teardown();
        }
    }

在register()方法中GenericLoaderFactory的register()方法,看一下这个方法。

GenericLoaderFactory里面的register()方法

//缓存各种ModelLoaderFactory
private final Map> modelClassToResourceFactories =
            new HashMap>();
//缓存各种ModelLoader
private final Map> cachedModelLoaders =
            new HashMap>();
public synchronized  ModelLoaderFactory register(Class modelClass, Class resourceClass,
            ModelLoaderFactory factory) {
        cachedModelLoaders.clear();
        //根据modelClass来判断是否已经存在与modelClass对应的ModelLoaderFactory的Map
        Map resourceToFactories = modelClassToResourceFactories.get(modelClass);
        if (resourceToFactories == null) {
            resourceToFactories = new HashMap();
            //以modelClass为key,将新建的Map集合放入modelClassToResourceFactories中
            modelClassToResourceFactories.put(modelClass, resourceToFactories);
        }
         //以resourceClass为key,将传入的factory放入与modelClass对应的Map中
        ModelLoaderFactory/*T, Y*/ previous = resourceToFactories.put(resourceClass, factory);

        if (previous != null) {
            for (Map factories : modelClassToResourceFactories.values()) {
                if (factories.containsValue(previous)) {
                    previous = null;
                    break;
                }
            }
        }

        return previous;
    }

GenericLoaderFactory里面的register()方法就是将Glide的register()方法中所传来的factory存入了GenericLoaderFactory类里面的modelClassToResourceFactories中。

创建Glide对象及Glide对象的创建过程中,在Glide的构造函数中都发生了什么,这个过程已经结束了。回到Glide.get(context).getLoaderFactory().buildModelLoader(modelClass, resourceClass);这行代码。

Glide的getLoaderFactory()方法

private GenericLoaderFactory getLoaderFactory() {
        return loaderFactory;
    }

这个loaderFactory就是在Glide初始化时初始的GenericLoaderFactory对象。接着调用了GenericLoaderFactory类的buildModelLoader(modelClass, resourceClass)方法。

GenericLoaderFactory类的buildModelLoader(modelClass, resourceClass)方法

public synchronized  ModelLoader buildModelLoader(Class modelClass, Class resourceClass) {
       // 用getCachedLoader()这个方法得到之前缓存的ModelLoader
        ModelLoader result = getCachedLoader(modelClass, resourceClass);
        if (result != null) {
            if (NULL_MODEL_LOADER.equals(result)) {
                return null;
            } else {
                return result;
            }
        }
        //如果得到的ModelLoader为null,用getFactory()方法来得到之前缓存的factory
        final ModelLoaderFactory factory = getFactory(modelClass, resourceClass);
        if (factory != null) {
            //factory.build()用来生成相对应的ModleLoader
            result = factory.build(context, this);
            //缓存生成的ModleLoader
            result = factory.build(context, this);
            cacheModelLoader(modelClass, resourceClass, result);
        } else {
            cacheNullLoader(modelClass, resourceClass);
        }
        return result;
    }
private  ModelLoader getCachedLoader(Class modelClass, Class resourceClass) {
        //根据modelClass的到对应的Map
        Map resourceToLoaders = cachedModelLoaders.get(modelClass);
        ModelLoader/*T, Y*/ result = null;
        if (resourceToLoaders != null) {
           //根据resourceClass得到对应的ModelLoader
            result = resourceToLoaders.get(resourceClass);
        }
        return result;
    }
private  ModelLoaderFactory getFactory(Class modelClass, Class resourceClass) {
       //利用modelClass得到相对于的Map
        Map resourceToFactories = modelClassToResourceFactories.get(modelClass);
        ModelLoaderFactory/*T, Y*/ result = null;
        if (resourceToFactories != null) {
           //利用resourceClass得到之前缓存的Factory
            result = resourceToFactories.get(resourceClass);
        }

        if (result == null) {
            for (Class registeredModelClass : modelClassToResourceFactories.keySet()) {
                if (registeredModelClass.isAssignableFrom(modelClass)) {
                    Map currentResourceToFactories =
                            modelClassToResourceFactories.get(registeredModelClass);
                    if (currentResourceToFactories != null) {
                        result = currentResourceToFactories.get(resourceClass);
                        if (result != null) {
                            break;
                        }
                    }
                }
            }
        }

        return result;
    }

这个过程分析结束了,实际上就是利用GenericLoaderFactory类得到相应的ModelLoader的过程,在这个过程中,我们传入的modelClass类型为String.Class,传入的resourceClass类型为InputStream类型,最终得到了StreamStringLoader类型的对象。
Glide.get(context).getLoaderFactory().buildModelLoader(modelClass, resourceClass);这行代码分析结束了,所以最后Glide.buildStreamModelLoader(modelClass, context)这句代码最后的到了StreamStringLoader类型的对象。回到loadGeneric()方法,再贴一下。

private  DrawableTypeRequest loadGeneric(Class modelClass) {
        ModelLoader streamModelLoader = Glide.buildStreamModelLoader(modelClass, context);
        ModelLoader fileDescriptorModelLoader =
                Glide.buildFileDescriptorModelLoader(modelClass, context);
        if (modelClass != null && streamModelLoader == null && fileDescriptorModelLoader == null) {
            throw new IllegalArgumentException("Unknown type " + modelClass + ". You must provide a Model of a type for"
                    + " which there is a registered ModelLoader, if you are using a custom model, you must first call"
                    + " Glide#register with a ModelLoaderFactory for your custom model class");
        }

        return optionsApplier.apply(
                new DrawableTypeRequest(modelClass, streamModelLoader, fileDescriptorModelLoader, context,
                        glide, requestTracker, lifecycle, optionsApplier));
    }
ModelLoader fileDescriptorModelLoader =
                Glide.buildFileDescriptorModelLoader(modelClass, context);

这个上面贴的过程和上面分析的得到StreamStringLoader类型的对象的过程类似,所以这个过程最后得到的是FileDescriptorUriLoader类型的对象。
回到loadGeneric()方法,在这个方法的最后生成了一个DrawableTypeRequest对象。并将刚刚生成的StreamStringLoader类型的对象和FileDescriptorUriLoader类型的对象还有requestTracker, lifecycle,等一起传入其构造器中。
进入这个DrawableTypeRequest类看看。

DrawableTypeRequest类的构造函数

public class DrawableTypeRequest extends DrawableRequestBuilder implements DownloadOptions {
private final ModelLoader streamModelLoader;
    private final ModelLoader fileDescriptorModelLoader;
    private final RequestManager.OptionsApplier optionsApplier;
   private static  FixedLoadProvider buildProvider(Glide glide,
            ModelLoader streamModelLoader,
            ModelLoader fileDescriptorModelLoader, Class resourceClass,
            Class transcodedClass,
            ResourceTranscoder transcoder) {
        if (streamModelLoader == null && fileDescriptorModelLoader == null) {
            return null;
        }
        //1.得到GifBitmapWrapperDrawableTranscoder类型对象
        if (transcoder == null) {
            transcoder = glide.buildTranscoder(resourceClass, transcodedClass);
        }
        //2.得到ImageVideoGifDrawableLoadProvider类型对象
        DataLoadProvider dataLoadProvider = glide.buildDataProvider(ImageVideoWrapper.class,
                resourceClass);
        //3.传入StreamStringLoader类型的对象和FileDescriptorUriLoader类型的对象来生成ImageVideoModelLoader对象。
        ImageVideoModelLoader modelLoader = new ImageVideoModelLoader(streamModelLoader,
                fileDescriptorModelLoader);
        //在这个buildProvider()方法中,将ImageVideoModelLoader类型的ModelLoader,GlideBitmapDrawableTranscoder类型的ResourceTranscoder,ImageVideoGifDrawableLoadProvider类型的DataLoadProvider传入FixedLoadProvider的构造器中
        return new FixedLoadProvider(modelLoader, transcoder, dataLoadProvider);
    }
   //在DrawableTypeRequest的构造函数中,初始化从父类继承来的实例
    DrawableTypeRequest(Class modelClass, ModelLoader streamModelLoader,
            ModelLoader fileDescriptorModelLoader, Context context, Glide glide,
            RequestTracker requestTracker, Lifecycle lifecycle, RequestManager.OptionsApplier optionsApplier) {
        super(context, modelClass,
                buildProvider(glide, streamModelLoader, fileDescriptorModelLoader, GifBitmapWrapper.class,
                        GlideDrawable.class, null),
                glide, requestTracker, lifecycle);
      this.streamModelLoader = streamModelLoader;
        this.fileDescriptorModelLoader = fileDescriptorModelLoader;
        this.optionsApplier = optionsApplier;
    }
}

先来分析一下代码中标记的1,2过程。
1过程transcoder = glide.buildTranscoder(resourceClass, transcodedClass);调用了Glide的buildTranscoder()方法。

Glide的buildTranscoder()方法

private final TranscoderRegistry transcoderRegistry = new TranscoderRegistry();
Glide(Engine engine, MemoryCache memoryCache, BitmapPool bitmapPool, Context context, DecodeFormat decodeFormat) {
       ......
       //注册GlideBitmapDrawableTranscoder到transcoderRegistry
       transcoderRegistry.register(GifBitmapWrapper.class, GlideDrawable.class,
                new GifBitmapWrapperDrawableTranscoder(
                        new GlideBitmapDrawableTranscoder(context.getResources(), bitmapPool)));
     ......
    }
 ResourceTranscoder buildTranscoder(Class decodedClass, Class transcodedClass) {
        return transcoderRegistry.get(decodedClass, transcodedClass);
    }

Glide的buildTranscoder()方法所用到的方法如上,可以看到是通过transcoderRegistry.get(decodedClass, transcodedClass);来得到ResourceTranscoder对象的。

  • ResourceTranscoder接口:它是用于对图片进行转码的

TranscoderRegistry类中

public class TranscoderRegistry {
    private static final MultiClassKey GET_KEY = new MultiClassKey();
    //这个Map以MultiClassKey为key,ResourceTranscoder对象为value.
    private final Map> factories =
            new HashMap>();
//之前在Glide的构造方法中已经注册过了。用这个方法得到之前注册的
public  ResourceTranscoder get(Class decodedClass, Class transcodedClass) {
        if (decodedClass.equals(transcodedClass)) {
            return (ResourceTranscoder) UnitTranscoder.get();
        }
        final ResourceTranscoder result;
        synchronized (GET_KEY) {
            GET_KEY.set(decodedClass, transcodedClass);
            //利用MultiClassKey从Map中得到注册的GlideBitmapDrawableTranscoder
            result = factories.get(GET_KEY);
        }
        if (result == null) {
            throw new IllegalArgumentException("No transcoder registered for " + decodedClass + " and "
                    + transcodedClass);
        }
        return (ResourceTranscoder) result;
    }
}

所以总结1过程,传入的resourceClass, transcodedClass分别为GifBitmapWrapper, GlideDrawable类型。最后得到的是GifBitmapWrapperDrawableTranscoder对象。GlideBitmapDrawableTranscoder继承自ResourceTranscoder接口,该接口用于图片的转码。

2过程类似1过程,最后得到的对象类型为ImageVideoGifDrawableLoadProvider,ImageVideoGifDrawableLoadProvider类继承DataLoadProvider接口。

  • DataLoadProvider接口:用于对图片进行编解码的。

再来看一下标记3
ImageVideoModelLoader
modelLoader = new ImageVideoModelLoader(streamModelLoader,
fileDescriptorModelLoader);

ImageVideoModelLoader类

public class ImageVideoModelLoader implements ModelLoader {
    private final ModelLoader streamLoader;
    private final ModelLoader fileDescriptorLoader;

    public ImageVideoModelLoader(ModelLoader streamLoader,
            ModelLoader fileDescriptorLoader) {
        if (streamLoader == null && fileDescriptorLoader == null) {
            throw new NullPointerException("At least one of streamLoader and fileDescriptorLoader must be non null");
        }
        //传进来的StreamStringLoader类型的对象
        this.streamLoader = streamLoader;
        //传入的FileDescriptorUriLoader类型的对象
        this.fileDescriptorLoader = fileDescriptorLoader;
    }
@Override
    public DataFetcher getResourceFetcher(A model, int width, int height) {
        DataFetcher streamFetcher = null;
        if (streamLoader != null) {
            //1.
            streamFetcher = streamLoader.getResourceFetcher(model, width, height);
        }
        DataFetcher fileDescriptorFetcher = null;
        if (fileDescriptorLoader != null) {
            //2.
            fileDescriptorFetcher = fileDescriptorLoader.getResourceFetcher(model, width, height);
        }

        if (streamFetcher != null || fileDescriptorFetcher != null) {
            //返回的ImageVideoFetcher对象是ImageVideoModelLoader类的内部类
            return new ImageVideoFetcher(streamFetcher, fileDescriptorFetcher);
        } else {
            return null;
        }
    }

    static class ImageVideoFetcher implements DataFetcher {
        private final DataFetcher streamFetcher;
        private final DataFetcher fileDescriptorFetcher;

        public ImageVideoFetcher(DataFetcher streamFetcher,
                DataFetcher fileDescriptorFetcher) {
            this.streamFetcher = streamFetcher;
            this.fileDescriptorFetcher = fileDescriptorFetcher;
        }

        @SuppressWarnings("resource")
        // @see ModelLoader.loadData
        @Override
        public ImageVideoWrapper loadData(Priority priority) throws Exception {
            InputStream is = null;
            if (streamFetcher != null) {
                try {
                    is = streamFetcher.loadData(priority);
                } catch (Exception e) {
                    if (Log.isLoggable(TAG, Log.VERBOSE)) {
                        Log.v(TAG, "Exception fetching input stream, trying ParcelFileDescriptor", e);
                    }
                    if (fileDescriptorFetcher == null) {
                        throw e;
                    }
                }
            }
            ParcelFileDescriptor fileDescriptor = null;
            if (fileDescriptorFetcher != null) {
                try {
                    fileDescriptor = fileDescriptorFetcher.loadData(priority);
                } catch (Exception e) {
                    if (Log.isLoggable(TAG, Log.VERBOSE)) {
                        Log.v(TAG, "Exception fetching ParcelFileDescriptor", e);
                    }
                    if (is == null) {
                        throw e;
                    }
                }
            }
            return new ImageVideoWrapper(is, fileDescriptor);
        }
    }
}

将传入DrawableTypeRequest构造器的StreamStringLoader类型的对象和FileDescriptorUriLoader类型的对象传入ImageVideoModelLoader的构造器中来生成ImageVideoModelLoader对象。这个ImageVideoModelLoader类继承自ModelLoader接口,在该类的getResourceFetcher()方法中,来分析一下12过程。
1过程

streamFetcher = streamLoader.getResourceFetcher(model, width, height);

streamLoader的类型是StreamStringLoader类,看一下StreamStringLoader类的getResourceFetcher()方法。发现这个类里面没有getResourceFetcher()方法,但是它继承于StringLoader类,发现getResourceFetcher()方法就在StringLoader类中。

StringLoader类中的getResourceFetcher()方法

private final ModelLoader uriLoader;
public DataFetcher getResourceFetcher(String model, int width, int height) {
        Uri uri;
        if (TextUtils.isEmpty(model)) {
            return null;
        } else if (model.startsWith("/")) {
            uri = toFileUri(model);
        } else {
            uri = Uri.parse(model);
            final String scheme = uri.getScheme();
            if (scheme == null) {
                uri = toFileUri(model);
            }
        }

        return uriLoader.getResourceFetcher(uri, width, height);
    }

在这个StringLoader类中的getResourceFetcher()方法的最后返回了uriLoader的getResourceFetcher()方法。这个uriLoader的类型是HttpUrlGlideUrlLoader。那进入HttpUrlGlideUrlLoader的getResourceFetcher()方法。

HttpUrlGlideUrlLoader的getResourceFetcher()方法

public DataFetcher getResourceFetcher(GlideUrl model, int width, int height) {
        // GlideUrls memoize parsed URLs so caching them saves a few object instantiations and time spent parsing urls.
        GlideUrl url = model;
        if (modelCache != null) {
            url = modelCache.get(model, 0, 0);
            if (url == null) {
                modelCache.put(model, 0, 0, model);
                url = model;
            }
        }
        return new HttpUrlFetcher(url);
    }

看到在这个HttpUrlGlideUrlLoader的getResourceFetcher()方法的最后返回了一个HttpUrlFetcher()对象。StringLoader类中的getResourceFetcher()方法中最后返回的是一个HttpUrlFetcher()对象,即StreamStringLoader类的getResourceFetcher()方法最后得到了一个HttpUrlFetcher()对象。所以1过程最后得到了一个HttpUrlFetcher()对象。
2过程类似于1过程。
回到ImageVideoModelLoader类的getResourceFetcher方法中,最后将得到的HttpUrlFetcher()对象传入ImageVideoFetcher()的构造器中,这个ImageVideoFetche类是ImageVideoModelLoader类的内部类。

总结一下,从开始到现在的3个很重要的接口

  • ModelLoader接口:是用于加载图片的
  • ResourceTranscoder接口:它是用于对图片进行转码的
  • DataLoadProvider接口:用于对图片进行编解码的。

这里还要补充一下,DrawableTypeRequest类里面的asBitmap()方法和asGif()方法。

DrawableTypeRequest类里面的asBitmap()方法

public BitmapTypeRequest asBitmap() {
        return optionsApplier.apply(new BitmapTypeRequest(this, streamModelLoader,
                fileDescriptorModelLoader, optionsApplier));
    }

asBitmap()方法里面生成了BitmapTypeRequest对象,它类似于DrawableTypeRequest,这个BitmapTypeRequest的最终的父类也是GenericRequestBuilder。
asGif()方法asBitmap()方法类似。

回到DrawableTypeRequest类的构造函数中。
DrawableTypeRequest类的构造函数中又初始化了从父类那里继承得到的实例对象,我们看一下DrawableTypeRequest类的父类。

DrawableRequestBuilder类

public class DrawableRequestBuilder
        extends GenericRequestBuilder
        implements BitmapOptions, DrawableOptions {

    DrawableRequestBuilder(Context context, Class modelClass,
            LoadProvider loadProvider, Glide glide,
            RequestTracker requestTracker, Lifecycle lifecycle) {
        super(context, modelClass, loadProvider, GlideDrawable.class, glide, requestTracker, lifecycle);
        // Default to animating.
        ......
    }
}

看到这个类同样初始化了从父类那里继承得到的实例对象,我们看一下DrawableRequestBuilder类的父类。

GenericRequestBuilder类

public class GenericRequestBuilder implements Cloneable {
    private ChildLoadProvider loadProvider;

    GenericRequestBuilder(LoadProvider loadProvider,
            Class transcodeClass, GenericRequestBuilder other) {
        this(other.context, other.modelClass, loadProvider, transcodeClass, other.glide, other.requestTracker,
                other.lifecycle);
        ......
    }

    GenericRequestBuilder(Context context, Class modelClass,
            LoadProvider loadProvider,
            Class transcodeClass, Glide glide, RequestTracker requestTracker, Lifecycle lifecycle) {
        this.loadProvider = loadProvider != null
                ? new ChildLoadProvider(loadProvider) : null;
        }
    }
}

在这个类中,将DrawableTypeRequest类中生成的FixedLoadProvider类的对象在构造函数中传入ChildLoadProvider()的构造器中,所以最后在该类中的loadProvider实例变量所指向的类型为ChildLoadProvider类型,ChildLoadProvider也是DataLoadProvider接口的实现类。

  • GenericRequestBuilder类:这个类提供了加载各种资源的方法,是各种资源请求构造类的父类。

好了,从在RequestManager类的loadGeneric()方法中,创建DrawableTypeRequest对象,又分析了很多。
现在回到RequestManager类的load(string)方法中。

load(string)方法中

public DrawableTypeRequest load(String string) {
        return (DrawableTypeRequest) fromString().load(string);
    }

在load(string)方法中,fromString()方法的过程已经分析过了,发现这个方法最终返回的是DrawableTypeRequest类型的对象,所以fromString()方法后面调用的.load()方法,就是DrawableTypeRequest类里面的方法,但是发现DrawableTypeRequest类里面根本没有load()方法,所以这个方法是其父类的方法。

DrawableRequestBuilder类中

public DrawableRequestBuilder load(ModelType model) {
        super.load(model);
        return this;
    }

调用了其父类的load()方法。那就到GenericRequestBuilder类中看看。

GenericRequestBuilder类中

 public GenericRequestBuilder load(ModelType model) {
        this.model = model;
        isModelSet = true;
        return this;
    }

这个model就是load()方法中传入的String类型的字符串。
Glide的load()过程结束了。这个分析过程中分析了很多其他的东西,但是分析的东西都是后面我们into()过程中要使用的,所以写了很多。好滴,load()过程最终调用了GenericRequestBuilder类的load()方法。

参考

Android图片加载框架最全解析(二),从源码的角度理解Glide的执行流程

你可能感兴趣的:(Glide的load()过程源码分析)