為文字逐字加入動畫效果

您可以逐一為文字顯示方式製作動畫,讓文字看起來像是打字機打出來的串流效果。

版本相容性

這項實作作業需要將專案 minSDK 設為 API 級別 21 以上。

依附元件

逐一為文字字元加上動畫效果

這段程式碼會逐一為文字字元加上動畫效果。這個索引會追蹤要顯示多少文字。顯示的文字會動態更新,只顯示目前索引之前的字元。最後,變數會在變更時執行動畫。

@Composable
private fun AnimatedText() {
    val text = "This text animates as though it is being typed \uD83E\uDDDE\u200D♀\uFE0F \uD83D\uDD10  \uD83D\uDC69\u200D❤\uFE0F\u200D\uD83D\uDC68 \uD83D\uDC74\uD83C\uDFFD"

    // Use BreakIterator as it correctly iterates over characters regardless of how they are
    // stored, for example, some emojis are made up of multiple characters.
    // You don't want to break up an emoji as it animates, so using BreakIterator will ensure
    // this is correctly handled!
    val breakIterator = remember(text) { BreakIterator.getCharacterInstance() }

    // Define how many milliseconds between each character should pause for. This will create the
    // illusion of an animation, as we delay the job after each character is iterated on.
    val typingDelayInMs = 50L

    var substringText by remember {
        mutableStateOf("")
    }
    LaunchedEffect(text) {
        // Initial start delay of the typing animation
        delay(1000)
        breakIterator.text = StringCharacterIterator(text)

        var nextIndex = breakIterator.next()
        // Iterate over the string, by index boundary
        while (nextIndex != BreakIterator.DONE) {
            substringText = text.subSequence(0, nextIndex).toString()
            // Go to the next logical character boundary
            nextIndex = breakIterator.next()
            delay(typingDelayInMs)
        }
    }
    Text(substringText)

程式碼重點

  • BreakIterator 會正確疊代字元,無論字元儲存方式為何。舉例來說,動畫表情符號是由多個字元組成;BreakIterator 可確保系統將這些字元視為單一字元,避免動畫中斷。
  • LaunchedEffect 會啟動協同程式,在字元之間加入延遲。您可以將程式碼區塊換成點擊事件監聽器或其他事件,藉此觸發動畫。
  • 每當 substringText 的值更新時,Text 可組合函式就會重新算繪。

結果

圖 1. 文字和表情符號會逐字元顯示動畫。

包含本指南的集合

本指南是精選快速指南系列的一部分,涵蓋更廣泛的 Android 開發目標:

文字是所有 UI 的核心。瞭解在應用程式中呈現文字的不同方式,提供令人愉悅的使用者體驗。
這一系列影片將介紹各種 Compose API,快速展示可用的 API 和使用方式。

如有問題或想提供意見

前往常見問題頁面,瞭解快速指南或與我們聯絡,分享您的想法。