Compose Multiplatform animation library that parses Adobe After Effects animations. Inspired by Airbnb/Lottie.
Add the dependency in your common module's commonMain source set:
implementation("io.github.ismai117:kottie:latest_version")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
// Load composition
val composition = rememberKottieComposition(
KottieCompositionSpec.Url("https://example.com/animation.json")
)
// Animate
val animationState by animateKottieCompositionAsState(
composition = composition,
iterations = Kottie.IterateForever
)
// Display
KottieAnimation(
composition = composition,
progress = { animationState.progress },
modifier = Modifier.size(300.dp)
)Load animations from different sources using KottieCompositionSpec:
// From URL
val composition = rememberKottieComposition(
KottieCompositionSpec.Url("https://example.com/animation.json")
)
// From file (using Compose Resources)
var json by remember { mutableStateOf("") }
LaunchedEffect(Unit) {
json = Res.readBytes("files/animation.json").decodeToString()
}
val composition = rememberKottieComposition(KottieCompositionSpec.File(json))
// From JSON string
val composition = rememberKottieComposition(
KottieCompositionSpec.JsonString("""{"v":"5.7.4",...}""")
)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}")
}
}var isPlaying by remember { mutableStateOf(true) }
val animationState by animateKottieCompositionAsState(
composition = composition,
isPlaying = isPlaying
)
Button(onClick = { isPlaying = !isPlaying }) {
Text(if (isPlaying) "Pause" else "Play")
}val animationState by animateKottieCompositionAsState(
composition = composition,
speed = 1.5f
)// Play 3 times
val animationState by animateKottieCompositionAsState(
composition = composition,
iterations = 3
)
// Loop forever
val animationState by animateKottieCompositionAsState(
composition = composition,
iterations = Kottie.IterateForever
)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}")
}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://www.apache.org/licenses/LICENSE-2.0
Contributions are welcome! Feel free to open issues or submit pull requests.
