▼Androidメモ▼
フレームアニメーション


フレームアニメーションを利用するプログラムを作成する。


リソース
「res/drawable-nodpi」に「face0.gif」「face1.gif」「face2.gif」「face3.gif」「face4.gif」を配置。

face0.gif

face1.gif

face2.gif

face3.gif

「res/anim」に「face.xml」を配置。
face.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/face0" android:duration="200" />
    <item android:drawable="@drawable/face1" android:duration="200" />
    <item android:drawable="@drawable/face2" android:duration="200" />
    <item android:drawable="@drawable/face3" android:duration="200" />
</animation-list>

ソースコード
FrameAnimationEx.java
package net.npaka.frameanimationex;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ImageView;

//フレームアニメーション
public class FrameAnimationEx extends Activity {
    private final static int WC=LinearLayout.LayoutParams.WRAP_CONTENT;
    AnimationDrawable animation;

    //初期化
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //レイアウトの生成
        LinearLayout layout=new LinearLayout(this);
        layout.setBackgroundColor(Color.rgb(255,255,255));
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout); 
        
        //イメージビューの生成
        ImageView imageView=new ImageView(this);
        imageView.setBackgroundResource(R.anim.face);
        imageView.setLayoutParams(new LinearLayout.LayoutParams(WC,WC));
        layout.addView(imageView);
        animation=(AnimationDrawable)imageView.getBackground();
    }
    
    //タッチ時に呼ばれる
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction()==MotionEvent.ACTION_DOWN) {
            //アニメーションの開始と停止
            if (!animation.isRunning()) {
                animation.start();
            } else {
                animation.stop();
            }
            return true;
        }
        return super.onTouchEvent(event);
    }
}


−戻る−