2014年10月25日土曜日

【Android】アニメーション終了を検知する

アプリの起動時に表示されるスプラッシュ画面なんかをフェイドアウトなどのアニメーションをしてから遷移させたい場合。
アニメーションの終了を検知するリスナーメソッドがあるのでそれを使うといい。

■該当のフラグメントクラスに AnimationListener を implements させる。
■必須メソッドを実装する。
 ◆onAnimationStart(Animation animation)
 ◆onAnimationRepeat(Animation animation)
 ◆onAnimationEnd(Animation animation) →終了時の動作をここに書く
■該当のアニメーションインスタンスに .setAnimationListener(this) とする。
 ここでの引数は「AnimationListener を implements したクラスのインスタンス=自分自身のフラグメントクラスのインスタンス」

こういう感じ。


-----------
//ImageViewのオブジェクト取得
ImageView splash02 = (ImageView)rootView.findViewById(R.id.splash02_animation_imageview);

//アニメーションで使う、フェードアウトにかかる時間(ミリ秒)
int duration_animation = 1*1000;

//複合アニメーション作成
//【1】インスタンスを生成
AnimationSet animation_set = new AnimationSet(true);
splash02_container = (ViewGroup) getActivity().findViewById(R.id.splash02_container);

//【2】基本のアニメーションを生成
//フェードアウト
AlphaAnimation fade_out_animation = new AlphaAnimation(1.0f, 0.0f);
//拡大
ScaleAnimation expand_out_animation = new ScaleAnimation(
 1.0f,
 20.0f,
 1.0f,
 20.0f,
 (splash02.getWidth())/2,
 (splash02.getHeight())/2
);

//【3】生成したアニメーションを追加
animation_set.addAnimation(fade_out_animation);
animation_set.addAnimation(expand_out_animation);

//アニメーションを適用
animation_set.setDuration(duration_animation);

//アニメーション終了感知リスナーを登録 onAnimationEnd()メソッドが実行される
animation_set.setAnimationListener(this);

//アニメーションをスタート
splash02_container.startAnimation(animation_set);

-----------

0 件のコメント:

コメントを投稿