我有 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);
}
}
但是这段代码不起作用,因为我已经尝试使用 CircleImageView和 RoundedImageView作为 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/