diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java b/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java index 4240035028..ca8d13b8ee 100644 --- a/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java +++ b/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java @@ -553,6 +553,10 @@ public void setComposition(@NonNull LottieComposition composition) { // We can avoid re-setting the drawable, and invalidating the view, since the composition // hasn't changed. return; + } else if (!isNewComposition) { + // The current drawable isn't lottieDrawable but the drawable already has the right composition. + setImageDrawable(null); + setImageDrawable(lottieDrawable); } // This is needed to makes sure that the animation is properly played/paused for the current visibility state. diff --git a/sample/src/androidTest/java/com/airbnb/lottie/samples/LottieAnimationViewTest.kt b/sample/src/androidTest/java/com/airbnb/lottie/samples/LottieAnimationViewTest.kt index 976bc4afab..a10e4a7b61 100644 --- a/sample/src/androidTest/java/com/airbnb/lottie/samples/LottieAnimationViewTest.kt +++ b/sample/src/androidTest/java/com/airbnb/lottie/samples/LottieAnimationViewTest.kt @@ -1,7 +1,11 @@ package com.airbnb.lottie.samples +import android.graphics.Color +import android.graphics.drawable.ColorDrawable import androidx.fragment.app.Fragment +import androidx.fragment.app.testing.FragmentScenario import androidx.fragment.app.testing.launchFragmentInContainer +import androidx.lifecycle.Lifecycle import androidx.test.espresso.Espresso.onView import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.matcher.ViewMatchers @@ -9,6 +13,10 @@ import androidx.test.espresso.matcher.ViewMatchers.isDisplayed import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.LargeTest +import com.airbnb.lottie.LottieAnimationView +import com.airbnb.lottie.LottieCompositionFactory +import com.airbnb.lottie.LottieDrawable +import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith @@ -21,4 +29,21 @@ class LottieAnimationViewTest { launchFragmentInContainer() onView(withId(R.id.animation_view)).check(matches(isDisplayed())) } + + @Test + fun testCanSetAnAnimationAndChangeItBack() { + class TestFragment : Fragment(R.layout.lottie_activity_main) + val scenario = launchFragmentInContainer() + scenario.moveToState(Lifecycle.State.RESUMED) + scenario.onFragment { fragment -> + val composition = LottieCompositionFactory.fromRawResSync(fragment.requireContext(), R.raw.hamburger_arrow).value!! + val view = fragment.requireView().findViewById(R.id.animation_view) + view.setComposition(composition) + assertTrue(view.drawable is LottieDrawable) + view.setImageDrawable(ColorDrawable(Color.GREEN)) + assertTrue(view.drawable is ColorDrawable) + view.setComposition(composition) + assertTrue(view.drawable is LottieDrawable) + } + } }