टेक्स्ट को एक-एक अक्षर करके ऐनिमेट किया जा सकता है, ताकि वह टाइपराइटर से टाइप किए गए टेक्स्ट की तरह दिखे.
वर्शन के साथ काम करने की सुविधा
इस सुविधा को लागू करने के लिए, आपके प्रोजेक्ट का 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एक को-रूटीन शुरू करता है, ताकि वर्णों के बीच देरी हो. ऐनिमेशन को ट्रिगर करने के लिए, कोड ब्लॉक को क्लिक लिसनर या किसी अन्य इवेंट से बदला जा सकता है.substringTextकी वैल्यू अपडेट होने पर,Textकंपोज़ेबल हर बार फिर से रेंडर होता है.
नतीजे
ऐसे कलेक्शन जिनमें यह गाइड शामिल है
यह गाइड, Quick Guide के इन चुने गए कलेक्शन का हिस्सा है. इनमें Android डेवलपमेंट के ज़्यादातर लक्ष्यों के बारे में बताया गया है:
डिसप्ले टेक्स्ट