使用毕加索加载图像时淡入动画

Fade in animation while loading image Using Picasso

我想在 Imageview 上加载图像时显示淡入淡出效果。我正在使用 picasso 来缓存图像并在图像视图中显示。我已经搜索了很多,但找不到任何解决方案。

我以前用过,我知道在某些版本中他们有 .fade(int Duration) 方法在加载时淡化图像,但我找不到这个方法了。

这是我现在正在做的事情

Picasso.with(context)
                .load(viewHolder.data.imageList.get(0).url)
                .networkPolicy(NetworkPolicy.OFFLINE)
                .placeholder(R.drawable.a_place_holder_list_view)
                .error(R.drawable.a_place_holder_list_view)
                .into(viewHolder.ivUser, context.loadImage(viewHolder.ivUser, viewHolder.data.imageList.get(0).url));


public Callback loadImage(RoundedImageView ivUser, String url) {
    return new callback(ivUser, url);
}

public class callback implements Callback {

    RoundedImageView imageView;
    String url;

    public callback(RoundedImageView imageView, String url) {
        this.imageView = imageView;
        this.url = url;
    }

    @Override
    public void onSuccess() {

    }

    @Override
    public void onError() {
        Picasso.with(BaseActivity.this)
                .load(url)
                .placeholder(R.drawable.a_place_holder_list_view)
                .error(R.drawable.a_place_holder_list_view)
                .into(imageView, new Callback() {
                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onError() {
                        Log.v("Picasso", "Could not fetch image");
                    }
                });
    }
}

请帮助我,我已经被困在这个问题上很长时间了。 提前致谢。

引用 Jake Wharton 的回答 here:

If the image comes from anywhere except the memory cache the fade should be automatically applied.

如果勾选 PicassoDrawable class

boolean fade = loadedFrom != MEMORY && !noFade;
if (fade) {
  this.placeholder = placeholder;
  animating = true;
  startTimeMillis = SystemClock.uptimeMillis();
}
.
.
.
@Override public void draw(Canvas canvas) {
if (!animating) {
  super.draw(canvas);
} else {
.
.
.

fade effect 已应用于从 n/w 而不是 memory/cache 加载的图像 和 FADE_DURATION = 200f; //ms

强制淡入淡出,再次引用 jake wharton 的回答 here:

You can specify noFade() and then always play an animation in the image loaded callback. You can also rely on the callback being called synchronously to determine if an animation needs played.

final AtomicBoolean playAnimation = new AtomicBoolean(true);
Picasso.with(context).load(..).into(imageView, new Callback() {
  @Override public void onLoad() {
    if (playAnimation.get()) {
      //play fade
      Animation fadeOut = new AlphaAnimation(0, 1);
      fadeOut.setInterpolator(new AccelerateInterpolator());
      fadeOut.setDuration(1000);
      imageView.startAnimation(fadeOut);

      Animation fadeOutPlaceholder = new AlphaAnimation(1, 0);
      fadeOutPlaceholder.setInterpolator(new AccelerateInterpolator());
      fadeOutPlaceholder.setDuration(1000);
      placeHolderImageView.startAnimation(fadeOutPlaceholder);
    }
  }
  //..
});
playAnimation.set(false);

你可以简单地做

Picasso.with(context).load(url).fetch(new Callback(){
            @Override
            public void onSuccess() {
                imageView.setAlpha(0f);
                Picasso.with(context).load(url).into(imageView);
                imageView.animate().setDuration(300).alpha(1f).start();
            }

            @Override
            public void onError() {

            }
        });

我这样做:

Picasso.get().load(url).fit().noFade().centerInside().into(imageView, new Callback() {

                @Override
                public void onSuccess() {
                    imageView.setAlpha(0f);
                    imageView.animate().setDuration(200).alpha(1f).start();
                }

                @Override
                public void onError(Exception e) {
                }
            });