Compose 中的动画矢量图像

在 Compose 中,您可以通过几种不同的方式为矢量添加动画效果。您可以使用以下任一方法:

带动画的矢量可绘制对象(实验性)

沙漏动画,内容在旋转
图 1. Compose 中的动画矢量可绘制对象

如需使用 AnimatedVectorDrawable 资源,请使用 animatedVectorResource 加载可绘制对象文件,并传入 boolean 以在可绘制对象的开始和结束状态之间切换,从而执行动画。

@Composable
fun AnimatedVectorDrawable() {
    val image = AnimatedImageVector.animatedVectorResource(R.drawable.ic_hourglass_animated)
    var atEnd by remember { mutableStateOf(false) }
    Image(
        painter = rememberAnimatedVectorPainter(image, atEnd),
        contentDescription = "Timer",
        modifier = Modifier.clickable {
            atEnd = !atEnd
        },
        contentScale = ContentScale.Crop
    )
}

如需详细了解可绘制文件的格式,请参阅为可绘制图形添加动画