変形によるアニメーションには、透過・回転・拡縮・移動の4種類があります。AnimationSetを使用することにより、アニメーションを組み合わせることも可能です。
注意点としては、setAnimation()やstartAnimation()は、アニメーションが完了する前に復帰してきます。裏で別スレッドに処理をまかせているのでしょう。
import android.app.Activity;
import android.view.animation.Animation;
import android.view.animation.AlphaAnimation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.CycleInterpolator;
import android.view.animation.AnimationSet;
import android.widget.ImageView;
public class MyClass extends Activity{
public void startAnimation(){
ImageView image = (ImageView)findViewById(R.id.animView);
Animation anim = _getAnimation();
// 即時アニメーション開始
image.startAnimation(anim);
// setStartOffSetで設定された時間後に開始
image.setAnimation(anim);
}
private Animation _getAnimation(){
int px = 100; // アニメーションの中心X座標軸
int py = 100; // アニメーションの中心Y座標軸
// Alpha 透過
// 0 ~ 1 透明から不透明へ
Animation alpha = new AlphaAnimation(0.0f,1.0f);
alpha.setDuration(1000); // 1000msかけて動く
alpha.setStartOffset(1000); // 1000ms後に開始
// Rotate 回転
// 0°~ 360°(1回転する)
Animation rotate = new RotateAnimation(0.0f,360f,px,py);
// Scale 拡大縮小
// from幅倍率,to幅倍率,from高さ倍率,to高さ倍率,中心軸X,中心軸Y
Animation scale = new ScaleAnimation(0.0f,1.0f,0.0f,1.0f,px,py);
// Translate 移動
// fromX座標,toX座標,fromY座標,toY座標
// ABSOLUTE:直接座標
// RELATIVE_TO_PARENT:親要素に対しての相対値
// RELATIVE_TO_SELF:自身に対しての相対値
Animation trans = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,-1.0f,
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,0.0f);
// アニメーションの組み合わせ
// true:補間をsetに適用 false:補間を個々に指定
AnimationSet set = new AnimationSet(true);
set.addAnimation(alpha);
set.addAnimation(scale);
set.addAnimation(rotate);
set.addAnimation(trans);
// アニメーション補間
// LinearInterpolator:default(等間隔)
// AccelerateInterpolator:ゆっくりから徐々に速く
// DecelerateInterpolator:速くから徐々にゆっくり
// AccelerateDecelerateInterpolator:ゆっくり → 速く → ゆっくり
// CycleInterpolator:指定回フェイントする
// AnticipateInterpolator:勢いをつけて移動
// OvershootInterpolator:勢いあまって行き過ぎる
// AnticipateOvershootInterpolator:勢いをつけて移動し行き過ぎる
// BounceInterpolator:バウンドする
set.setInterpolator(new CycleInterpolator(1));
// アニメーション終了時の動作
set.setFillBefore(false); // true:終了時に元の状態に戻す
set.setFillAfter(true); // true:終了時に元の状態に戻さない
// リピート
// RESTART:リピートする REVERSE:前回と逆の動作
set.setRepeatMode(Animation.RESTART);
set.setRepeatCount(10); // リピート回数(0でリピートなし、-1で永久)
return set;
}
}
参考: Tweenアニメーション for XML
参考: Frameアニメーション


コメント