
Kottie
Compose Multiplatform animation library that parses Adobe After Effects animations. Inspired by Airbnb/Lottie.
Installation
Add the dependency in your common module's commonMain source set:
implementation("io.github.ismai117:kottie:latest_version")
iOS Setup
For iOS, add the Lottie framework via Swift Package Manager:
- In Xcode, select File → Add Packages...
- Enter
https://github.com/airbnb/lottie-spm.git
- Go to File → Packages → Resolve Package Versions
Usage
Basic Example
val composition = rememberKottieComposition(
KottieCompositionSpec.Url("https://example.com/animation.json")
)
val animationState by animateKottieCompositionAsState(
composition = composition,
iterations = Kottie.IterateForever
)
KottieAnimation(
composition = composition,
progress = { animationState.progress },
modifier = Modifier.size(300.dp)
)
Loading Compositions
Load animations from different sources using KottieCompositionSpec:
val composition = rememberKottieComposition(
KottieCompositionSpec.Url("https://example.com/animation.json")
)
var json by remember { mutableStateOf("") }
LaunchedEffect(Unit) {
json = Res.readBytes("files/animation.json").decodeToString()
}
val composition = rememberKottieComposition(KottieCompositionSpec.File(json))
val composition = rememberKottieComposition(
KottieCompositionSpec.JsonString("""{"v":"5.7.4",...}""")
)
Handling Loading States
KottieComposition is a sealed type with three states:
when (composition) {
is KottieComposition.Loading -> {
CircularProgressIndicator()
}
is KottieComposition.Success -> {
KottieAnimation(
composition = composition,
progress = { animationState.progress }
)
}
is KottieComposition.Failure -> {
Text("Error: ${composition.error.message}")
}
}
Controlling Playback
var isPlaying by remember { mutableStateOf(true) }
val animationState by animateKottieCompositionAsState(
composition = composition,
isPlaying = isPlaying
)
Button(onClick = { isPlaying = !isPlaying }) {
Text(if (isPlaying) "Pause" else "Play")
}
Speed
val animationState by animateKottieCompositionAsState(
composition = composition,
speed = 1.5f
)
Iterations
val animationState by animateKottieCompositionAsState(
composition = composition,
iterations = 3
)
val animationState by animateKottieCompositionAsState(
composition = composition,
iterations = Kottie.IterateForever
)
Observing Animation State
val animationState by animateKottieCompositionAsState(
composition = composition,
iterations = Kottie.IterateForever
)
LaunchedEffect(animationState) {
println("Progress: ${animationState.progress}")
println("Playing: ${animationState.isPlaying}")
println("Completed: ${animationState.isCompleted}")
println("Iteration: ${animationState.iteration}")
}
License
Copyright 2024 ismai117
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http:
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests.