Androidプログラマへの道 〜 Moonlight 明日香 〜 - フェードイン/アウトを行う
アニメーションフレームワークは, 以下の2種類が準備されている.   Viewに限らず任意のオブジェクト(View, Drawable, Fragument, etc)に対してアニメーションを実行できる.   Viewに対してのみアニメーションを実行できる.

アニメーションの一種であるフェードイン/アウトを行うには, アニメータ(Animator)クラスあるいはビューアニメーション(view.Animation)クラスを利用する.


            フェードアウト


Property Animation (API Level 11)

  • res/animator/alpha.xml
アニメーションの設定を行うXMLファイルである.
    • interpolatorは, アニメーション動作の変化を指定する. 例では, decelerate_interpolator(減速).
    • propertyNameは, アニメーションするオブジェクトのプロパティ名を指定する. 例では, Viewのalpha.
    • durationは, アニメーションの動作時間(ms)を指定する.
    • valueFromとvalueToは, 指定したpropertyのアニメーション開始/終了時の値を指定する. 例では, 透過度.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
  <objectAnimator
    android:interpolator="@android:anim/decelerate_interpolator"
    android:propertyName="alpha"
    android:duration="2000"
    android:valueFrom="1.0"
    android:valueTo="0.2"
    />
</set>
  • Animation04.java
    • findViewByIdメソッドで, 指定したリソースインデックスのIDに対応したイメージビューのインスタンスを取得する.
    • AnimatorInflater#loadAnimatorメソッドで, アニメーション設定ファイル(alpha.xml)を読み込み, AnimatorSetを作成する.
    • AnimatorSet#setTargetメソッドで, ターゲットのオブジェクトを設定する.
    • AnimatorSet#startメソッドで, アニメーションを開始する.
package com.moonlight_aska.android.animation04;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class Animation04 extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView img = (ImageView)findViewById(R.id.robot);
    AnimatorSet set = (AnimatorSet)AnimatorInflater.loadAnimator(this, R.animator.alpha);
    set.setTarget(img);
    set.start();
  }
}
  • res/layout/main.xml
    • 画像ファイル(robot.png)は, res/drawableフォルダの下に置く.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<ImageView
  android:id="@+id/robot"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:src="@drawable/robot"
  />
</RelativeLayout>

View Animation (API Level 1)

  • res/anim/alpha.xml
アニメーションの設定を行うXMLファイルである.
    • AlphaAnimationクラスで指定する属性
android:fromAlphaアニメーション開始時のアルファ値
andorid:toAlphaアニメーション終了時のアルファ値
    • Animationクラスで指定する属性
android:durationアニメーションの動作時間(msec)
android:fillAftertrue:アニメーション終了後にアニメーションの変換が適用
android:fillBeforetrue:アニメーション開始前にアニメーションの変換が適用
android:fillEnabledtrue:fillAfterが適用
android:repeatCountアニメーションの繰り返し回数
android:repeatModeアニメーションが終了した場合の振る舞い
android:startOffsetアニメーションが開始する前に置く時間(msec)
android:zAdjustmentアニメーション実行中のコンテンツについてZ軸方向の調整
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/decelerate_interpolator"
  android:fromAlpha="1.0"
  android:toAlpha="0.2"
  android:fillAfter="true"
  android:duration="2000"
  />
  • Animation04.java
    • findViewByIdメソッドで, 指定したリソースインデックスのIDに対応したイメージビューのインスタンスを取得する.
    • AnimationUtils#loadAnimationメソッドでアニメーション設定ファイル(alpha.xml)を読み込み, Animationを作成する.
    • ImageView#startAnimationメソッドでアニメーションを開始する.
package com.moonlight_aska.android.animation04;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class Animation04 extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView img = (ImageView)findViewById(R.id.robot);
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
    img.startAnimation(anim);
  }
}