ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 加载图像时淡入动画使用Picasso

2019-10-07 18:26:13  阅读:299  来源: 互联网

标签:fade image-loading android picasso


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

我之前使用过,我知道在某些版本中他们有.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 one rror() {
        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 one rror() {
                        Log.v("Picasso", "Could not fetch image");
                    }
                });
    }
}

请帮助我,我已经被困在这很长一段时间了.
提前致谢.

解决方法:

引用杰克沃顿的回答here

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

如果你检查PicassoDrawable

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 {
.
.
.

淡入淡出效果已经应用于从n / w而不是内存/缓存加载的图像
和FADE_DURATION = 200f; //女士

为了强制淡出,再次引用杰克沃顿的回答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 onl oad() {
    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);

标签:fade,image-loading,android,picasso
来源: https://codeday.me/bug/20191007/1868450.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有