Anil Gorthy
Anil Gorthy

Reputation: 899

Loop Lottie animation from a certain frame

I'm trying to loop animation between certain frames but I don't see the animation at all and if I remove the LottieCompositionFactory....addListener code and just use the xml file, the animation does display but, I want to run the animation between certain frames:

class OnboardingFragment : Fragment() {
  ...
  ...
  private var myComposition: LottieComposition? = null
  ...
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

    LottieCompositionFactory.fromRawRes(context, R.raw.my_animation)
                .addListener {
                    myComposition = it
                    Log.d(TAG, "Is animation displaying: $myComposition") //does print the LottieComposition
                    with(lottieAnimation) {
                        alpha = 0F
                        visibility = View.VISIBLE
                        myComposition?.let { it1 -> setComposition(it1) }
                        repeatCount = ValueAnimator.INFINITE
                        setMinAndMaxFrame(29, 219)
                        playAnimation()
                    }
                }
                .addFailureListener {
                    Log.e(TAG, "Error displaying animation: ${it.message}")
                }

and the layout file:

<androidx.constraintlayout.widget.ConstraintLayout
...
...
<com.airbnb.lottie.LottieAnimationView
        android:id="@+id/lottieAnimation"
        android:layout_width="170dp"
        android:layout_height="146dp"
        android:layout_marginBottom="34dp"
        android:scaleType="fitStart"
        app:layout_constraintBottom_toTopOf="@+id/title"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/topGuide"
        app:lottie_autoPlay="true"
        app:lottie_loop="true"/>

I tried adding app:lottie_rawRes="@raw/my_animation" in the above layout but that didn't change anything.

Upvotes: 2

Views: 5637

Answers (1)

Anil Gorthy
Anil Gorthy

Reputation: 899

So, I went with this approach even though I don't like that I'm using android.animation.Animator, android.animation.ValueAnimator along with LottieAnimation:

  private fun setupAnimation(lottieAnimation: LottieAnimationView) {
        lottieAnimation.addAnimatorListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(anim: Animator?) {
            }

            override fun onAnimationEnd(anim: Animator?) {
                lottieAnimation.removeAllAnimatorListeners()
                lottieAnimation.repeatCount = ValueAnimator.INFINITE
                lottieAnimation.setMinAndMaxFrame(25, 147)
                lottieAnimation.playAnimation()
            }

            override fun onAnimationRepeat(anim: Animator?) {
            }

            override fun onAnimationCancel(anim: Animator?) {
            }
        })
    }

and removed app:lottie_loop="true" from the layout

Upvotes: 7

Related Questions