Initial work has been done to add Lottie support to Jetpack Compose. It is currently in a pre-alpha stage but a public preview will be published soon.
Add the dependency to your project build.gradle
file:
dependencies {
...
implementation "com.airbnb.android:lottie-compose:$lottieVersion"
...
}
The primary Composable used for Lottie is LottieAnimation
. It takes two parameters:
- LottieAnimationSpec or LottieComposition?
- LottieAnimationState
LottieAnimationSpec
is a Kotlin sealed class that has a subtype for each animation source:
- res/raw
- assets
- url
- file
LottieAnimationState
gives you access to the current state of the animation such as its progress and play state and lets you call actions on it such as play/pause, speed, etc.
@Composable
fun Loader() {
val animationSpec = remember { LottieAnimationSpec.RawRes(R.raw.loading) }
LottieAnimation(
animationSpec,
modifier = Modifier.preferredSize(100.dp)
)
}
@Composable
fun Loader() {
val animationSpec = remember { LottieAnimationSpec.RawRes(R.raw.loading) }
// You can control isPlaying/progress/repeat/etc. with this.
val animationState = rememberLottieAnimationState(autoPlay = true, repeatCount = Integer.MAX_VALUE)
LottieAnimation(
animationSpec,
animationState,
modifier = Modifier.preferredSize(100.dp)
)
}