java - 如何在TextView中制作计数动画

标签 java android textview sharedpreferences

所以在我的游戏中有一个分数标签会在游戏结束时显示

这是我的标签得分的例子

这是代码:

    highscoretext = (TextView)findViewById(R.id.bt);
    highscoretext.setText("0");

    currentscore = (TextView)findViewById(R.id.ct);
    currentscore.setText(String.valueOf(gamescore));

这就是我如何保存将在 bestscore 中显示的分数

           SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
           SharedPreferences.Editor editor = pref.edit();
           editor.putInt("totalScore", gamescore);

            if (gamescore > highscore){
                highscoretext.setText(String.valueOf(gamescore));
                editor.putInt("highscore", gamescore);
                editor.commit();    

我想像这样为我的 TextView 制作动画

所以当tab得分显示时,得分会从0开始计算到游戏中的当前得分,例如:10

当score停止计数时,如果score > best score,则best score中的值发生变化

谁能帮帮我?

最佳答案

对于API >11,我建议你使用ValueAnimator :

ValueAnimator animator = new ValueAnimator();
animator.setObjectValues(0, count);// here you set the range, from 0 to "count" value
animator.addUpdateListener(new AnimatorUpdateListener() {
    public void onAnimationUpdate(ValueAnimator animation) {
     highscoretext.setText(String.valueOf(animation.getAnimatedValue()));
    }
});
animator.setDuration(1000); // here you set the duration of the anim
animator.start();

关于java - 如何在TextView中制作计数动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33839865/

相关文章:

java - 捕获现有的构建

android - 使用 Android Studio/AVD 模拟器开发 "system app"

Android:如何设置可绘制对象可见

java - Android TextView 在一段后切断

android - 我可以为 TextView 中的链接设置文本颜色吗?

java - Documentbuilder.parse() 的问题

java - 在 Eclipse 中找不到 java 虚拟机

java - input.nextInt() 究竟是如何工作的?

java - 处理父类(super class)型的所有子类型

使用 TOP 和 BOTTOM 重力时 Android DrawerLayout 失败