kotlin-multiplatformlottiecompose-multiplatformcompose-desktopjetbrains-compose

How to include and play a Lottie animation in Compose Multiplatform?


How can I embed After Effects animations exported with Bodymovin extension as Lottie JSON format in a Compose Multiplatform app (Desktop/Windows/Linux/macOS, Android, iOS, Web/JS/WASM)? Is there a Lottie library for CMP as well?


Solution

  • There is a library called Compottie for Compose Multiplatform.

    implementation("io.github.alexzhirkevich:compottie:2.0.0")
    

    Just put your animation JSON file in src/main/composeResources/files/.

    Here is an example minimal code to show/render/play that animation file:

    val composition by rememberLottieComposition {
        val animationBytes = Res.readBytes("files/my-animation.json")
        LottieCompositionSpec.JsonString(animationBytes.decodeToString())
    }
    val progress by animateLottieCompositionAsState(composition)
    Image(
        contentDescription = "Lottie animation",
        painter = rememberLottiePainter(
            composition = composition,
            progress = { progress }
        )
    )