4. 修改图片的尺寸,填充图片进ImageView
public void into(ImageView target, Callback callback) {
long started = System.nanoTime();
//线程检查
checkMain();
if (target == null) {
throw new IllegalArgumentException("Target must not be null.");
}
//没设置url以及resId则取消请求
if (!data.hasImage()) {
picasso.cancelRequest(target);
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
return;
}
//仅有fit()方法会修改该flag为true,但是该方法只能由开发者显式调用,因此下面的代码默认是不会执行的
if (deferred) {//当尝试调整图片的大小让其正好适合ImageView时defered为true(fit()方法)
if (data.hasSize()) {
//判断ImageView大小,即如果ImageView已经渲染完成,则无法改变大小
throw new IllegalStateException("Fit cannot be used with resize.");
}
int width = target.getWidth();
int height = target.getHeight();
if (width == 0 || height == 0) {
//设置默认图
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
//TODO:
picasso.defer(target, new DeferredRequestCreator(this, target, callback));
return;
}
data.resize(width, height);
}
//Request的拦截器:
//简单介绍下。他会对原始的Request做一个拦截,看是否需要做处理。
//比如说使用CDN的时候修改主机名以达到更快的访问速度等操作。
Request request = createRequest(started);
//根据请求的URL,图片的处理等参数创建一个字符串Key
String requestKey = createKey(request);
//缓存策略,是否应该从内存中读取
if (shouldReadFromMemoryCache(memoryPolicy)) {
//内存的快速检查,
Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);
if (bitmap != null) {
//如果缓存中已存在则取消请求并直接设置给ImageView
picasso.cancelRequest(target);
setBitmap(target, picasso.context, bitmap, MEMORY, noFade, picasso.indicatorsEnabled);
...//日志
if (callback != null) {
callback.onSuccess();
}
return;
}
}
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
//这里创建的是ImageViewAction对象,后面会用到
Action action = new ImageViewAction(picasso, target, request,
memoryPolicy, networkPolicy, errorResId,errorDrawable, requestKey, tag, callback, noFade);
//提交请求,Picasso内部维护了一个map,key是imageView,value是Action
//提交时做判断,如果当前imageView已经在任务队列里了。判断当前任务与之前的任务是否相同,
//如果不相同则取消之前的任务并将新的key-value加入到map
picasso.enqueueAndSubmit(action);
} |