使用 Picasso 获取带位图的回调 [英] Use Picasso to get a callback with a Bitmap
问题描述
我正在使用 Picasso 为我的应用下载图像.
I'm using Picasso to download images for my app.
我需要先访问 Bitmap
,然后再将其加载到 ImageView
.Downloader.Response
类的存在似乎表明这是可能的,但我找不到任何使用示例.如果可以使用 Picasso,我不想编写更多代码来异步处理这个特殊情况.
I'm in a situation where I need to access the Bitmap
first before it's loaded into the ImageView
. The presence of the Downloader.Response
class seems to suggest this is possible, but I can't find any use examples. I don't want to write a bunch more code to asynchronously handle this one particular case if it's possible to do with Picasso.
谁能告诉我怎么做?
推荐答案
在 github 上找到答案,以防有人疑惑:
Found the answer on github in case anyone is wondering:
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();
}
该帖子建议不要使用匿名回调,而是为目标使用实例变量.
The post recommends not using an anonymous callback, and instead using an instance variable for target.
这篇关于使用 Picasso 获取带位图的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!