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
    )
}

有關可繪項目檔案格式的詳細資訊請參閱「以動畫方式呈現可繪項目圖形」。