李国兵
李国兵

Reputation: 127

How to control the update speed of ValueAnimator

in 10 seconds, from 1 to 10

The normal calculation result is

1,
1.1,
1.2,
1.3
...
9.7,
9.8,
9.9,
10

The result I want is

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Update every second

How to achieve it?

Thanks ---------------------example------------------------

        ValueAnimator.setFrameDelay(1000)
        val animator = ValueAnimator.ofInt(1, 5)
        animator.addUpdateListener { animation ->
            Log.d("TTT", "${animation.animatedValue}")
        }
        animator.duration = 5000
        animator.start()

Log:

2020-10-14 03:40:18.883 28503-28503/XXXXX D/TTT: 1
2020-10-14 03:40:18.900 28503-28503/XXXXX D/TTT: 1
2020-10-14 03:40:18.916 28503-28503/XXXXX D/TTT: 1
2020-10-14 03:40:18.933 28503-28503/XXXXX D/TTT: 1
2020-10-14 03:40:19.131 28503-28503/XXXXX D/TTT: 1
2020-10-14 03:40:19.147 28503-28503/XXXXX D/TTT: 1
2020-10-14 03:40:19.478 28503-28503/XXXXX D/TTT: 1
2020-10-14 03:40:19.494 28503-28503/XXXXX D/TTT: 1
2020-10-14 03:40:20.073 28503-28503/XXXXX D/TTT: 1
2020-10-14 03:40:20.089 28503-28503/XXXXX D/TTT: 2
2020-10-14 03:40:20.319 28503-28503/XXXXX D/TTT: 2
2020-10-14 03:40:20.336 28503-28503/XXXXX D/TTT: 2
2020-10-14 03:40:20.469 28503-28503/XXXXX D/TTT: 2
2020-10-14 03:40:20.485 28503-28503/XXXXX D/TTT: 2
2020-10-14 03:40:20.584 28503-28503/XXXXX D/TTT: 2
2020-10-14 03:40:20.600 28503-28503/XXXXX D/TTT: 2
2020-10-14 03:40:20.914 28503-28503/XXXXX D/TTT: 2
2020-10-14 03:40:20.931 28503-28503/XXXXX D/TTT: 3
2020-10-14 03:40:21.475 28503-28503/XXXXX D/TTT: 3
2020-10-14 03:40:21.491 28503-28503/XXXXX D/TTT: 3
2020-10-14 03:40:21.756 28503-28503/XXXXX D/TTT: 3
2020-10-14 03:40:21.772 28503-28503/XXXXX D/TTT: 4
2020-10-14 03:40:22.465 28503-28503/XXXXX D/TTT: 4
2020-10-14 03:40:22.481 28503-28503/XXXXX D/TTT: 4
2020-10-14 03:40:23.406 28503-28503/XXXXX D/TTT: 4
2020-10-14 03:40:23.422 28503-28503/XXXXX D/TTT: 5

I need like this:

2020-10-14 03:40:21.772 28503-28503/XXXXX D/TTT: 1
2020-10-14 03:40:22.465 28503-28503/XXXXX D/TTT: 2
2020-10-14 03:40:22.481 28503-28503/XXXXX D/TTT: 3
2020-10-14 03:40:23.406 28503-28503/XXXXX D/TTT: 4
2020-10-14 03:40:23.422 28503-28503/XXXXX D/TTT: 5

Upvotes: 0

Views: 745

Answers (2)

Yeldar Nurpeissov
Yeldar Nurpeissov

Reputation: 2296

You can use CountDownTimer:

val timer = object : CountDownTimer(5000L, 1000L) {
    var counter = 1
    override fun onTick(millisUntilFinished: Long) {
        Log.d("TTT", "${counter++}")
    }

    override fun onFinish() {}
}

timer.start()

Upvotes: 0

Perraco
Perraco

Reputation: 17360

The ValueAniamtor update rate can't be adjusted, as it is used for animations and consequently gets refreshed at a very high rate.

Instead, you can use the Timer class, scheduling at a specific rate, such as next:

Timer().scheduleAtFixedRate(object : TimerTask() {
        var value = 1 // Initial value

        override fun run() {
            value++

            Log.d("TTT", "$value")

            if (value > 10)  // Exit after 10
                this.cancel()
        }
    }, 0, 500) // Update every 500 milliseconds

If for example, you need 10 iterations with a 5 seconds duration, then set the update rate to 500, (Formula: 5000 milliseconds / 10 iterations)

Upvotes: 1

Related Questions