2013年11月15日金曜日

[Androidアニメーション] 1. アニメーションの種類

Androidフレームワークには、Android 3.0からサポートされているProperty Animationと従来からサポートされているView Animationがあります。Property AnimationはView Animationよりも柔軟性が高く多くの機能を持っているので、使いやすいと言えます。これらの2つのシステムに加えてDrawable Animationがあります。Drawable Animationではリソースをロードし、ぱらぱら漫画のように、連続したフレームで画像を表示することが出来ます。

Property Animation

Android3.0(API level 11)からサポートされ、Viewsに限らず、どんなオブジェクトのプロパティにも指定した時間間隔で、アニメーションを行うことができます。ここでいうプロパティとはアニメーションに必要な設定値を指しています(例えば、色の変化値、透明度など)。このシステムは拡張性があり、カスタムタイプのプロパティにも適しています。
サンプルプログラムの説明

View Animation

View Animationは、従来のシステムで、Viewsにのみアニメーションを行うことが出来ます。設定が簡単で、Viewsの移動、拡大縮小、透過度の変化など、基本的な機能を備えています。
サンプルプログラムの説明

Drawable Animation

Drawable Animationは、Drawable リソースを次から次へと表示することが出来きます。パラパラ漫画のような連続的なアニメーションを行うことが出来ます。
サンプルプログラムの説明




サンプルプログラムは、各アニメーションを最も簡単な例で示しています。詳細は各アニメーションの紹介ページ(Property Animation, View Animation, Drawable Animation)で説明しています。参考にしてください。


public class SampleAnimation extends Activity {
 
 private static final int RED = 0xffFF8080;
 private static final int BLUE = 0xff8080FF;
 
 AnimationDrawable mAnimationDrawable;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  setContentView(R.layout.activity_main);
  
  //Property Animation: Changing TextColor-------------------------------------
  Button btnColor = (Button) findViewById(R.id.btncolor);
  //Define an object and property in ObjectAnimator 
  ObjectAnimator animColor = ObjectAnimator.ofInt(btnColor,"textColor", RED, BLUE);
  //Set duration time (ms)
  animColor.setDuration(3000);
  //Set Evaluator, how to calculate the specified values.
  animColor.setEvaluator(new ArgbEvaluator());
  //Set repeat count
  animColor.setRepeatCount(ValueAnimator.INFINITE);
  //Set repeat mode
  animColor.setRepeatMode(ValueAnimator.REVERSE);
  //Animation starts
  animColor.start();
  
  //View Animation: Changing Transparency--------------------------------------
  Button btnAlpha = (Button) findViewById(R.id.btnalpa);
  //Set a value of transparency
  AlphaAnimation alp1 = new AlphaAnimation(1.0f, 0.0f);
  //Set duration time (ms)
  alp1.setDuration(3000);
  //Set repeat count
  alp1.setRepeatCount(Animation.INFINITE);
  //Animation starts
  btnAlpha.startAnimation(alp1);
     
  //Drawable Animation: Changing Images ----------------------------------------------
  //Create AnimationDrawable
  mAnimationDrawable = new AnimationDrawable();
  //Set frames
  Drawable[] mFrame = new Drawable[2];
  mFrame[0] = getResources().getDrawable( R.drawable.imgcat1);
  mFrame[1] = getResources().getDrawable( R.drawable.imgcat2);
  //Add frames to AnimationDrawable
  for(int i = 0; i < mFrame.length; i++){
   mAnimationDrawable.addFrame( mFrame[i],  200);
  }
  ImageView imageView = (ImageView) findViewById(R.id.image);
  //Set the animation to background drawable
  imageView.setBackgroundDrawable(mAnimationDrawable);
  //Animation starts
  imageView.post(new Runnable(){
      public void run(){
       mAnimationDrawable.setOneShot(false);
    mAnimationDrawable.start();
    
      }
  });
  
 }

}

0 件のコメント:

コメントを投稿