我创建了矢量可绘制动画,我想在 Compose 中使用。我在官方文档中发现我需要调用
animatedVectorResource
。但整个 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
)
更新答案:参考
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
)
}
AnimatedImageVector
在 1.0.0-rc01
中被暂时删除,并且它不存在于最终稳定版本
1.0.0
中。
从
1.1.0-alpha01
AnimatedImageVector
开始,相关API现已在新版本中
androidx.compose.animation:animation-graphics
模块。
你可以使用类似的东西:
val image = AnimatedImageVector.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
}
)