简体   繁体   English

如何在不使用ImageView的情况下使用Picasso加载位图?

[英]How to load a Bitmap with Picasso without using an ImageView?

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(); 或者只是从这个Picasso.with(activity).load(url).get();获得Bitmap Picasso.with(activity).load(url).get(); . Is there anyway to add callback for just download the image? 无论如何都要添加回调才能下载图像? If possible please provide sample code, Cheers! 如果可能请提供示例代码,干杯!

You can create a Target and then modify the Bitmap inside the Targets callback method onBitmapLoaded(...) . 您可以创建一个 Target ,然后在Bitmap回调方法onBitmapLoaded(...)修改Bitmap Here is how: 方法如下:

// 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM