Glide源码解读(二)

本文深入解析Glide的源码,从buildRequest开始,逐步剖析into方法、RequestTracker的运行机制、getSize过程,直至Engine的load方法、HttpUrlFetcher的网络请求和Bitmap的解码过程,最后讲解如何将加载的资源设置到ImageView上,全面理解Glide的图片加载流程。
摘要由CSDN通过智能技术生成

前面文章分析到buildRequest方法,现在继续看

buildRequest方法,调用了buildRequestRecursive方法

  private Request buildRequest(Target<TranscodeType> target) {
    if (priority == null) {
        priority = Priority.NORMAL;
    }
    isThumbnailBuilt = false;
    return buildRequestRecursive(target, null);
}

buildRequestRecursive方法,前面两个if判断都是判断是否设置了缩略图需要,调用了thumbnai两个方法走两个if,

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);
        isThumbnailBuilt = true;
        // Recursively generate thumbnail requests.
        Request thumbRequest = thumbnailRequestBuilder.buildRequestRecursive(target, coordinator);
        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方法,该方法直接调用GenericRequest类的obtain方法

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,
            requestListener,
            requestCoordinator,
            glide.getEngine(),
            transformation,
            transcodeClass,
            isCacheable,
            animationFactory,
            overrideWidth,
            overrideHeight,
            diskCacheStrategy);
}

GenericRequest 的 obtain方法,该方法从REQUEST_POOL队列里面拿请求,如果没有则生成

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.init(...);
    return request;
}

调用了GenericRequest的init方法,该方法就是直接把参数赋值到对象里面

继续看into方法。构建request后, 把request丢进了requestTracker执行

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())");
    }
	//获取target里面的request。清楚和回收
    Request previous = target.getRequest();

    if (previous != null) {
        previous.clear();
        requestTracker.removeRequest(previous);
        previous.recycle();
    }
	//构建requeset
    Request request = buildRequest(target);
    target.setRequest(request);
    lifecycle.addListener(target);
    requestTracker.runRequest(request);

    return target;
}

RequestTracker 的runRequest方法,把request放入requests集合,如果已经是暂停状态了,则放入pendingRequests集合,否则执行begin方法。至于什么时候是暂停状态,看什么时候调用了pauseRequests方法。

 public void runRequest(Request request) {
    requests.add(request);
    if (!isPaused) {
		//调用request的begin方法
      request.begin();
    } else {
      pendingRequests.add(request);
    }
  }

Request的begin方法。如果前面调用了thumbnai方法,则会走onSizeReady,否则走else语句,我们没设置,看getSize方法

public void begin() {
    startTime = LogTime.getLogTime();
    if (model == null) {
        onException(null);
        return;
    }
	//设置状态
    stat
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值