2012-01-31 7 views
7

私はカウントダウンしたいTextViewを持っています(3 ... 2 ... 1 ...何かが起こる)。Android Simple TextView Animation

もう少し面白いように、私は各桁を完全な不透明度から開始し、透明にフェードアウトさせたいと思います。

これを行う簡単な方法はありますか?

答えて

11

はこのような何かを試してみてください:

private void countDown(final TextView tv, final count) { 
    if (count == 0) { 
    tv.setText(""); //Note: the TextView will be visible again here. 
    return; 
    } 
    tv.setText(count); 
    AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f); 
    animation.setDuration(1000); 
    animation.setAnimationListener(new AnimationListener() { 
    public void onAnimationEnd(Animation anim) { 
     countDown(tv, count - 1); 
    } 
    ... //implement the other two methods 
    }); 
    tv.startAnimation(animation); 
} 

私はちょうどそれを入力した、そうであるとして、それがコンパイルされない場合があります。

+1

'tv.setText(count);を' tv.setText(String.valueOf(count)); 'に置き換え、コードは正常に動作します –

2

CountDownAnimationをご覧ください。

私はまず@dmonソリューションを試しましたが、すべてのアニメーションは前のアニメーションの終わりに開始するので、いくつかの呼び出しの後に遅延が生じます。

したがって、HandlerpostDelayed機能を使用するCountDownAnimationクラスを実装しました。デフォルトでは、アルファアニメーションが使用されますが、任意のアニメーションを設定できます。プロジェクトhereをダウンロードできます。

4

私はこのために、より従来型のAndroidスタイルのアニメーションを使用しました:

 ValueAnimator animator = new ValueAnimator(); 
     animator.setObjectValues(0, count); 
     animator.addUpdateListener(new AnimatorUpdateListener() { 
      public void onAnimationUpdate(ValueAnimator animation) { 
       view.setText(String.valueOf(animation.getAnimatedValue())); 
      } 
     }); 
     animator.setEvaluator(new TypeEvaluator<Integer>() { 
      public Integer evaluate(float fraction, Integer startValue, Integer endValue) { 
       return Math.round((endValue - startValue) * fraction); 
      } 
     }); 
     animator.setDuration(1000); 
     animator.start(); 
あなたはカウンターを任意の番号に任意の番号から行かせる、と一緒にプレイする 0count値で遊ぶことができます

1000を使用して、アニメーション全体の期間を設定します。

これはAndroid APIレベル11以上をサポートしていますが、素晴らしいnineoldandroidsプロジェクトを使用すると、簡単に下位互換性を持たせることができます。