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.95k stars 5.4k forks source link

[Bug] Afterimage Persists in Android #2460

Closed MoonJR closed 6 months ago

MoonJR commented 7 months ago

LottieAnimationView's Parent View After Applying Alpha Animation

When changing the visibility of the LottieAnimationView during the animation of the parent view, the animation pauses, but the view itself continues to linger on the screen as an afterimage.

When the visibility is set to 'gone,' and a layout inspection is performed, the view appears to be removed, but an afterimage persists.

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">

    <FrameLayout
        android:id="@+id/lottieParentView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.airbnb.lottie.LottieAnimationView
            android:id="@+id/lottieView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:lottie_autoPlay="true"
            app:lottie_loop="true"
            app:lottie_rawRes="@raw/loading_spinner_w_30" />
    </FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

code

         binding.lottieParentView.animation = AlphaAnimation(0.5f, 1f)
            .apply {
                // after 10second animation done
                duration = 10000
                setAnimationListener(object : AnimationListener {
                    override fun onAnimationStart(animation: Animation?) {
                    }

                    override fun onAnimationEnd(animation: Animation?) {
                        toast("End Alpha Animation")
                    }

                    override fun onAnimationRepeat(animation: Animation?) {
                    }
                })
            }

        binding.lottieParentView.postDelayed(3000) {
            binding.lottieParentView.isVisible = false
            toast("Exec lottieParentView visible gone")
        }
example layout inspect
https://github.com/airbnb/lottie-android/assets/7312966/9153ae1d-3cdc-4e89-959d-304ebc4c6288 IMG_1074
gpeal commented 6 months ago

This behavior may seem surprising but it has nothing to do with Lottie,. If you set a view to gone but it has an animation, Android will add it to a list of disappearing views that will be removed only when the animation finishes (source).

To validate this, I copied your code and could repro what you see. I then replaced the Lottie animation with a plain View that was a red square and the exact same thing happened.