Animate TextView to increase integer and stop at some point?

前端 未结 5 1801
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 21:50

I have a TextView showing integer value. Integer value is transferred from previous activity, and I want to add nice animation. I want to if for example int value is 73, I w

5条回答
  •  忘掉有多难
    2020-12-12 22:31

    This is a Kotlin code for incrementing from initial value to final value over a duration of time. Here I have used duration of 5 sec.

     fun animateTextView(initialValue: Int, finalValue: Int, textView: TextView) {
            val valueAnimator = ValueAnimator.ofInt(initialValue, finalValue)
            valueAnimator.duration = 5000 // 5 sec
            valueAnimator.addUpdateListener { valueAnimator -> textView.text = valueAnimator.animatedValue.toString() }
            valueAnimator.start()
        }
    

    use this code in Utilities and call the method accordingly with required parameters.

提交回复
热议问题