■ImageViewを移動(Tweenアニメーション)させる
以下で定義されている ImageView を移動させる例を記します。
<ImageView id="@+id/img"
android:src="@drawable/image01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
ImageView img = (ImageView)findViewById(R.id.img);
・平行移動させる
// TranslateAnimation(float fromX, float toX, float fromY, float toY)
// fromX/fromY : 移動元X座標/Y座標、toX/toY : 移動先X座標/Y座標
TranslateAnimation translate = new TranslateAnimation(0, 10, 0, 0);
// 1000ms間
translate.setDuration(1000);
// 2回繰り返す
translate.setInterpolator(new CycleInterpolator(2));
// アニメーションスタート
img.startAnimation(translate);
・透明度を変化させる
// AlphaAnimation(float fromAlpha, float toAlpha)
// from/toAlpha : 開始/終了時の透明度 (0.0 : 透明~1.0 : 不透明)
AlphaAnimation alpha = new AlphaAnimation(1, 0);
// 1000ms間
alpha.setDuration(1000);
// 3回繰り返す
alpha.setInterpolator(new CycleInterpolator(3));
// アニメーションスタート
img.startAnimation(alpha);
・回転させる
// RotateAnimation(float from, float to, float pivotX, float pivotY)
// from/to : 開始/終了角度、pivotX/Y : 回転軸の座標
RotateAnimation rotate = new RotateAnimation(0, 360, 30, 90);
// 1000ms間
rotate.setDuration(1000);
// アニメーションスタート
img.startAnimation(rotate);
・拡大/縮小させる
// ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
// fromX/Y : 開始時の表示倍率、toX/Y : 終了時の表示倍率、pivotX/Y : 拡大(縮小)の基点座標
ScaleAnimation scale = new ScaleAnimation(1, 2, 1, 2, 50, 50);
// 1000ms間
scale.setDuration(1000);
// 1回繰り返す
scale.setInterpolator(new CycleInterpolator(1));
// アニメーションスタート
img.startAnimation(scale);
・アニメーションを組み合わせる
// AnimationSet(boolean shareInterpolator)
// true or false : Interpolatorを共有するかどうか
AnimamtionSet set = new AnimationSet(true);
// 回転させる
set.addAnimation(rotate);
// 拡大(縮小)させる
set.addAnimation(scale);
// 繰り返し回数は1回
set.setInterpolator(new CycleInterpolator(1));
// 終了時の状態保持
set.setFillAfter(true);
// アニメーションスタート
img.startAnimation(set);