Android 如何在不使用 ImageView 的情况下使用 Picasso 加载位图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24302431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load a Bitmap with Picasso without using an ImageView?
提问by EES
With ImageView
, I can use the following code to download image with callback
使用ImageView
,我可以使用以下代码通过回调下载图像
Picasso.with(activity).load(url).into(imageView, new Callback()
{
@Override
public void onSuccess()
{
// do something
}
@Override
public void onError() { }
);
Or simply get the Bitmap from this Picasso.with(activity).load(url).get();
. Is there anyway to add callback for just download the image? If possible please provide sample code, Cheers!
或者简单地从中获取位图Picasso.with(activity).load(url).get();
。无论如何要添加回调以仅下载图像?如果可能,请提供示例代码,干杯!
回答by Philipp Jahoda
You can create aTarget
and then modify the Bitmap
inside the Targets callback method onBitmapLoaded(...)
. Here is how:
你可以创建一个Target
然后修改Bitmap
里面的Targets回调方法onBitmapLoaded(...)
。方法如下:
// make sure to set Target as strong reference
private Target loadtarget;
public void loadBitmap(String url) {
if (loadtarget == null) loadtarget = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// do something with the Bitmap
handleLoadedBitmap(bitmap);
}
@Override
public void onBitmapFailed() {
}
};
Picasso.with(this).load(url).into(loadtarget);
}
public void handleLoadedBitmap(Bitmap b) {
// do something here
}