重学Android——Glide4.x源码分析(2)

Glide的加载流程

接上文重学Android——Glide4.x源码分析(1)

执行加载主流程

接上一文,昨天讲到图片加载,最终调用到了onSizeReady的方法,调用了其中的engine.load方法

  @Override
  public synchronized void onSizeReady(int width, int height) {
   
    stateVerifier.throwIfRecycled();
    ...
    status = Status.RUNNING;

      //计算缩略图的尺寸
    float sizeMultiplier = requestOptions.getSizeMultiplier();
    this.width = maybeApplySizeMultiplier(width, sizeMultiplier);
    this.height = maybeApplySizeMultiplier(height, sizeMultiplier);

    if (IS_VERBOSE_LOGGABLE) {
   
      logV("finished setup for calling load in " + LogTime.getElapsedMillis(startTime));
    }
      //加载流程
    loadStatus =
        engine.load(
            glideContext,
            model,//对应rul,图片地址
            requestOptions.getSignature(),
            this.width,
            this.height,
            requestOptions.getResourceClass(),//默认是Object.class
            transcodeClass,//默认是Drawable.class
            priority,
            requestOptions.getDiskCacheStrategy(),//缓存策略,默认是AUTOMATIC
            requestOptions.getTransformations(),
            requestOptions.isTransformationRequired(),
            requestOptions.isScaleOnlyOrNoTransform(),
            requestOptions.getOptions(),
            requestOptions.isMemoryCacheable(),
            requestOptions.getUseUnlimitedSourceGeneratorsPool(),
            requestOptions.getUseAnimationPool(),
            requestOptions.getOnlyRetrieveFromCache(),
            this,
            callbackExecutor);
  }

我们再直接跟进Engin.load方法

    public synchronized <R> LoadStatus load(
        GlideContext glideContext,
        Object model,
        Key signature,
        int width,
        int height,
        Class<?> resourceClass,
        Class<R> transcodeClass,
        Priority priority,
        DiskCacheStrategy diskCacheStrategy,
        Map<Class<?>, Transformation<?>> transformations,
        boolean isTransformationRequired,
        boolean isScaleOnlyOrNoTransform,
        Options options,
        boolean isMemoryCacheable,
        boolean useUnlimitedSourceExecutorPool,
        boolean useAnimationPool,
        boolean onlyRetrieveFromCache,
        ResourceCallback cb,
        Executor callbackExecutor) {
   
        long startTime = VERBOSE_IS_LOGGABLE ? LogTime.getLogTime() : 0;

        //1创建资源索引key,内存缓存的唯一键值
        EngineKey key = keyFactory.buildKey(model, signature, width, height, transformations, resourceClass, transcodeClass, options);

        //2首先从内存中找当前正在显示的资源缓存
        EngineResource<?> active = loadFromActiveResources(key, isMemoryCacheable);
        if (active != null) {
   
            cb.onResourceReady(active, DataSource.MEMORY_CACHE);
            if (VERBOSE_IS_LOGGABLE) {
   
                logWithTimeAndKey("Loaded resource from active resources", startTime, key);
            }
            return null;
        }

        //3没有找到,就从内存缓存资源中加载图片
        EngineResource<?> cached = loadFromCache(key, isMemoryCacheable);
        if (cached != null) {
   
            cb.onResourceReady(cached, DataSource.MEMORY_CACHE);
            if (VERBOSE_IS_LOGGABLE) {
   
                logWithTimeAndKey("Loaded resource from cache", startTime, key);
            }
            return null;
        }

        //4还是没找到,如果这个任务已经在队列中,获取已经存在的加载任务
        EngineJob<?> current = jobs.get(key, onlyRetrieveFromCache);
        if (current != null) {
   
            current.addCallback(cb, callbackExecutor);
            if (VERBOSE_IS_LOGGABLE) {
   
                logWithTimeAndKey("Added to existing load", startTime, key);
            }
            return new LoadStatus(cb, current);
        }

        //5还没有找到,如果是新建加载任务,创建EngineJob和DecodeJob,然后开始任务
        EngineJob<R> engineJob =
            engineJobFactory.build(
            key,
            isMemoryCacheable,
            useUnlimitedSourceExecutorPool,
            useAnimationPool,
            onlyRetrieveFromCache);

        //6新建解码任务,真正执行数据加载和解码的类
        DecodeJob<R> decodeJob =
            decodeJobFactory.build(
            glideContext,
            model,
            key,
            signature,
            width,
            height,
            resourceClass,
       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值