使用Picasso加载图像时淡入动画 [英] Fade in animation while loading image Using Picasso

查看:110
本文介绍了使用Picasso加载图像时淡入动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Imageview上加载图像时显示淡入淡出的效果.我正在使用毕加索来缓存图像并在图像视图中显示.我已经为此进行了大量搜索,但找不到任何解决方案.

I want to show a fade effect when image is loading on Imageview. I am using picasso to cache image and display in image view. I have searched alot for this but couldnt find any solution.

我以前使用过,我知道在某些版本中,他们有.fade(int Duration)方法在加载时使图像褪色,但是我再也找不到这种方法了.

I have used earlier before and i know in some version they had .fade(int Duration) method to fade image while loading but i couldnt find this method anymore.

这是我现在正在做的事情

Here is what i am doing now

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

请帮助我,我已经被困在很长一段时间了. 预先感谢.

Please help me i have been stuck in this for quite long time. Thanks in advance.

推荐答案

此处:

如果图像来自内存缓存以外的任何地方,则淡入淡出 应该自动应用.

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

如果您选中 PicassoDrawable

If you check the 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加载的图像,而不是从内存/缓存加载的图像 和FADE_DURATION = 200f; //ms

fade effect is already applied for images loaded from n/w and not memory/cache and FADE_DURATION = 200f; //ms

要强制褪色,请再次引用杰克·沃顿的答案此处:

To force fade, again quoting jake wharton's answer here:

您可以指定noFade(),然后始终在 图片加载的回调.您还可以依赖于被调用的回调 同步确定动画是否需要播放.

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加载图像时淡入动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆