Java 毕加索图片加载回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24072176/
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
Picasso image load callback
提问by Ebay89
I want to use Picasso to load three consecutive images one on top of each other in a listview. Using the methods Picasso provides makes this easy. However because these images are loading in at different times it causes a flickering effect as the images come in. For example sometimes image 2 appears before image 1, and when image 1 loads it causes an unnatural stutter. It would be better if I could set the listview's visibility to invisible until all the images are available to be shown. However, there is no callback method I could find for Picasso that would signal when an image has been loaded.
我想使用毕加索在列表视图中加载三个连续的图像,一个在彼此之上。使用毕加索提供的方法使这变得容易。然而,因为这些图像在不同的时间加载,所以当图像进入时会导致闪烁效果。例如,有时图像 2 出现在图像 1 之前,并且当图像 1 加载时会导致不自然的卡顿。如果我可以将列表视图的可见性设置为不可见,直到所有图像都可以显示,那就更好了。但是,我找不到 Picasso 的回调方法可以在加载图像时发出信号。
Does anyone know of a solution for this kind of a situation using Picasso?
有谁知道使用毕加索解决这种情况的解决方案?
Thanks
谢谢
采纳答案by Jake Wharton
The .into
method provides a second argument which is a callback to success and failure. You can use this to keep track of when all three have been called and act on their visibility all at once.
该.into
方法提供了第二个参数,它是成功和失败的回调。您可以使用它来跟踪所有三个被调用的时间,并立即对它们的可见性采取行动。
Javadoc:https: //square.github.io/picasso/2.x/picasso/com/squareup/picasso/RequestCreator.html#into-android.widget.ImageView-com.squareup.picasso.Callback-
回答by mbmc
回答by Lord Sidious
You can implement a callback with Picasso like shown below:
您可以使用 Picasso 实现回调,如下所示:
ImageHandler.getSharedInstance(getApplicationContext()).load(imString).skipMemoryCache().resize(width, height).into(image, new Callback() {
@Override
public void onSuccess() {
layout.setVisibility(View.VISIBLE);
}
@Override
public void onError() {
}
});
}
The implementation of my ImageHandler class is shown below:
我的 ImageHandler 类的实现如下所示:
public class ImageHandler {
private static Picasso instance;
public static Picasso getSharedInstance(Context context)
{
if(instance == null)
{
instance = new Picasso.Builder(context).executor(Executors.newSingleThreadExecutor()).memoryCache(Cache.NONE).indicatorsEnabled(true).build();
}
return instance;
}
}
回答by dzikovskyy
Here is a simple example how to impement Picasso picture loading callback:
下面是一个如何实现毕加索图片加载回调的简单示例:
Picasso.with(MainActivity.this)
.load(imageUrl)
.into(imageView, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
//do smth when picture is loaded successfully
}
@Override
public void onError() {
//do smth when there is picture loading error
}
});
On the latest Picasso's version, onError recives an Exception as parameter and uses get() instead of with()
在最新的毕加索版本中,onError 接收异常作为参数并使用 get() 而不是 with()
Picasso.get()
.load(imageUrl)
.into(imageView, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
//do smth when picture is loaded successfully
}
@Override
public void onError(Exception ex) {
//do smth when there is picture loading error
}
});
回答by MIDHUN CEASAR
This is loading a image url into an imageview with simple picasso callbacks
这是使用简单的毕加索回调将图像 url 加载到图像视图中
Picasso.with(this)
.load(Picurl)
.into(Imageview, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
}
}
);
And this is picasso image loading with more callbacks
这是带有更多回调的毕加索图像加载
private void loadImage() {
Picasso.with(this)
.load(PicURL)
.into(mContentTarget);
}
private Target mContentTarget = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Imageview.setImageBitmap(bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};