【问题标题】:Picasso image load callbackPicasso 图片加载回调
【发布时间】:2014-07-27 03:41:45
【问题描述】:

我想使用 Picasso 在列表视图中加载三个连续的图像,一个在彼此之上。使用毕加索提供的方法可以很容易地做到这一点。然而,由于这些图像是在不同的时间加载的,因此在图像进入时会产生闪烁效果。例如,有时图像 2 会出现在图像 1 之前,而当图像 1 加载时会导致不自然的卡顿。如果我可以将列表视图的可见性设置为不可见,直到所有图像都可以显示,那会更好。但是,我找不到 Picasso 的回调方法,它会在加载图像时发出信号。

有人知道使用毕加索解决这种情况的方法吗?

谢谢

【问题讨论】:

  • @ElectronicGeek 我认为 OP 提出这个问题的方式很好。他解释了他在问题中已经做了什么(他已经实现了图像加载但遇到闪烁),并询问毕加索是否提供某种图像加载回调来解决问题。这一点都没有错。

标签: java android picasso


【解决方案1】:

.into 方法提供了第二个参数,它是成功和失败的回调。您可以使用它来跟踪所有三个都被调用的时间并立即对它们的可见性采取行动。

Javadoc:https://square.github.io/picasso/2.x/picasso/com/squareup/picasso/RequestCreator.html#into-android.widget.ImageView-com.squareup.picasso.Callback-

【讨论】:

  • @jamrockRay 现在应该修复了
  • 回调未被调用
【解决方案2】:

下面是一个简单的例子,如何实现 Picasso 图片加载回调:

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

在最新的毕加索版本上,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
                        }
                    });

【讨论】:

    【解决方案3】:

    您可以使用 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;
        }
    }
    

    【讨论】:

      【解决方案4】:

      这是通过简单的毕加索回调将图像 url 加载到图像视图中

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

      【讨论】:

      • 你可能会在你的代码中添加一些进一步的解释。
      【解决方案5】:

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

      【讨论】:

      • 这很浪费,而且比同时加载要花更长的时间。
      猜你喜欢
      • 2018-07-06
      • 1970-01-01
      • 2017-01-20
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      相关资源
      最近更新 更多