ラベル Drawable Animation の投稿を表示しています。 すべての投稿を表示
ラベル Drawable Animation の投稿を表示しています。 すべての投稿を表示

2013年11月30日土曜日

[Androidアニメーション] 5. Drawable Animation

AndroidのAPIににおける2Dアニメーションの種類は主に3種類あります。

Property Animation

Android3.0(API level 11)からサポートされ、どんなオブジェクトにもアニメーションを行うことができる
View Animation
Viewsに回転、移動、拡大縮小、透明度変換などのアニメーションが行える
Drawable Animation
パラパラ漫画のようにフレームを連続再生することができる
ここでは、Developer AndroidのDrawable Animationの中身をかいつまんで、Drawable Animationについて紹介します。

Drawable Animationとは

Drawable Animationは、Drawable リソースを使って、連続的に表示をすることによってアニメーションを作ることが出来ます。フィルムのような役割をし、順番に沿って、連続した異なった画像を表示して行く従来のアニメーションです。AnimationDrawableクラスは 
Drawable Animationのためのクラスになります。

プログラムの中で、アニメーションのフレームを定義し、AnimationDrawableクラスのAPIを使って、アニメーションを構成するフレームをリスト化したXMLファイルによって簡単に行うことが出来ます。このXMLファイルはAndroidプロジェクトのres/drawable/に置きます。この場合、アニメーションの各フレームに順番と継続時間を指定します。

XMLファイルには、ルートノードとして、<animation-list>要素と連続したフレーム<item>ノードを指定します。XMLファイルの例は以下の通りです。

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>
このアニメーションは、3フレームのみです。listのandroid:oneshotの属性をtrueにセットすることによって、1回で停止し、最後にラストのフレームを表示します。falseをセットするとアニメーションは繰り返します。プロジェクトのres/drawable/ディレクトリに保存されたrocket_thrust.xmlを、Viewに背景画像として指定することが出来ます。ここでのActivityの例は、ImageViewにアニメーションを加え、スクリーンにタッチした時に、アニメーションが始まるプログラムです。
AnimationDrawable rocketAnimation;
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
  rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
  rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}
public boolean onTouchEvent(MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_DOWN) {
    rocketAnimation.start();
    return true;
  }
  return super.onTouchEvent(event);
}
AnimationDrawableで呼ばれるstart()は、onCreate()の中では呼ぶことができません。AnimationDrawableがウィンドウ上にまだ配置されていないからです。もし、アニメーションをすぐに始めたい場合、interactionを要求することなしに、Windowにフォーカスされた時に呼ばれるonWindowFocusChanged()から呼ぶことが出来ます。

XMLを使用しないサンプルプログラム

XMLファイルを使用しないサンプルプログラムは以下の通りです。Activityの中のみでアニメーションを指定することが出来ます。ここでは3フレームを使った250msごとにフレームが変化するアニメーションを説明しています。

public class DrawableAnimation extends Activity{
 
 AnimationDrawable mAnimationDrawable;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
     setContentView(R.layout.drawable_animation);
     //Drawable Animation: Changing Images ----------------------------------------------
     //Create AnimationDrawable
     mAnimationDrawable = new AnimationDrawable();
     //Set frames
     Drawable[] mFrame = new Drawable[3];
     mFrame[0] = getResources().getDrawable( R.drawable.img1);
     mFrame[1] = getResources().getDrawable( R.drawable.img2);
     mFrame[2] = getResources().getDrawable( R.drawable.img3);
    
     //Add frames to AnimationDrawable
     for(int i = 0; i < mFrame.length; i++){
      mAnimationDrawable.addFrame( mFrame[i],  250);
     }
     ImageView imageView = (ImageView) findViewById(R.id.imageView1);
     //Set the animation to background drawable
     imageView.setBackground(mAnimationDrawable);
     //Animation starts
     imageView.post(new Runnable(){
         public void run(){
          mAnimationDrawable.setOneShot(false);
          mAnimationDrawable.start();
        
         }
     });          
 }

まず最初に11行目でAnimationDrawableのコンストラクタをセットしています。
13行目では、フレームにDrawableリソースをセットします。各画像は、res/drawableの中に置きます。ここでは3フレームのアニメーションを行っています。
20行目では、各フレームをAnimationDrawableにセットしています。ここでのパラメータは以下の通りです。
addFrame(Drawable frame, int duration)
  • Drawable frame: フレームとしてのDrawableリソース(画像)
  • int duration: このフレームの継続時間

24行目では、ImageViewの背景画像として、アニメーションをセットしています。
onCreate()の中では、まだリソースがセットされていないので、直接onCreate()の中でアニメーションを始めることはできません。そこで、26行目のようにImageView.postを使用します。これは、ImageViewに背景画像がセットされたら呼ばれるので、ここで、AnimationDrawable.start()を指定します。

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();
    
      }
  });
  
 }

}