【发布时间】:2021-12-28 12:02:55
【问题描述】:
我可以在 1 秒内使用 textview 制作分数变量增加的动画吗?
【问题讨论】:
标签: android-studio animation textview
我可以在 1 秒内使用 textview 制作分数变量增加的动画吗?
【问题讨论】:
标签: android-studio animation textview
我强烈建议您使用 this question 签出,对于不同的情况,有多种不同的方法。
你可以试试:
//You can animate from a specified starting point till the final score
val valueAnimator = ValueAnimator.ofInt(initialScore, finalScore)
//Here you set the duration of the animation in milliseconds
valueAnimator.duration = 1000
valueAnimator.addUpdateListener { valueAnimator ->
//The textview gets updates as the value animator's value updates
textView.text = valueAnimator.animatedValue.toString()
}
//Here you just start the animation
valueAnimator.start()
这也可以重新用于它自己的方法;)
【讨论】:
另一种效果很好的方法是使用 new Handler(Looper.getMainLooper()).postDelayed(new Runnable() ... 例如:
private void wonBlockIncreaseScore() {
int oldScore = score;
score = score + 50;
int timeOverWhichAnimationShouldRun = 1000; //ms
int equalGap = (timeOverWhichAnimationShouldRun / (score-oldScore));
//equal gap...
for (int i = oldScore; i < score; i++) {
int finalI = i;
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
scoreTextView.setText(String.valueOf(finalI+1));
}
}, (equalGap * i));
}
【讨论】: