我创建了矢量可绘制动画,并希望在Compose中使用。我在官方文档中发现需要调用
animatedVectorResource
,但是当前版本的Compose已经删除了整个AnimatedImageVector
。如何在当前Compose版本中启动动画可绘制?animatedVectorResource
,但是当前版本的Compose已经删除了整个AnimatedImageVector
。如何在当前Compose版本中启动动画可绘制?您需要将Compose更新到1.1.0-alpha01
版本,并按照最新的更改日志添加模块androidx.compose.animation:animation-graphics
implementation("androidx.compose.animation:animation-graphics:1.1.0-alpha01")
val image = animatedVectorResource(id = R.drawable.animated_vector)
val atEnd by remember { mutableStateOf(false) }
Icon(
painter = image.painterFor(atEnd = atEnd),
contentDescription = null
)
AnimatedImageVector
被暂时移除在1.0.0-rc01
中,并且在最终稳定版1.0.0
中不存在。
从1.1.0-alpha01
开始,AnimatedImageVector
和相关的API现在在新的androidx.compose.animation:animation-graphics
模块中。
您可以使用类似以下的内容:
val image = animatedVectorResource(drawableId)
var atEnd by remember { mutableStateOf(false) }
Image(
painter = image.painterFor(atEnd),
contentDescription = "Your content description",
modifier = Modifier.size(64.dp).clickable {
atEnd = !atEnd
}
)
更新的答案:参考资料
implementation("androidx.compose.animation:animation-graphics:1.3.3")
var atEnd by remember { mutableStateOf(false) }
val drawable = AnimatedImageVector.animatedVectorResource(R.drawable.avd_anim)
IconButton(onClick = { atEnd = !atEnd }) {
Icon(
painter = rememberAnimatedVectorPainter(animatedImageVector = drawable, atEnd = atEnd),
contentDescription = null
)
}