avatarKappdev

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2527

Abstract

lass="hljs-keyword">val</span> shimmerProgress <span class="hljs-keyword">by</span> infiniteTransition.animateFloat( initialValue = <span class="hljs-number">0f</span>, targetValue = <span class="hljs-number">1f</span>, animationSpec = infiniteRepeatable(animationSpec), label = <span class="hljs-string">"ShimmerProgress"</span> )</pre></div><h2 id="dc01">Creating the Shimmer Brush</h2><p id="486d">Next, we create the shimmer brush that moves across the text based on the <code>shimmerProgress</code> value:</p><div id="032f"><pre><span class="hljs-keyword">val</span> brush = remember(shimmerProgress) { <span class="hljs-keyword">object</span> : ShaderBrush() { <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">createShader</span><span class="hljs-params">(size: <span class="hljs-type">Size</span>)</span></span>: Shader { <span class="hljs-comment">// Define the starting X offset, beginning outside the left edge of the text</span> <span class="hljs-keyword">val</span> initialXOffset = -size.width <span class="hljs-comment">// Total distance the shimmer will sweep across (double the text width for full coverage)</span> <span class="hljs-keyword">val</span> totalSweepDistance = size.width * <span class="hljs-number">2</span> <span class="hljs-comment">// Calculate the current position of the shimmer based on the animation progress</span> <span class="hljs-keyword">val</span> currentPosition = initialXOffset + totalSweepDistance * shimmerProgress

        <span class="hljs-keyword">return</span> LinearGradientShader(
            colors = listOf(Color.Transparent, shimmerColor, Color.Transparent),
            from = Offset(currentPosition, <span class="hljs-number">0f</span>),
            to = Offset(currentPosition + size.width, <span class="hljs-number">0f</span>)
        )
    }
}

}</pre></div><figure id="5400"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*5lOrMk3gVk9_clKZPZsEyg.png"><figcaption>Created by <a href="undefined"><b>Kappdev</b></a></figcaption></figure><figure id="347b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*yp_xMO4Fc6nIpgxLZZjhRg.png"><figcaption>Created by <a href="undefined"><b>Kappdev</b></a></figcaption></figure><h2 id="b6be">Rendering the Text</h2><p id="28d5">Finally, we render the <code>Text</code> composable wit

Options

h the animated shimmer brush applied:</p><div id="893a"><pre>Text( text = text, modifier = modifier, style = textStyle.copy(brush = brush) )</pre></div><p id="eab8">Congratulations 🥳! We’ve successfully built it 👏. You can find the full code on <a href="https://gist.github.com/L10n42/9361c9729b743012a0b3499cc28f42d0"><b>GitHub Gist</b></a><b> </b>🧑‍💻. Let’s explore the usage 👇</p><figure id="8c48"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*k-e9aTwqEjrNkq-eETC6Vg.png"><figcaption></figcaption></figure><h1 id="ef18">Practical Example 💁‍♂️</h1><p id="67bd">For this example, let’s create a simple loading text animation:</p><div id="4e9b"><pre>Box( modifier = Modifier .fillMaxSize() .background(Color.Black), contentAlignment = Alignment.Center ) { ShimmeringText( text = <span class="hljs-string">"LOADING"</span>, shimmerColor = Color.White, textStyle = LocalTextStyle.current.copy( fontSize = <span class="hljs-number">20.</span>sp, letterSpacing = <span class="hljs-number">5.</span>sp, fontWeight = FontWeight.Bold ) ) }</pre></div><h2 id="17fc">Output ✨</h2><figure id="a57d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*mvLbVdYoPJAnfg5vhAGVsA.gif"><figcaption></figcaption></figure><figure id="ab90"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*xQzj0_h9M_IyGvYWRnjmzw.gif"><figcaption></figcaption></figure><div id="aa83" class="link-block"> <a href="https://medium.com/@kappdev/list/b55bf37df3f0"> <div> <div> <h2>Custom Loaders | Jetpack Compose</h2> <div><h3>Discover and get inspired by creative loader animations in Jetpack Compose ✨</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*636b6d537da906945b6583ee42ac71ab3378fce3.jpeg)"></div> </div> </div> </a> </div><p id="b870"><b>Thank you</b> for reading this article! ❤️ If you found it <i>enjoyable</i> and <i>valuable</i>, show your appreciation by <b>clapping</b> 👏 and following <a href="undefined"><b>Kappdev</b></a> for more exciting articles 😊</p><figure id="3124"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VSdel_RRdopnsaWjcbQFsQ.png"><figcaption></figcaption></figure></article></body>

How to Create a Shimmering Text Animation in Jetpack Compose

Welcome 👋

In this article, we’ll create a stunning Shimmering Text Animation in Jetpack Compose, perfect for a minimalistic loader ✨

Excited? 🤩 Let’s dive in 🚀👇

Created by Kappdev

Defining the Function

Let’s start by defining the ShimmeringText composable function:

@Composable
fun ShimmeringText(
    text: String,
    shimmerColor: Color,
    modifier: Modifier = Modifier,
    textStyle: TextStyle = LocalTextStyle.current,
    animationSpec: DurationBasedAnimationSpec<Float> = tween(1000, 500, LinearEasing)
)

Parameters

🖤 text 👉 The text that will be displayed.

🖤 shimmerColor 👉 The color used for the shimmer effect.

🖤 modifier 👉 A Modifier to be applied to the Text composable.

🖤 textStyle 👉 The style of the text.

🖤 animationSpec 👉 The animation specification for controlling the shimmer speed, delay, and easing.

Implementing the Function

In this section, we’ll implement the ShimmeringText function.

Creating an Infinite Animation

First, we define an infinite animation transition:

val infiniteTransition = rememberInfiniteTransition(label = "ShimmeringTextTransition")

Using this transition, we create a shimmerProgress animation that runs from 0 to 1 and repeats:

val shimmerProgress by infiniteTransition.animateFloat(
    initialValue = 0f,
    targetValue = 1f,
    animationSpec = infiniteRepeatable(animationSpec),
    label = "ShimmerProgress"
)

Creating the Shimmer Brush

Next, we create the shimmer brush that moves across the text based on the shimmerProgress value:

val brush = remember(shimmerProgress) {
    object : ShaderBrush() {
        override fun createShader(size: Size): Shader {
            // Define the starting X offset, beginning outside the left edge of the text
            val initialXOffset = -size.width
            // Total distance the shimmer will sweep across (double the text width for full coverage)
            val totalSweepDistance = size.width * 2
            // Calculate the current position of the shimmer based on the animation progress
            val currentPosition = initialXOffset + totalSweepDistance * shimmerProgress

            return LinearGradientShader(
                colors = listOf(Color.Transparent, shimmerColor, Color.Transparent),
                from = Offset(currentPosition, 0f),
                to = Offset(currentPosition + size.width, 0f)
            )
        }
    }
}
Created by Kappdev
Created by Kappdev

Rendering the Text

Finally, we render the Text composable with the animated shimmer brush applied:

Text(
    text = text,
    modifier = modifier,
    style = textStyle.copy(brush = brush)
)

Congratulations 🥳! We’ve successfully built it 👏. You can find the full code on GitHub Gist 🧑‍💻. Let’s explore the usage 👇

Practical Example 💁‍♂️

For this example, let’s create a simple loading text animation:

Box(
    modifier = Modifier
        .fillMaxSize()
        .background(Color.Black),
    contentAlignment = Alignment.Center
) {
    ShimmeringText(
        text = "LOADING",
        shimmerColor = Color.White,
        textStyle = LocalTextStyle.current.copy(
            fontSize = 20.sp,
            letterSpacing = 5.sp,
            fontWeight = FontWeight.Bold
        )
    )
}

Output ✨

Thank you for reading this article! ❤️ If you found it enjoyable and valuable, show your appreciation by clapping 👏 and following Kappdev for more exciting articles 😊

Jetpack Compose
Android
Android Development
Software Engineering
Programming
Recommended from ReadMedium
avatarNine Pages Of My Life
Flow Layouts in Jetpack Compose

🎯Index

4 min read