My Blog

~岩手から発信中~

【Android】アニメーションをActivityで定義する方法

画像(ImageView)に対してアニメーションを付ける方法を書いていきたいと思います。

XML(レイアウト)ファイルにアニメーションを定義する方法もあるのですが、 今回はActivityに定義する方法を書いていきます。

画像の準備


まずレイアウトファイルに画像を配置します。

//activity_main.xml

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="31dp"
        android:layout_marginTop="91dp"
        android:src="@drawable/doroid"
        android:contentDescription="@string/doroidimg"//警告を無くすために追加(画像の説明のため)
/>

android:contentDescription」を追加したので「strings.xml」にも追加します。

//strings.xml

<string name="doroidimg">ドロイド</string>

アニメーション


あとはActivityのonCreateの中に以下のコードを書いていくだけです。

//画像の取得
ImageView img = (ImageView)findViewById(R.id.imageView1);

移動するアニメーション(TranslateAnimation)


//TranslateAnimation(float fromX, float toX, float fromY, float toY)
TranslateAnimation translate = new TranslateAnimation(0, 10, 0, 0);
//動作時間の設定(単位はms)
translate.setDuration(2000);
//繰り返す回数の設定(繰り返さない場合は不要)
translate.setInterpolator(new CycleInterpolator(5));
//アニメーションの開始
img.startAnimation(translate);
コンストラクタ引数 説明
float fromX アニメーション開始時のX座標の位置
float toX アニメーション終了時のX座標の位置
float fromY アニメーション開始時のY座標の位置
float toY アニメーション終了時のY座標の位置

透明度を変更するアニメーション(AlphaAnimation)


//AlphaAnimation(float fromAlpha, float toAlpha)
AlphaAnimation alpha = new AlphaAnimation((float) 0.5, 0);
//動作時間をの設定(単位はms)
alpha.setDuration(1000);
//繰り返す回数の設定(繰り返さない場合は不要)
alpha.setInterpolator(new CycleInterpolator(1));
//アニメーションの開始
img.startAnimation(alpha);
コンストラクタ引数 説明
float fromAlpha アニメーション開始時の透明度を0.0~1.0の間で指定(0.0は完全透明、1.0は完全不透明)
float toAlpha アニメーション終了時の透明度を0.0~1.0の間で指定(0.0は完全透明、1.0は完全不透明)

回転するアニメーション(RotateAnimation)


//RotateAnimation(float from, float to, float pivotX, float pivotY)
RotateAnimation rotate = new RotateAnimation(0, 360, 30, 90);
//動作時間の設定(単位ms)
rotate.setDuration(1000);
//繰り返す回数の設定(繰り返さない場合は不要)
rotate.setInterpolator(new CycleInterpolator(3));
//アニメーションの開始
img.startAnimation(rotate);
コンストラクタ引数 説明
float from アニメーション開始時の角度
float to アニメーション終了時の角度
float pivotX 回転するX座標の基点位置
float pivotY 回転するY座標の基点位置

拡大/縮小するアニメーション(ScaleAnimation)


//ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
ScaleAnimation scale = new ScaleAnimation(1,2,1,2, 300, 100);
//動作時間の設定(単位ms)
scale.setDuration(1000);
//繰り返す回数の設定(繰り返さない場合は不要)
scale.setInterpolator(new CycleInterpolator(1));
//アニメーションの開始
img.startAnimation(scale);
コンストラクタ引数 説明
float fromX アニメーション開始時のX軸方向の表示倍率
float toX アニメーション終了時のX軸方向の表示倍率
float fromY アニメーション開始時のY軸方向の表示倍率
float toY アニメーション終了時のY軸方向の表示倍率
float pivotX 拡大/縮小するX座標の基点位置
float pivotY 拡大/縮小するY座標の基点位置

アニメーションの組み合わせ(AnimationSet)


//回転のアニメーションを設定
//RotateAnimation(float from, float to, float pivotX, float pivotY)
RotateAnimation rotate = new RotateAnimation(0, 360, 30, 90);
//動作時間の設定(単位ms)
rotate.setDuration(1000);
//繰り返す回数の設定(繰り返さない場合は不要)
rotate.setInterpolator(new CycleInterpolator(3));

//拡大/縮小のアニメーションを設定
//ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
ScaleAnimation scale = new ScaleAnimation(1,2,1,2, 300, 100);
//動作時間の設定(単位ms)
scale.setDuration(1000);
//繰り返す回数の設定(繰り返さない場合は不要)
scale.setInterpolator(new CycleInterpolator(1));

//設定された回転と拡大/縮小を組み合せる
AnimationSet set = new AnimationSet(true);
//回転をセット
set.addAnimation(rotate);
//拡大/縮小をセット
set.addAnimation(scale);
//繰り返す回数の設定(繰り返さない場合は不要)
set.setInterpolator(new CycleInterpolator(1));
//アニメーションの開始
img.startAnimation(set);

次回はXMLにアニメーションを定義する方法を書いていきたいと思います。


参考サイト:http://www.k-sugi.sakura.ne.jp/java/android/1808/