如何使用淡入淡出动画实现圆形图像?

时间:2015-07-19 02:33:48

标签: android listview animation fadein

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

我试过这个:

 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。

所以我很奇怪有没有简单的方法来实现动画淡入淡出的圆形图像?你会怎么做?

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

2 个答案:

答案 0 :(得分:2)

使用毕加索库进行动画制作,你也可以对图像进行圆形渲染,并且可以自动将图像存储到捕捉中。

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

答案 1 :(得分:1)

您可以使用动画来实现淡出和淡入动画在res-> anim中创建文件夹并定义fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:fillAfter="true">
     <alpha
         android:fromAlpha="0.0"
         android:toAlpha="1.0"
         android:duration="1000"
         android:interpolator="@android:anim/accelerate_interpolator"
      />
</set>

fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:fillAfter="true">
     <alpha
         android:fromAlpha="1.0"
         android:toAlpha="0.0"
         android:duration="1000"
         android:interpolator="@android:anim/accelerate_interpolator"
      />
</set>

然后在你的java

 Animation anim=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_out);
 imageView.startAnimation(anim);
 final Handler handler=new Handler();

 handler.postDelayed(new Runnable(){

    @Override
    public void run() {
        imageView.setImageResource(R.drawable.ic_network_wifi_black_48dp);//changing to different image ,here you will set image that you have loaded
        anim=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in);
        imageView.startAnimation(anim);
    }

  },1000);