android - 如何在动画中实现带有淡入淡出的圆形图像?

标签 android listview animation fadein

我有 ListView ,我希望每个列表项中的 ImageView 都是圆形的。我还希望动画图像看起来像一种“淡入”动画。

我试过这个:

 private void setImageDrawable(ImageView imageView, Bitmap bitmap) {
        if (mFadeInBitmap) { // mFadeInBitmap = true;
            BitmapDrawable drawable = null, placeholder = null;
            if (bitmap != null) {
                drawable = new BitmapDrawable(mResources, bitmap);
                placeholder = new BitmapDrawable(mResources, mPlaceHolderBitmap);
            }
            final TransitionDrawable td =
                    new TransitionDrawable(new Drawable[] {
                            placeholder,
                            drawable,
                    });

            imageView.setImageDrawable(td);
            td.startTransition(500);
        } else {
            imageView.setImageBitmap(bitmap);
        }
    }

但是这段代码不起作用,因为我已经尝试使用 CircleImageViewRoundedImageView作为 ImageView 。两者都不支持 TransitionAnimation。

所以我想知道是否有任何简单的方法来实现带有淡入淡出动画的圆形图像?你会怎么做?

非常感谢任何帮助。 提前致谢!

最佳答案

使用 picasso 库制作动画,还可以将图像圆形化,并且可以自动将图像存储到捕获中。

Picasso.with(activity).load(mayorShipImageLink).transform(new CircleTransform()).into(ImageView);

并创建java文件CircleTransform.java

public class CircleTransform implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());

int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;

 Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
 if (squaredBitmap != source) {
  source.recycle();
}

Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

Canvas canvas = new Canvas(bitmap);
 Paint paint = new Paint();
 BitmapShader shader = new BitmapShader(squaredBitmap,
BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);

 float r = size / 2f;
 canvas.drawCircle(r, r, r, paint);

  squaredBitmap.recycle();
  return bitmap;
   }

  @Override
   public String key() {
   return "circle";
   } 
  }

关于android - 如何在动画中实现带有淡入淡出的圆形图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31497457/

相关文章:

java - ActionBarSherlock 库导入后报错

android - 如何在 textview 中添加 imageview 或在 imageview 中添加 textview,android?

android - 重复使用膨胀的 View ,不膨胀超过一次

c# - 为其设置模板时,我的 ListView 项目未显示

ios - io swift 动画 : alpha and constraint

android - 移动开发准入阈值

android - 键盘以 Xamarin 形式将 Textarea 隐藏在混合 Webview 中

android - ScrollView 内的 TabLayout+ViewPager

ios - 如何将 UINavigationBar 与动画重叠?

ios - 如何一次翻转多个 View ?