使用毕加索获得一个位图的callback

我正在使用毕加索下载我的应用程序的图像。

我处于需要在加载到ImageView之前首先访问Bitmap的情况。 Downloader.Response类的存在似乎表明这是可能的,但我找不到任何使用示例。 我不想写更多的代码来asynchronous处理这个特殊的情况,如果可能的话,毕加索。

任何人都可以告诉我如何做到这一点?

在github上find答案,以防有人在想:

 private Target target = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { } @Override public void onBitmapFailed(Drawable errorDrawable) { } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } } private void someMethod() { Picasso.with(this).load("url").into(target); } @Override public void onDestroy() { // could be in onPause or onStop Picasso.with(this).cancelRequest(target); super.onDestroy(); } 

该文章build议不要使用匿名callback,而是使用实例variables作为目标。

从这里采取:

 Picasso.with(this) .load(url) .into(new Target() { @Override public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){ /* Save the bitmap or do something with it here */ //Set it in the ImageView theView.setImageBitmap(bitmap); } }); 

更新(2016年5月4日):

  Picasso.with(this) .load(youUrl) .into(new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { } @Override public void onBitmapFailed(Drawable errorDrawable) { } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } }); 

更新(2016年11月22日)

或者使用Target的强大参考,这样它就不会被垃圾收集

 Target target = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { } @Override public void onBitmapFailed(Drawable errorDrawable) { } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } }; void foo() { Picasso.with(getContext()).load(getUrl()).into(target); } 

科特林

 object: com.squareup.picasso.Target { override fun onBitmapFailed(errorDrawable: Drawable?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun onPrepareLoad(placeHolderDrawable: Drawable?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) { } } 

我想也许你们中的一些人会喜欢上面的答案的RxJava版本…这里是:

  public static Observable<Bitmap> loadBitmap(Picasso picasso, String imageUrl) { return Observable.create(new Observable.OnSubscribe<Bitmap>() { @Override public void call(Subscriber<? super Bitmap> subscriber) { Target target = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { subscriber.onNext(bitmap); subscriber.onCompleted(); } @Override public void onBitmapFailed(Drawable errorDrawable) { subscriber.onError(new Exception("failed to load " + imageUrl)); } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } }; subscriber.add(new Subscription() { private boolean unSubscribed; @Override public void unsubscribe() { picasso.cancelRequest(target); unSubscribed = true; } @Override public boolean isUnsubscribed() { return unSubscribed; } }); picasso.load(imageUrl).into(target); } }); } 

PS在订阅时,将订阅参考存储在您的活动中,否则,在收到回复之前,目标将被GC'd …

与使用目标垃圾收集器用完你的plans.so我做这个示例代码的技巧:

 ImageView bitmapImageView = new ImageView(context); picasso.with(context).load(url).into(bitmapImageView); Bitmap bitmap = ((BitmapDrawable)bitmapImageView.getDrawable()).getBitmap(); 

现在你可以享受你的位图; :))