Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset LottieDrawable when setting the same composition a second time #1703

Merged
merged 1 commit into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}