繁体   English   中英

毕加索图像加载回调

[英]Picasso image load callback

我想用Picasso在列表视图中一个一个地加载三个连续的图像。 使用Picasso提供的方法可以轻松实现。 然而,因为这些图像在不同时间加载,所以当图像进入时会引起闪烁效果。例如,有时图像2出现在图像1之前,并且当图像1加载时,会导致不自然的口吃。 如果我可以将listview的可见性设置为不可见,直到可以显示所有图像,那会更好。 但是,我没有找到Picasso的回调方法,它会在图像加载时发出信号。

有没有人知道使用Picasso这种情况的解决方案?

谢谢

.into方法提供了第二个参数,它是成功和失败的回调。 您可以使用它来跟踪何时调用所有三个并同时对其可见性进行操作。

Javadoc: https//square.github.io/picasso/2.x/picasso/com/squareup/picasso/RequestCreator.html#into-android.widget.ImageView-com.squareup.picasso.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
                        }
                    });

在最新的Picasso版本中,onError将Exception作为参数重新使用,并使用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
                        }
                    });

您可以使用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() {

            }
        });
}

我的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;
    }
}

这是使用简单的毕加索回调将图像URL加载到imageview中

           Picasso.with(this)
            .load(Picurl)
            .into(Imageview, new Callback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError() {


                        }
                    }
            );

这是毕加索图像加载更多的回调

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

您可以使用Target对象。 一旦target1收到回调,你就可以下载第二个资产,然后在target2获得回调,然后触发第三次下载。

暂无
暂无

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

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