-
-
Save L10n42/9361c9729b743012a0b3499cc28f42d0 to your computer and use it in GitHub Desktop.
Shimmering Text Animation in Jetpack Compose
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
import androidx.compose.animation.core.DurationBasedAnimationSpec | |
import androidx.compose.animation.core.LinearEasing | |
import androidx.compose.animation.core.animateFloat | |
import androidx.compose.animation.core.infiniteRepeatable | |
import androidx.compose.animation.core.rememberInfiniteTransition | |
import androidx.compose.animation.core.tween | |
import androidx.compose.material3.LocalTextStyle | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.remember | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.geometry.Offset | |
import androidx.compose.ui.geometry.Size | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.graphics.LinearGradientShader | |
import androidx.compose.ui.graphics.Shader | |
import androidx.compose.ui.graphics.ShaderBrush | |
import androidx.compose.ui.text.TextStyle | |
/** | |
* A composable function that displays shimmering text using a customizable shimmer animation. | |
* | |
* @param text The text to be displayed. | |
* @param shimmerColor The color used for the shimmer effect. | |
* @param modifier The modifier to be applied to the `Text` composable. | |
* @param textStyle The style of the text. | |
* @param animationSpec The animation specification for controlling the shimmer speed, delay, and easing. | |
*/ | |
@Composable | |
fun ShimmeringText( | |
text: String, | |
shimmerColor: Color, | |
modifier: Modifier = Modifier, | |
textStyle: TextStyle = LocalTextStyle.current, | |
animationSpec: DurationBasedAnimationSpec<Float> = tween(1000, 500, LinearEasing) | |
) { | |
val infiniteTransition = rememberInfiniteTransition(label = "ShimmeringTextTransition") | |
val shimmerProgress by infiniteTransition.animateFloat( | |
initialValue = 0f, | |
targetValue = 1f, | |
animationSpec = infiniteRepeatable(animationSpec), | |
label = "ShimmerProgress" | |
) | |
val brush = remember(shimmerProgress) { | |
object : ShaderBrush() { | |
override fun createShader(size: Size): Shader { | |
val initialXOffset = -size.width | |
val totalSweepDistance = size.width * 2 | |
val currentPosition = initialXOffset + totalSweepDistance * shimmerProgress | |
return LinearGradientShader( | |
colors = listOf(Color.Transparent, shimmerColor, Color.Transparent), | |
from = Offset(currentPosition, 0f), | |
to = Offset(currentPosition + size.width, 0f) | |
) | |
} | |
} | |
} | |
Text( | |
text = text, | |
modifier = modifier, | |
style = textStyle.copy(brush = brush) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment