5 quick animations to make your Compose app stand out: Adding Life to Your Android Apps
Hi, I’m Gourav, an Android engineer at Spyne.AI, and today I want to share a fascinating journey through the world of animations in Compose. If you’ve ever felt your app was too static and yearned to infuse it with dynamic, eye-catching movements, then you’re in the right place. Let’s dive in and explore how to make your app not just functional but visually stunning.
1. AnimatedVisibility Composable
What is it?
In any app, the way content appears and disappears can profoundly impact user experience. AnimatedVisibility Composable is our first hero. It elegantly manages the visibility of UI elements, ensuring a smooth transition rather than a jarring instant change.
Customization & Code Snippet
Imagine a timestamp in a chat app that appears and disappears with a click. Rather than a blunt show/hide, wrap the visible state in an AnimatedVisibility composable. Here’s how:
kotlinCopy code
AnimatedVisibility(visible = /* your dynamic state */) {
// Your content
}
By doing this, the transition from visible to invisible becomes a pleasant experience.
2. AnimateContentSize
What is it?
Next, we tackle the challenge of changing the size of UI elements. AnimateContentSize is a modifier that allows elements to transition between sizes smoothly.
Customization & Code Snippet
Consider a text block expanding to reveal more content upon user interaction. Add AnimateContentSize to your modifier chain like this:
kotlinCopy code
Text(
text = "Your text here",
modifier = Modifier.animateContentSize()
)
For added flair, try a spring animation with low stiffness for a playful bounce effect.
3. AnimatedContent Composable
What is it?
Transitioning between different UI components seamlessly is crucial. AnimatedContent Composable makes this possible, offering a smooth shift between different states of your app.
Customization & Code Snippet
For example, in a tabbed interface, switch content with AnimatedContent:
kotlinCopy code
AnimatedContent(targetState = /* your target state */) {
// Your composable based on the target state
}
Further, customize with slideIntoContainer
and slideOutContainer
to dictate how new content enters and exits the screen.
4. animateFloatAsState (Progress Bar)
What is it?
Animation isn’t just for visual elements; it’s also effective for indicating progress. animateFloatAsState smoothly transitions the progress indicator.
Customization & Code Snippet
In a survey app, to display progression:
kotlinCopy code
val progress by animateFloatAsState(targetValue = /* your progress value */)
LinearProgressIndicator(progress = progress)
This way, the progress bar elegantly moves between stages, enhancing the user experience.
5. Rotation Animation (Custom Rainbow Circular Border)
What is it?
Lastly, a rotation animation can add a magical touch, especially with something like a rotating rainbow border around a profile picture.
Customization & Code Snippet
Using rotate(rotationAnimation.value)
within a draw function, you can create this mesmerizing effect:
kotlinCopy code
val rotationAnimation = rememberInfiniteTransition().animateFloat(
initialValue = 0f,
targetValue = 360f,
animationSpec = infiniteRepeatable(...)
)
Image(
painter = /* your image painter */,
contentDescription = "Profile picture",
modifier = Modifier.graphicsLayer(rotationZ = rotationAnimation.value)
)
This simple addition transforms a static image into a dynamic, eye-catching element.
In conclusion, adding animations to your Compose app isn’t just about aesthetics; it’s about creating a more intuitive and engaging user experience. With these tools at your disposal, you can bring your apps to life, making every interaction a delight. Keep experimenting and happy coding! Stay tuned for my next article, let’s grow together.
Thanks for reading! If you enjoyed it, please clap and check out my Twitter and Instagram for more on sprinting-software-startups. Join the WhatsApp channel for a closer look into my daily life.
Recommended Reading