Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make count animation in TextView

So in my game there is score tab that will show in gameover

This is example for my tab score

This is the code :

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

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

And this is how i save the score that will display in 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();    

And i wonder to make an animation to my TextView like this

So when the tab score display, the score will count from 0 to Current score that get in the game, example : 10

and when score stop count, if the score > best score, the value in best score changed

Can anyone help me?

like image 890
Ricci Avatar asked Dec 24 '22 12:12

Ricci


1 Answers

For API >11, i suggest you to use 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();
like image 158
Rami Avatar answered Jan 08 '23 22:01

Rami