-
Notifications
You must be signed in to change notification settings - Fork 287
Code snippet for Compose doc at https://developer.android.com/quick-g… #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
69588da
Code snippet for Compose doc at https://developer.android.com/quick-g…
thedmail 783b770
Apply Spotless
thedmail 8b542c0
Fixes malformed region tags.
thedmail c1803c3
Merge remote-tracking branch 'origin/animate-char-by-char' into anima…
thedmail 4d0bd1f
Fixes region tags.
thedmail c5c65e1
Apply Spotless
thedmail File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,6 +109,9 @@ import androidx.compose.ui.unit.Dp | |
| import androidx.compose.ui.unit.IntSize | ||
| import androidx.compose.ui.unit.dp | ||
| import com.example.compose.snippets.R | ||
| import java.text.BreakIterator | ||
| import java.text.StringCharacterIterator | ||
| import kotlinx.coroutines.delay | ||
|
|
||
| /* | ||
| * Copyright 2023 The Android Open Source Project | ||
|
|
@@ -820,3 +823,39 @@ private fun Expanded() { | |
| @Composable | ||
| private fun ContentIcon() { | ||
| } | ||
|
|
||
| // [START android_compose_animations_vector_char_by_char] | ||
| @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) | ||
| // [END android_compose_animations_animate_char_by_char] | ||
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.