我正在尝试在 jetpack Compose 中实现帧动画。
我知道在android视图系统中,可以通过使用AnimationDrawable来实现。
但是如何在 jetpack Compose 中正确使用动画可绘制类呢?
使用
AndroidView
包裹动画 ImageView
。
AndroidView(factory = { ctx ->
ImageView(ctx).apply {
setBackgroundResource(R.drawable.my_frame_animation);
val animationDrawable : AnimationDrawable = background as AnimationDrawable
animationDrawable.start()
}
}
最后我找到了一个解决方案。我通过以编程方式创建动画可绘制对象并将其分配给图像作为可绘制对象,在jetpack compose中使用动画可绘制对象实现了帧动画。最后使用动画可绘制对象引用来控制动画。我在下面发布了示例代码。 .
val animationDrawable = AnimationDrawable()
//add your images in animationDrawable by giving information of duration etc like you gave in xml file..
Image(
painter = rememberDrawablePainter(animationBlast),
contentDescription = null,
Modifier
.offset {
IntOffset(
offsetX.toInt() - (if (size == 250) 300 else 0),
offsetY.toInt() - (if (size == 250) 300 else 0)
)
}
.size(size.dp), contentScale = ContentScale.Crop
)
animationDrawable?.start()