Lottie for Android

repository·master·Indexed 12 days ago

https://github.com/airbnb/lottie-android

A mobile library that parses Adobe After Effects animations exported as JSON via Bodymovin and renders them natively on Android devices. Includes support for Jetpack Compose via Lottie-Compose, .tgs files (v6.4.1+), dynamic properties, and custom font maps. Features include LottiePainter for Compose rendering and the ability to modify animation properties at runtime using rememberLottieDynamicProperty.

Tokens
2.3K
Snippets
6
Records
15
Agent score
98%

What's inside Lottie

  1. Migrate LottieAnimation progress usage in Jetpack Compose

    master

    Starting from version 5.2.0, LottieAnimation requires the progress parameter to be passed as a lambda () -> Float instead of a raw Float. This change allows Lottie to redraw without triggering a full recomposition on every progress update.

    If you are using the old API, it is deprecated and will be removed in a future release. You should update your calls as follows:

    // Old way (deprecated since 5.2.0)
    LottieAnimation(composition, progress)
    
    // New way (recommended)
    LottieAnimation(composition, { progress })
    
    // Or using named arguments
    LottieAnimation(
      composition = composition,
      progress = { progress }
    )
  2. Install Lottie for Android via Gradle

    master

    Lottie for Android uses Gradle as its only supported build configuration. To use Lottie in your Android project, add the dependency to your build.gradle file.

    Note: Lottie version 2.8.0 and above requires your project to have migrated to androidx.

    dependencies {
      implementation 'com.airbnb.android:lottie:$lottieVersion'
    }
  3. Handle custom images in Lottie animations

    master

    In recent versions, images are loaded from assets or decoded from base64 strings embedded in the JSON file on an IO thread pool during parsing.

    If you need to supply your own bitmaps manually, you should set them directly on the composition to avoid performing IO tasks on the animation hot path (the main thread). Use the following pattern:

    composition.images["your_image_id"].bitmap = yourBitmap
  4. How dynamic properties work in Lottie Compose

    master

    In Lottie Compose, dynamic properties allow you to modify animation elements (like colors, opacity, or scale) at runtime without changing the original JSON file.

    1. Definition: You define properties using rememberLottieDynamicProperty (for single items) or rememberLottieDynamicProperties (for a collection).
    2. KeyPaths: You target specific elements using KeyPath strings (e.g., "Layer Name", "Shape Name").
    3. Value Calculation:
      • Static: A fixed value is applied.
      • Dynamic: A callback receives LottieFrameInfo, which contains information about the current frame (like progress), allowing for frame-aware logic.
    4. Lifecycle: The properties are wrapped in remember blocks to ensure they are stable across recompositions. The internal addTo and removeFrom mechanisms (used by the library's integration) manage the registration of these properties as LottieValueCallbacks on the LottieDrawable instance.
  5. Create a LottiePainter with rememberLottiePainter

    master

    In Jetpack Compose, use the rememberLottiePainter composable to create and manage a LottiePainter. This painter is responsible for rendering a LottieComposition and allows you to control animation properties like progress, renderMode, and dynamicProperties within the Compose lifecycle.

    Parameters

    ParameterTypeDefaultDescription
    compositionLottieComposition?nullThe Lottie composition to render.
    progressFloat0fThe current animation progress (0.0 to 1.0).
    outlineMasksAndMattesBooleanfalseWhether to enable outline masks and mattes.
    applyOpacityToLayersBooleanfalseWhether to apply opacity to layers.
    enableMergePathsBooleanfalseWhether to enable merge paths.
    renderModeRenderModeRenderMode.AUTOMATICThe rendering mode to use.
    maintainOriginalImageBoundsBooleanfalseWhether to maintain original image bounds.
    dynamicPropertiesLottieDynamicProperties?nullDynamic properties to apply to the animation.
    clipToCompositionBoundsBooleantrueWhether to clip the drawing to the composition bounds.
    clipTextToBoundingBoxBooleanfalseWhether to clip text to its bounding box.
    fontMapMap<String, Typeface>?nullA map of font names to Typeface objects.
    asyncUpdatesAsyncUpdatesAsyncUpdates.AUTOMATICHow asynchronous updates are handled.
    val painter = rememberLottiePainter(
        composition = myComposition,
        progress = progressState,
        renderMode = RenderMode.HARDWARE
    )
    
    Image(painter = painter, contentDescription = null)