airbnb / lottie-android

Render After Effects animations natively on Android and iOS, Web, and React Native
http://airbnb.io/lottie/
Apache License 2.0
34.92k stars 5.4k forks source link

LottieAnimationView.duration returns 0 #2481

Closed u13 closed 4 months ago

u13 commented 5 months ago
binding.lottieAnimation.setAnimation(fileName)
val animationDuration = binding.lottieAnimation.duration

// animationDuration == 0

If we go inside of LottieAnimationView, we see that:

  public long getDuration() {
    LottieComposition composition = getComposition(); // composition == null
    return composition != null ? (long) composition.getDuration() : 0;
  }

and composition == null because:

  @Nullable public LottieComposition getComposition() {
    return getDrawable() == lottieDrawable ? lottieDrawable.getComposition() : null; // getDrawable() == null
  }

Most probably this bug appeared after merging this PR #2468

gpeal commented 5 months ago

Did you set your own drawable on LottieAnimationView (like your own resource or something)?

u13 commented 5 months ago

Did you set your own drawable on LottieAnimationView (like your own resource or something)?

No, it's just:

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/lottie_animation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
gpeal commented 4 months ago

Lottie parses compositions synchronously. If you are setting an animation for the first time, the duration won't be synchronously available. I just did a test where I logged the duration in the callback for LottieAnimationView.addLottieOnCompositionLoadedListener and it returned the correct duration.