4

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?

Rami
  • 7,879
  • 12
  • 36
  • 66
Ricci
  • 217
  • 5
  • 21

2 Answers2

10

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();
Rami
  • 7,879
  • 12
  • 36
  • 66
0

It should be fairly simple to implement using a timer of some sort. Every time the timer hits, you increment the score and update the display unless you've reached the final score.

This can be done using Handlers (http://developer.android.com/reference/android/os/Handler.html) or java.util.Timer (http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html), though the latter approach may not be preferable for Android development.

(Source: How to set a timer in android)

Community
  • 1
  • 1
Timothy
  • 26
  • 6