2013年11月16日土曜日

[Androidアニメーション] 3. Property Animation のプログラム

Android APIには2DAnimationが3種類用意されています。Property Animation, View Animation,Drawable Animationです。これらの違いの説明は1. Android Animationの種類紹介しています。
ここでは主にObject Animatorを使ったProperty Animationのサンプルプログラムを紹介します。Property Aniamtionの詳細な説明は2. Property Animationの説明で紹介しています。

  1. Android Animationの種類
  2. Property Animationの説明
  3. Property Animationのプログラム
  4. View Animation の説明とプログラム
  5. Drawable Animationの説明とプログラム


Property Animationのサンプルプログラム

サンプルではオブジェクトをButton 1とButton2とし、プロパティをButton 1ではテキストカラー、Button 2ではx方向の移動距離とするアニメーションを行っています。



public class SamplePropertyAnimation extends Activity {
 
 private static final int RED = 0xffFF8080;
 private static final int BLUE = 0xff8080FF;
 Button button1;
 Button button2;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  setContentView(R.layout.propertyanimation_main);
  
  //Property Animation: Changing TextColor
  button1 = (Button) findViewById(R.id.btn1);
  ObjectAnimator colorAnimator = ObjectAnimator.ofInt(button1,"textColor", RED, BLUE);
  colorAnimator.setDuration(3000);
  colorAnimator.setEvaluator(new ArgbEvaluator());
  colorAnimator.setRepeatCount(ValueAnimator.INFINITE);
  colorAnimator.setRepeatMode(ValueAnimator.REVERSE);
  colorAnimator.start();
     
  //Property Animation: Translation
  button2 = (Button) findViewById(R.id.btn2);
  ObjectAnimator transAnimator = ObjectAnimator.ofFloat(button2, "x", 0f, 100f);
  transAnimator.setDuration(5000);
  transAnimator.setRepeatCount(2);
  transAnimator.setInterpolator(new BounceInterpolator());
  transAnimator.addListener(new AnimatorListenerAdapter(){
   @Override
   public void onAnimationEnd(Animator animation){
    //Write processing after animating
    button2.setVisibility(View.GONE);
   } 
  });
  transAnimator.start();
  
 }

}
16行目でObjectAnimatorにアニメーションしたいオブジェクトとプロパティを設定します。
ObjectAnimator colorAnimator = ObjectAnimator.ofInt(button1,"textColor", RED, BLUE);
1.ターゲットとなるオブジェクト:ここではButtonを設定。
2.プロパティの名前(String):ここでは"textColor"を設定。
3.開始のプロパティの値
4.終了時のプロパティの値
17行目でアニメーションの継続時間をmsで設定します。デフォルトでは300msが設定されています。 

18行目でEvaluatorを設定します。Evaluatorsは与えられたプロパティ値をどのように計算するか指定します。ここでは色の変化をアニメーションするので、ArgbEvaluator()を設定しています。Evaluatorの種類は以下の通りです。
  • IntEvaluator: int プロパティの値を計算。
  • FloatEvaluator:  floatプロパティの値を計算。
  • ArgbEvaluator:  16進数で表されるカラープロパティの値を計算。
  • TypeEvaluator: カスタマイズのエバリュエータを作ることが出来るインターフェース。もし、int, float, colorではないオブジェクトプロパティにアニメーションをしたい場合、オブジェクトプロパティの値をどのように計算をするか指定することができます。

21行目でアニメーションをスタートします。

28行目ではアニメーションの速度を示すInterpolatorを設定しています。Interpolatorはアニメーションで指定された値がタイム機能としてどのように計算されるか定義することができます。例えば、全体のアニメーションを通して、連続的に起こるアニメーションを指定するか、またはアニメーションの始まりと終わりで加速するようなの不連続的なアニメーションを指定するかなど選択できます。android.view.animationに含まれるInterpolatorは以下の種類があります。
  • AccelerateDecelerateinterpolator: 最初最後がゆっくりになり、真ん中で加速する。 
  • AccelerateInterpolator: ゆっくりで始まり、徐々に加速して行く。 
  • AnticipateInterpolator: 開始時に逆方向にためる。 
  • AnticipateOvershootInterpolator:  開始時に逆方向にため、終了時にはみ出す。 
  • BounceInterpolator: 終了時にバウンドする。 
  • AnticipateOvershootInterpolator: 開始時に逆方向にため、終了時にはみ出す。 
  • CycleInterpolator: 指定された回数アニメーションを繰り返す。 
  • DecelerateInterpolator: 開始時に素早く徐々に速度を落とす。 
  • LinearInterpolator: 一定に変化させる。 
  • OvershootInterpolator: 終了時にはみ出す。
  • TimeInterpolator: カスタマー実装を行う。
29行目ではAnimationListenersを設定しています。これは、アニメーションが行われている間のイベントを監視することができます。例では、アニメーション終了時にボタンを消去しています。Listenersでは以下のイベントをキャッチすることができます。
  • onAnimationStart() アニメーション開始時に呼ばれる。
  • onAnimationEnd() アニメーション終了時に呼ばれる。
  • onAnimationRepeat() アニメーション繰り返し時に呼ばれる。
  • onAnimationCancel() キャンセル時に呼ばれる。このとき、onAnimationEnd()も同時に呼ばれる。


0 件のコメント:

コメントを投稿