【发布时间】: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