androidtextviewandroid-animationanimator

Sometimes text disappears in TextView during scale animation on some devices


I want to start bounce scale animation of textview every activity's onResume. I noticed that sometimes text doesnt'show during textview scaling animation. I see only background of that textview. This bug appears only on some devices: few Xiaomi models, one Samsung, one Pixel. Works good on emulator. I am using Animator. I tried Animation class and bug still exists.How can I fix this?

My Activity code:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onResume() {
        super.onResume()
        playAnimation()
    }

    private fun playAnimation() {
        val textView = findViewById<TextView>(R.id.textView)
        textView.scaleX = 0f
        textView.scaleY = 0f
        val badgeAnimator = ValueAnimator.ofFloat(0f, 1f
        ).apply {
            duration = 800L
            interpolator = BounceInterpolator()
            addUpdateListener { animation ->
                val value = animation.animatedValue as Float
                textView.scaleX = value
                textView.scaleY = value
            }
        }
        val animatorSet = AnimatorSet()
        animatorSet.apply {
            startDelay = 1500L
            play(badgeAnimator)
            start()
        }
    }
}

Activity 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textColor="@color/white"
        android:paddingHorizontal="10dp"
        android:background="@drawable/bg_badge_count_v2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Demonstration video of this issue. enter image description here


Solution

  • I found that if i valueanimator with start value 0.01f instead of 0f bug dissappears. That works with animation class too. I think that something in android system clears text in TextView when scaleX and scaleY are zero.

    So 0.01f I think is ok, because it is not visible for user and looks the same as 0f