Picasso源码完全解析(三)--Request和Action的创建

16 篇文章 0 订阅
10 篇文章 0 订阅


Picasso源码完全解析(一)--概述

Picasso源码完全解析(二)--Picasso实例的创建

Picasso源码完全解析(三)--Request和Action的创建

Picasso源码完全解析(四)--Action分发和执行

Picasso源码完全解析(五)--图片的获取(BitmapHunter)

Picasso源码完全解析(六)--请求的取消、暂停、和恢复

Picasso源码完全解析(七)-- CleanupThread 取消请求


Picasso源码完全解析(三)--Request和Action的创建

使用Picasso可以加载链接、文件、Uri以及引用自带的res资源,

public RequestCreator load(@Nullable Uri uri) {
    return new RequestCreator(this, uri, 0);
}

public RequestCreator load(@Nullable String path) {
    if (path == null) {
        return new RequestCreator(this, null, 0);
    }
    if (path.trim().length() == 0) {
        throw new IllegalArgumentException("Path must not be empty.");
    }
    return load(Uri.parse(path));
}


public RequestCreator load(@NonNull File file) {
    if (file == null) {
        return new RequestCreator(this, null, 0);
    }
    return load(Uri.fromFile(file));
}


public RequestCreator load(@DrawableRes int resourceId) {
    if (resourceId == 0) {
        throw new IllegalArgumentException("Resource ID must not be zero.");
    }
    return new RequestCreator(this, null, resourceId);
}

可以看出最终是创建了一个RequestCreator对象

 private static final AtomicInteger nextId = new AtomicInteger();

private final Picasso picasso;
private final Request.Builder data;

private boolean noFade;
private boolean deferred;
private boolean setPlaceholder = true;
private int placeholderResId;
private int errorResId;
private int memoryPolicy;
private int networkPolicy;
private Drawable placeholderDrawable;
private Drawable errorDrawable;
private Object tag;

这个类有13个属性,其方法主要分为两部分,一类是设置属性,一类是发起请求的方法,主要有 get()、fetch()、into()三种方式。

其中 Request.Builder data 是采用建造者模式来创建请求的。我们

    //fetch()方式
   Action action =
                new FetchAction(picasso, request, memoryPolicy, networkPolicy, tag, key, callback);
   picasso.submit(action);

//into()方式
 Action action =
            new ImageViewAction(picasso, target, request, memoryPolicy, networkPolicy, errorResId,
                    errorDrawable, requestKey, tag, callback, noFade);

    picasso.enqueueAndSubmit(action);

可以看到fetch()和into方式最终都是通过Picasso.submit()或者picasso.enqueueAndSubmit()两种方式提交的。到此,每个请求生成一个对应的Action,被提交给Picasso执行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值