Picasso是异步的,所以我想知道在执行任何其他代码之前是否有任何方法可以测试图像是否已完全加载?
Picasso.with(context).load(imageURI).into(ImageView);
// image fully loaded? do something else ..
答案 0 :(得分:11)
如果图像已满载,则会同步设置ImageView
。
您可以使用回调来断言。
final AtomicBoolean loaded = new AtomicBoolean();
Picasso.with(context).load(imageURI).into(imageView, new Callback.EmptyCallback() {
@Override public void onSuccess() {
loaded.set(true);
}
});
if (loaded.get()) {
// The image was immediately available.
}
答案 1 :(得分:7)
使用重载方法.into(ImageView target, Callback callback)
适合您的情况。您可以使用基本实现或扩展自己的类似
基:
Picasso.with(context).load(url).into(target, new Callback(){
@Override
public void onSuccess() {
}
@Override
public void onError() {
}
});
扩展版本:
package main.java.app.picasso.test;
/**
* Created by nikola on 9/9/14.
*/
public abstract class TargetCallback implements Callback {
private ImageView mTarget;
public abstract void onSuccess(ImageView target);
public abstract void onError(ImageView target);
public TargetCallback(ImageView imageView){
mTarget = imageView;
}
@Override
public void onSuccess() {
onSuccess(mTarget);
}
@Override
public void onError() {
onError(mTarget);
}
}
用法:
Picasso.with(context).load(url).into(target, new TargetCallback(target) {
@Override
public void onSuccess(ImageView target) {
}
@Override
public void onError(ImageView target) {
}
});