Java 如何监听毕加索 (Android) 加载完成事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26548660/
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-11 02:46:04  来源:igfitidea点击:

How to listen for Picasso (Android) load complete events?

javaandroidpicasso

提问by Keith

Is there a way to listen for events from Picasso when using the builder like:

有没有办法在使用构建器时监听来自毕加索的事件,例如:

Picasso.with(getContext()).load(url).into(imageView);

Picasso.with(getContext()).load(url).into(imageView);

I'm trying to call requestLayout()and invalidate()on the parent GridViewso it'll resize properly but I don't know how to set a listener or callback.

我正在尝试在父级上调用requestLayout()和以便它正确调整大小,但我不知道如何设置侦听器或回调。invalidate()GridView

I see that Picasso has error event reporting, but is there a success event?

我看到毕加索有错误事件报告,但是有成功事件吗?

采纳答案by MrEngineer13

You can use a Callbackto get onSuccess and onError events. Just add a new Callback to your request like so:

您可以使用 aCallback来获取 onSuccess 和 onError 事件。只需在您的请求中添加一个新的回调,如下所示:

Picasso.with(getContext())
    .load(url)
    .into(imageView, new com.squareup.picasso.Callback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError() {

                        }
                    });

Then you can perform any alterations and modifications in the onSuccess callback.

然后您可以在 onSuccess 回调中执行任何更改和修改。

回答by Androidme

If you need to access the bitmap before it is loaded to the view, try using :

如果您需要在加载到视图之前访问位图,请尝试使用:

private Target target = new Target() {
      @Override
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {       
      }
      @Override
      public void onBitmapFailed() {
      }
}

In the calling method:

在调用方法中:

Picasso.with(this).load("url").into(target);

Ideally you'd implement Target on a view or view holder object directly.

理想情况下,您应该直接在视图或视图持有者对象上实现 Target。

Hope this helps

希望这可以帮助

回答by Mateusz Pryczkowski

Square lately has updated Target class and now there are more methods to override (onPrepareLoadand onBitmapFailed):

Square 最近更新了 Target 类,现在有更多方法可以覆盖(onPrepareLoadonBitmapFailed):

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) {

    }
};

And you still have to use:

你仍然必须使用:

Picasso.with(context).load(url).into(target);

回答by Guilherme Santos

Answering @Jas follow up question as a comment to MrEngineer13's answer (since I don't have enough reputation to comment in any answer), you should use the error()method prior to registering the Callbackat the into()method, for example:

回答@Jas 后续问题作为对 MrEngineer13 答案的评论(因为我没有足够的声誉在任何答案中发表评论),您应该error()在注册方法之前使用Callbackinto()方法,例如:

Picasso.with(getContext())
    .load(url)
    .error(R.drawable.error_placeholder_image)
    .into(imageView, new com.squareup.picasso.Callback() {
        @Override
        public void onSuccess() {
            //Success image already loaded into the view
        }

        @Override
        public void onError() {
            //Error placeholder image already loaded into the view, do further handling of this situation here
        }
    }
);

回答by vaibhav jain

 private final Callback mImageCallback = new Callback() {
        @Override
        public void onSuccess() {
            startPostponedEnterTransition();
        }

        @Override
        public void onError() {
            startPostponedEnterTransition();
        }
    };

RequestCreator creator = Picasso.with(getActivity()).load(list.get(position).getId());
creator.into(imageView, mImageCallback);

回答by A.G.THAMAYS

Try This

尝试这个

       Picasso.with(context)
            .load(services.get(position).getImageInactive())
            .into(holder.icon, new Callback() {
                @Override
                public void onSuccess() {
                    holder.imageLoad.setVisibility(View.GONE);
                }

                @Override
                public void onError() {
                    holder.icon.setImageResource(R.drawable.ic_error_image_load);
                }
            });

回答by Fruit

As a complement to other answers, in case you don't know where to use original image view, e.g. ImageView myIV:

作为其他答案的补充,以防您不知道在哪里使用原始图像视图,例如ImageView myIV

Original:

原来的:

Picasso.with(activity).load(url).into(myIV);

New (inside onBitmapLoaded()of new Target()):

新的(内部onBitmapLoaded()new Target()):

public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
    myIV.setImageBitmap(bitmap);
}