How to get total number of frames available in the Lottie Animation?
Asked Answered
I

2

8

I'm using random Lottie animation in my android app. I need to know the total number of frames used in the Lottie animation. depending upon the total frames, I want to loop the animation from 1st frame to the certain frame. for eg. If the Lottie file contains 60 frames then I want to perform the animation from 1 to any number < 60.

I'm using below dependency

implementation 'com.airbnb.android:lottie:3.4.2'
Incommunicative answered 24/9, 2020 at 6:35 Comment(2)
You can see the number of frames on the lottie page while you are viewing it.Grapher
the Lottie file is not downloaded from Lottie's website. It was sent by the design team.Incommunicative
Q
21

You can see the first and last frame counts in the json file. ip=first frame and op: last frame.

example image

Quianaquibble answered 15/12, 2020 at 20:50 Comment(1)
+1. Note that in some Lottie files there are more than one ip and op. In this case the first ip and op is the frames of all file.Blighter
C
0

Use addLottieOnCompositionLoadedListener to get your lottie's Composition.

binding.myLottie.addLottieOnCompositionLoadedListener { composition ->
    val startFrame = composition.startFrame
    val endFrame = composition.endFrame
    //will log start/end frame values
    Log.d(TAG, "startFrame : $startFrame, endFrame : $endFrame") 
}

Then to specific part of an animation use setMinAndMaxFrame()

binding.myLottie.addLottieOnCompositionLoadedListener { composition ->
    val startFrame = composition.startFrame
    val endFrame = composition.endFrame
    if (maxFrame < endFrame) {
        binding.myLottie.setMinAndMaxFrame(1, maxFrame)
    }
}

read here more about looping a specific part of an animation.

Clamber answered 24/9, 2020 at 6:50 Comment(3)
getMaxFrame() returns 0.0Incommunicative
it is giving me an error LottieComposition.getStartFrame can only be called from within the same library (com.airbnb.android:lottie)Incommunicative
@VishalNaikawadi Can you post your code and xml of this part?Clamber

© 2022 - 2024 — McMap. All rights reserved.