टेक्स्ट के दिखने का ऐनिमेशन, हर वर्ण के लिए अलग-अलग किया जा सकता है, ताकि यह टाइपिंग के स्ट्रीमिंग इफ़ेक्ट जैसा दिखे. यह इफ़ेक्ट, टाइपराइटर से टाइप करने पर दिखने वाले इफ़ेक्ट जैसा होता है.
वर्शन के साथ काम करना
इसे लागू करने के लिए, ज़रूरी है कि आपके प्रोजेक्ट का minSDK एपीआई लेवल 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
, वर्णों के बीच में देरी करने के लिए कोरुटाइन शुरू करता है. ऐनिमेशन को ट्रिगर करने के लिए, कोड ब्लॉक को क्लिक Listener या किसी अन्य इवेंट से बदला जा सकता है.substringText
की वैल्यू अपडेट होने पर,Text
कॉम्पोज़ेबल फिर से रेंडर होता है.
नतीजे
ऐसे संग्रह जिनमें यह गाइड शामिल है
यह गाइड, चुने गए क्विक गाइड के कलेक्शन का हिस्सा है. इसमें Android डेवलपमेंट के बड़े लक्ष्यों के बारे में बताया गया है:
![](https://developer.android.com/static/images/quick-guides/collection-illustration.png?hl=hi)
डिसप्ले टेक्स्ट
![](https://developer.android.com/static/images/quick-guides/collection-illustration.png?hl=hi)