▼Androidメモ▼
アニメーション


アニメーションを行うプログラムを作成する。



リソース
「res/drawable-nodpi」に「sample.gif」を配置。

sample.gif

インターポレータ
インターポレータ 説明
AccelerateDecelerateInterpolator 遅い→早い→遅い
AccelerateInterpolator 遅い→早い
DecelerateInterpolator 早い→遅い
AnticipateInterpolator 反対方向へ少し移動→目的地
AnticipateOvershootInterpolator 反対方向へ少し移動→目的地→少し通り過ぎる→目的地
BounceInterpolator 目的地→少し通り過ぎる→バウンドしながら目的地
CycleInterpolator 目的地→開始位置→反対方向→開始位置→…
DecelerateInterpolator 急に遅く
LinearInterpolator 等速
OvershootInterpolator 目的地→少し通り過ぎる→目的地


ソースコード
AnimeEx.java
package net.npaka.animeex;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

//アニメーション
public class AnimeEx extends Activity
    implements View.OnClickListener {
    private ImageView imageView;
    private Button    btnTrans;
    private Button    btnScale;
    private Button    btnRotate;
    private Button    btnAlpha;
    private Button    btnSet;

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

        //レイアウトの生成
        LinearLayout layout=new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setBackgroundColor(Color.WHITE);
        setContentView(layout);

        //イメージビューの生成
        imageView=new ImageView(this);
        imageView.setImageBitmap(BitmapFactory.decodeResource(
            getResources(),R.drawable.sample));
        setLLParams(imageView);
        layout.addView(imageView);

        //ボタンの生成
        btnTrans=makeButton("平行移動");
        layout.addView(btnTrans);
        btnScale=makeButton("拡大縮小");
        layout.addView(btnScale);
        btnRotate=makeButton("回転");
        layout.addView(btnRotate);
        btnAlpha=makeButton("α");
        layout.addView(btnAlpha);
        btnSet=makeButton("アニメーションセット");
        layout.addView(btnSet);
    }

    //レイアウトのパラメータ指定
    private static void setLLParams(View view) {
        view.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    //ボタンの生成
    private Button makeButton(String text) {
        Button button=new Button(this);
        button.setText(text);
        button.setOnClickListener(this);
        setLLParams(button);
        return button;
    }

    //ボタンクリックイベントの処理
    public void onClick(View view) {
        //平行移動(fromX,toX, fromY,toY)
        if (view==btnTrans) {
            TranslateAnimation trans=new TranslateAnimation(0,0, 0,320);
            trans.setInterpolator(new LinearInterpolator());
            trans.setDuration(3000);
            imageView.startAnimation(trans);
        }
        //拡大縮小(fromX,toX, fromY,toY, pivotX,pivotY)
        else if (view==btnScale) {
            ScaleAnimation scale=new ScaleAnimation(1,2,1,2, 0,0);
            scale.setDuration(3000);
            scale.setInterpolator(new LinearInterpolator());
            imageView.startAnimation(scale);
        }
        //回転(from,to, pivotX,pivotY)
        else if (view==btnRotate) {
            RotateAnimation rotate=new RotateAnimation(0,360, 30,90);
            rotate.setDuration(3000);
            rotate.setInterpolator(new LinearInterpolator());
            imageView.startAnimation(rotate);
        }
        //α(from,to)
        else if (view==btnAlpha) {
            AlphaAnimation alpha=new AlphaAnimation(1,0);
            alpha.setDuration(3000);
            imageView.startAnimation(alpha);
        }
        //アニメーションセット
        else if (view==btnSet) {
            AnimationSet set=new AnimationSet(true);

            //平行移動
            TranslateAnimation trans=new TranslateAnimation(0,0, 0,320);
            trans.setInterpolator(new LinearInterpolator());
            trans.setDuration(3000);
            set.addAnimation(trans);

            //拡大縮小
            ScaleAnimation scale=new ScaleAnimation(1,2,1,2, 0,0);
            scale.setDuration(3000);
            scale.setInterpolator(new LinearInterpolator());
            set.addAnimation(scale);

            imageView.setAnimation(set);
        }
    }
}



−戻る−