Skip to content

Commit

Permalink
Remove LottieAnimationView.composition so it doesn't get out of sync (#…
Browse files Browse the repository at this point in the history
…2468)

Fixes #2467
  • Loading branch information
gpeal committed Feb 27, 2024
1 parent fe412fa commit 16e95c4
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
Expand Up @@ -43,7 +43,7 @@

/**
* This view will load, deserialize, and display an After Effects animation exported with
* bodymovin (https://github.com/bodymovin/bodymovin).
* bodymovin (<a href="https://github.com/airbnb/lottie-web">github.com/airbnb/lottie-web</a>).
* <p>
* You may set the animation in one of two ways:
* 1) Attrs: {@link R.styleable#LottieAnimationView_lottie_fileName}
Expand Down Expand Up @@ -139,10 +139,6 @@ public WeakFailureListener(LottieAnimationView target) {
private final Set<LottieOnCompositionLoadedListener> lottieOnCompositionLoadedListeners = new HashSet<>();

@Nullable private LottieTask<LottieComposition> compositionTask;
/**
* Can be null because it is created async
*/
@Nullable private LottieComposition composition;

public LottieAnimationView(Context context) {
super(context);
Expand Down Expand Up @@ -269,16 +265,22 @@ private void init(@Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
}

@Override public void setImageResource(int resId) {
this.animationResId = 0;
animationName = null;
cancelLoaderTask();
super.setImageResource(resId);
}

@Override public void setImageDrawable(Drawable drawable) {
this.animationResId = 0;
animationName = null;
cancelLoaderTask();
super.setImageDrawable(drawable);
}

@Override public void setImageBitmap(Bitmap bm) {
this.animationResId = 0;
animationName = null;
cancelLoaderTask();
super.setImageBitmap(bm);
}
Expand Down Expand Up @@ -607,7 +609,8 @@ public void setFallbackResource(@DrawableRes int fallbackResource) {

private void setCompositionTask(LottieTask<LottieComposition> compositionTask) {
LottieResult<LottieComposition> result = compositionTask.getResult();
if (result != null && result.getValue() == composition) {
LottieDrawable lottieDrawable = this.lottieDrawable;
if (result != null && lottieDrawable == getDrawable() && lottieDrawable.getComposition() == result.getValue()) {
return;
}
userActionsTaken.add(UserActionTaken.SET_ANIMATION);
Expand Down Expand Up @@ -636,7 +639,6 @@ public void setComposition(@NonNull LottieComposition composition) {
}
lottieDrawable.setCallback(this);

this.composition = composition;
ignoreUnschedule = true;
boolean isNewComposition = lottieDrawable.setComposition(composition);
ignoreUnschedule = false;
Expand All @@ -663,7 +665,7 @@ public void setComposition(@NonNull LottieComposition composition) {
}

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

/**
Expand Down Expand Up @@ -930,7 +932,7 @@ public boolean isAnimating() {
* Be wary if you are using many images, however. Lottie is designed to work with vector shapes
* from After Effects. If your images look like they could be represented with vector shapes,
* see if it is possible to convert them to shape layers and re-export your animation. Check
* the documentation at http://airbnb.io/lottie for more information about importing shapes from
* the documentation at <a href="http://airbnb.io/lottie">airbnb.io/lottie</a> for more information about importing shapes from
* Sketch or Illustrator to avoid this.
*/
public void setImageAssetsFolder(String imageAssetsFolder) {
Expand Down Expand Up @@ -981,7 +983,7 @@ public Bitmap updateBitmap(String id, @Nullable Bitmap bitmap) {
* Be wary if you are using many images, however. Lottie is designed to work with vector shapes
* from After Effects. If your images look like they could be represented with vector shapes,
* see if it is possible to convert them to shape layers and re-export your animation. Check
* the documentation at http://airbnb.io/lottie for more information about importing shapes from
* the documentation at <a href="http://airbnb.io/lottie">airbnb.io/lottie</a> for more information about importing shapes from
* Sketch or Illustrator to avoid this.
*/
public void setImageAssetDelegate(ImageAssetDelegate assetDelegate) {
Expand Down Expand Up @@ -1122,6 +1124,7 @@ private void setProgressInternal(
}

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

Expand All @@ -1135,7 +1138,6 @@ public PerformanceTracker getPerformanceTracker() {
}

private void clearComposition() {
composition = null;
lottieDrawable.clearComposition();
}

Expand Down Expand Up @@ -1247,7 +1249,7 @@ public void disableExtraScaleModeInFitXY() {
}

public boolean addLottieOnCompositionLoadedListener(@NonNull LottieOnCompositionLoadedListener lottieOnCompositionLoadedListener) {
LottieComposition composition = this.composition;
LottieComposition composition = getComposition();
if (composition != null) {
lottieOnCompositionLoadedListener.onCompositionLoaded(composition);
}
Expand Down

0 comments on commit 16e95c4

Please sign in to comment.