【问题标题】:Sometimes text disappears in TextView during scale animation on some devices在某些设备上的缩放动画期间,有时文本会在 TextView 中消失
【发布时间】:2021-05-12 16:48:53
【问题描述】:

我想在每个活动的 onResume 中启动 textview 的反弹比例动画。我注意到有时文本在 textview 缩放动画期间不会显示。我只看到那个文本视图的背景。此错误仅出现在某些设备上:少数小米型号,一款三星,一款 Pixel。在模拟器上运行良好。我正在使用Animator。我尝试了Animation 类,但错误仍然存​​在。我该如何解决这个问题?

我的活动代码:

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()
        }
    }
}

活动 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>

此问题的演示视频。

【问题讨论】:

    标签: android textview android-animation animator


    【解决方案1】:

    我发现如果我 valueanimator 的起始值为 0.01f 而不是 0f 错误就会消失。这也适用于动画类。我认为当 scaleX 和 scaleY 为零时,android 系统中的某些内容会清除 TextView 中的文本。

    所以 0.01f 我认为是可以的,因为它对用户不可见并且看起来与 0f 相同

    【讨论】:

      猜你喜欢
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 2018-11-13
      • 1970-01-01
      相关资源
      最近更新 更多