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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:00:13  来源:igfitidea点击:

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

androidandroid-imageviewpicassoandroid-bitmap

提问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 aTargetand then modify the Bitmapinside 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
}