2012-04-12 8 views
0

私はアンドロイドでゲームアプリケーションを開発しています。私はスコアカードの実装に打たれました。 数値が連続的に動き回っているので、スコアをテキストビューに表示したい。 たとえば、ユーザーが1つの障害物を横切って、その障害物のスコアが200であるとします。スコアカードの数値は0から200までスムーズにカウントし、200で停止します。 papiJumpのスコアカードでこのタイプのアニメーションを見ました。テキストビューで数字をカウントするアニメーション

私をご案内ください。

+0

Iは1から200まで数えるとのTextViewに表示するハンドラを使用して考えています。私の考えは正しいですか? –

+0

1つのことに注意してください。あなたがUIスレッドを手にしすぎるとANRされます。したがって、AsyncTaskはより適切かもしれません。 – njzk2

答えて

0
public void animateTextView(int initialValue, int finalValue, final TextView textview) 
{ 
    DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator(0.8f); 
     int start = Math.min(initialValue, finalValue); 
     int end = Math.max(initialValue, finalValue); 
     int difference = Math.abs(finalValue - initialValue); 
     Handler handler = new Handler(); 
     for (int count = start; count <= end; count++) { 
      int time = Math.round(decelerateInterpolator.getInterpolation((((float) count)/difference)) * 100) * count; 
      final int finalCount = ((initialValue > finalValue) ? initialValue - count : count); 
      handler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        textview.setText(finalCount + ""); 
       } 
      }, time); 
     } 
    } 
+1

このコードスニペットは問題を解決するかもしれませんが、[説明を含む](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)は本当に役立ちますあなたの投稿の質を向上させる。将来読者の質問に答えていることを覚えておいてください。そうした人々はあなたのコード提案の理由を知らないかもしれません。 – jmattheis

+0

初期設定値= 0最終値は移動後の移動に基づいています。初期値=最後に割り当てられた値です。最終値は移動位置に基づいて変更できます。 –

0
ValueAnimator animator = ValueAnimator.ofInt(int start, int end); 
       animator.setDuration(2000); 
       animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
        public void onAnimationUpdate(ValueAnimator animation) { 
         textView.setText(animation.getAnimatedValue().toString()); 
        } 
       }); 

       animator.start(); 
+0

テキストの処理方法やテキストビューについての説明も追加します。 – androidnoobdev

+0

この関数は非常に使いやすく、textViewの整数値を0からアニメーションである値に増加します。 開始日と終了日の範囲を0〜300に設定し、継続時間の値を2000などに設定します。したがって、値の増分処理は2秒で完了します。 詳細https://developer.android.com/reference/android/animation/ValueAnimator.html –

+0

答えについてのjmattheisのコメントを読んでください。 – androidnoobdev

関連する問題