Skip to content

Commit

Permalink
Reset LottieDrawable when setting the same composition a second time (#…
Browse files Browse the repository at this point in the history
…1703)

Fixes #1696
  • Loading branch information
gpeal authored Dec 24, 2020
1 parent 937148b commit 5ebcdcf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
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
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

Expand All @@ -21,4 +29,21 @@ class LottieAnimationViewTest {
launchFragmentInContainer<TestFragment>()
onView(withId(R.id.animation_view)).check(matches(isDisplayed()))
}

@Test
fun testCanSetAnAnimationAndChangeItBack() {
class TestFragment : Fragment(R.layout.lottie_activity_main)
val scenario = launchFragmentInContainer<TestFragment>()
scenario.moveToState(Lifecycle.State.RESUMED)
scenario.onFragment { fragment ->
val composition = LottieCompositionFactory.fromRawResSync(fragment.requireContext(), R.raw.hamburger_arrow).value!!
val view = fragment.requireView().findViewById<LottieAnimationView>(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)
}
}
}

0 comments on commit 5ebcdcf

Please sign in to comment.