アニメーションを使ってビューを表示または非表示にする

Compose をお試しください
Jetpack Compose は、Android で推奨される UI ツールキットです。Compose でアニメーションを使用する方法について説明します。

アプリの使用中は、古い情報が画面から削除され、新しい情報が画面に表示されます。画面に表示される内容をあまりに瞬間的に切り替えると、不快に感じられる場合や、ユーザーが突然表示された新しいコンテンツを見逃してしまう場合があります。アニメーションを利用すると、変更速度を遅くして、ユーザーの目を引くことができるため、更新内容がわかりやすくなります。

ビューの表示 / 非表示を切り替える際によく利用されるアニメーションは 3 種類あります。円形出現エフェクト アニメーション、クロスフェード アニメーション、カードフリップ アニメーションです。

クロスフェード アニメーションを作成する

クロスフェード アニメーション(別名ディゾルブ)とは、1 つの View または ViewGroup を徐々にフェードアウトしながら、同時に別のものをフェードインしていく方法です。このアニメーションは、アプリ内のコンテンツやビューを切り替える場合に便利です。下記に示すクロスフェード アニメーションの例は、Android 3.1(API レベル 12)以降で利用可能な ViewPropertyAnimator を使用しています。

進行状況インジケーターからテキスト コンテンツへのクロスフェードの例を以下に示します。

図 1. クロスフェード アニメーション。

ビューを作成する

クロスフェードする 2 つのビューを作成します。進行状況インジケーターとスクロール可能なテキストビューを作成する例を以下に示します。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView style="?android:textAppearanceMedium"
            android:lineSpacingMultiplier="1.2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/lorem_ipsum"
            android:padding="16dp" />

    </ScrollView>

    <ProgressBar android:id="@+id/loading_spinner"
        style="?android:progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

</FrameLayout>

クロスフェード アニメーションをセットアップする

クロスフェード アニメーションをセットアップする手順は次のとおりです。

  1. クロスフェードするビューのメンバー変数を作成します。後で、アニメーション中にビューを変更する際に、この参照が必要になります。
  2. フェードインするビューの表示を GONE に設定します。これにより、ビューがレイアウト スペースを使用することがなくなり、レイアウトの計算から除外されるため、処理が高速化されます。
  3. config_shortAnimTime システム プロパティをメンバー変数内にキャッシュします。このプロパティは、このアニメーションに対して標準の「短い」持続時間を定義します。この持続時間は、軽いアニメーションや、頻繁に発生するアニメーションに最適です。config_longAnimTimeconfig_mediumAnimTime も利用できます。

上記のコード スニペットのレイアウトをアクティビティ コンテンツ ビューとして使用する例を以下に示します。

Kotlin

class CrossfadeActivity : Activity() {

    private lateinit var contentView: View
    private lateinit var loadingView: View
    private var shortAnimationDuration: Int = 0
    ...
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_crossfade)

        contentView = findViewById(R.id.content)
        loadingView = findViewById(R.id.loading_spinner)

        // Initially hide the content view.
        contentView.visibility = View.GONE

        // Retrieve and cache the system's default "short" animation time.
        shortAnimationDuration = resources.getInteger(android.R.integer.config_shortAnimTime)
    }
    ...
}

Java

public class CrossfadeActivity extends Activity {

    private View contentView;
    private View loadingView;
    private int shortAnimationDuration;
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crossfade);

        contentView = findViewById(R.id.content);
        loadingView = findViewById(R.id.loading_spinner);

        // Initially hide the content view.
        contentView.setVisibility(View.GONE);

        // Retrieve and cache the system's default "short" animation time.
        shortAnimationDuration = getResources().getInteger(
                android.R.integer.config_shortAnimTime);
    }
    ...
}

ビューをクロスフェードする

ビューを適切にセットアップできたら、以下の設定を行ってビューをクロスフェードします。

  1. フェードインするビューのアルファ値を 0 に設定し、表示を初期設定の GONE から VISIBLE に設定します。これにより、ビューは表示されますが、透明になります。
  2. フェードインするビューのアルファ値を 0 から 1 にアニメーション化します。フェードアウトするビューのアルファ値を 1 から 0 にアニメーション化します。
  3. Animator.AnimatorListener 内で onAnimationEnd() を使用し、フェードアウトするビューの表示を GONE に設定します。アルファ値が 0 であっても、ビューの表示を GONE に設定すると、ビューがレイアウト スペースを使用することがなくなり、レイアウトの計算から除外さるため、処理が高速化されます。

このセットアップを行うメソッドの例を以下に示します。

Kotlin

class CrossfadeActivity : Activity() {

    private lateinit var contentView: View
    private lateinit var loadingView: View
    private var shortAnimationDuration: Int = 0
    ...
    private fun crossfade() {
        contentView.apply {
            // Set the content view to 0% opacity but visible, so that it is
            // visible but fully transparent during the animation.
            alpha = 0f
            visibility = View.VISIBLE

            // Animate the content view to 100% opacity and clear any animation
            // listener set on the view.
            animate()
                    .alpha(1f)
                    .setDuration(shortAnimationDuration.toLong())
                    .setListener(null)
        }
        // Animate the loading view to 0% opacity. After the animation ends,
        // set its visibility to GONE as an optimization step so it doesn't
        // participate in layout passes.
        loadingView.animate()
                .alpha(0f)
                .setDuration(shortAnimationDuration.toLong())
                .setListener(object : AnimatorListenerAdapter() {
                    override fun onAnimationEnd(animation: Animator) {
                        loadingView.visibility = View.GONE
                    }
                })
    }
}

Java

public class CrossfadeActivity extends Activity {

    private View contentView;
    private View loadingView;
    private int shortAnimationDuration;
    ...
    private void crossfade() {

        // Set the content view to 0% opacity but visible, so that it is
        // visible but fully transparent during the animation.
        contentView.setAlpha(0f);
        contentView.setVisibility(View.VISIBLE);

        // Animate the content view to 100% opacity and clear any animation
        // listener set on the view.
        contentView.animate()
                .alpha(1f)
                .setDuration(shortAnimationDuration)
                .setListener(null);

        // Animate the loading view to 0% opacity. After the animation ends,
        // set its visibility to GONE as an optimization step so it doesn't
        // participate in layout passes.
        loadingView.animate()
                .alpha(0f)
                .setDuration(shortAnimationDuration)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        loadingView.setVisibility(View.GONE);
                    }
                });
    }
}

カードフリップ アニメーションを作成する

カードフリップは、カードがひっくり返る動きをエミュレートするアニメーションを表示することで、コンテンツのビュー間の切り替えを行います。下に示すカードフリップ アニメーションは、FragmentTransaction を使用しています。

カードフリップは次のように表示されます。

図 2. カードフリップ アニメーション。

Animator オブジェクトを作成する

カードフリップ アニメーションを作成するには、4 つのアニメーターが必要です。2 つのアニメーターは、カードの前面が左に出るとき用と、左から入るとき用です。残りの 2 つのアニメーターは、カードの背面が右から出るとき用と、右に入るとき用です。

card_flip_left_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Before rotating, immediately set the alpha to 0. -->
    <objectAnimator
        android:valueFrom="1.0"
        android:valueTo="0.0"
        android:propertyName="alpha"
        android:duration="0" />

    <!-- Rotate. -->
    <objectAnimator
        android:valueFrom="-180"
        android:valueTo="0"
        android:propertyName="rotationY"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:duration="@integer/card_flip_time_full" />

    <!-- Halfway through the rotation, set the alpha to 1. See startOffset. -->
    <objectAnimator
        android:valueFrom="0.0"
        android:valueTo="1.0"
        android:propertyName="alpha"
        android:startOffset="@integer/card_flip_time_half"
        android:duration="1" />
</set>

card_flip_left_out.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Rotate. -->
    <objectAnimator
        android:valueFrom="0"
        android:valueTo="180"
        android:propertyName="rotationY"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:duration="@integer/card_flip_time_full" />

    <!-- Halfway through the rotation, set the alpha to 0. See startOffset. -->
    <objectAnimator
        android:valueFrom="1.0"
        android:valueTo="0.0"
        android:propertyName="alpha"
        android:startOffset="@integer/card_flip_time_half"
        android:duration="1" />
</set>

card_flip_right_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Before rotating, immediately set the alpha to 0. -->
    <objectAnimator
        android:valueFrom="1.0"
        android:valueTo="0.0"
        android:propertyName="alpha"
        android:duration="0" />

    <!-- Rotate. -->
    <objectAnimator
        android:valueFrom="180"
        android:valueTo="0"
        android:propertyName="rotationY"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:duration="@integer/card_flip_time_full" />

    <!-- Halfway through the rotation, set the alpha to 1. See startOffset. -->
    <objectAnimator
        android:valueFrom="0.0"
        android:valueTo="1.0"
        android:propertyName="alpha"
        android:startOffset="@integer/card_flip_time_half"
        android:duration="1" />
</set>

card_flip_right_out.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Rotate. -->
    <objectAnimator
        android:valueFrom="0"
        android:valueTo="-180"
        android:propertyName="rotationY"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:duration="@integer/card_flip_time_full" />

    <!-- Halfway through the rotation, set the alpha to 0. See startOffset. -->
    <objectAnimator
        android:valueFrom="1.0"
        android:valueTo="0.0"
        android:propertyName="alpha"
        android:startOffset="@integer/card_flip_time_half"
        android:duration="1" />
</set>

ビューを作成する

カードの前面と背面はそれぞれ独立したレイアウトで、2 つのテキストビューや 2 つの画像、その他フリップする各種のビューの組み合わせなど、コンテンツを自由に格納することができます。そのような 2 つのレイアウトを、後でアニメーション化するフラグメント内で使用します。テキストを表示するカードの片面を作成するレイアウトの例を以下に示します。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#a6c"
    android:padding="16dp"
    android:gravity="bottom">

    <TextView android:id="@android:id/text1"
        style="?android:textAppearanceLarge"
        android:textStyle="bold"
        android:textColor="#fff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/card_back_title" />

    <TextView style="?android:textAppearanceSmall"
        android:textAllCaps="true"
        android:textColor="#80ffffff"
        android:textStyle="bold"
        android:lineSpacingMultiplier="1.2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/card_back_description" />

</LinearLayout>

次のレイアウトは、ImageView を表示するカードの反対側を作成します。

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/image1"
    android:scaleType="centerCrop"
    android:contentDescription="@string/description_image_1" />

フラグメントを作成する

カードの前面用と背面用のフラグメント クラスをそれぞれ作成します。フラグメント クラスで、onCreateView() メソッドから作成したレイアウトを返します。そして、カードを表示する親アクティビティ内に、このフラグメントのインスタンスを作成します。

フラグメント クラスを使用する親アクティビティ内にフラグメント クラスをネストする例を以下に示します。

Kotlin

class CardFlipActivity : FragmentActivity() {
    ...
    /**

                    *   A fragment representing the front of the card.
     */
    class CardFrontFragment : Fragment() {

    override fun onCreateView(
                inflater: LayoutInflater,
                container: ViewGroup?,
                savedInstanceState: Bundle?
    ): View = inflater.inflate(R.layout.fragment_card_front, container, false)
    }

    /**
    *   A fragment representing the back of the card.
    */
    class CardBackFragment : Fragment() {

    override fun onCreateView(
                inflater: LayoutInflater,
                container: ViewGroup?,
                savedInstanceState: Bundle?
    ): View = inflater.inflate(R.layout.fragment_card_back, container, false)
    }
}

Java

public class CardFlipActivity extends FragmentActivity {
    ...
    /**
    *   A fragment representing the front of the card.
    */
    public class CardFrontFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_card_front, container, false);
    }
    }

    /**
    *   A fragment representing the back of the card.
    */
    public class CardBackFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_card_back, container, false);
    }
    }
}

カードフリップをアニメーション化する

親アクティビティ内にフラグメントを表示します。そのためには、アクティビティのレイアウトを作成します。実行時にフラグメントを追加できる FrameLayout を作成する例を以下に示します。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

アクティビティ コード内で、作成したレイアウト用のコンテンツ ビューを設定します。アクティビティの作成時にデフォルト フラグメントを表示することをおすすめします。次のアクティビティ例では、カードの前面をデフォルトで表示しています。

Kotlin

class CardFlipActivity : FragmentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_activity_card_flip)
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                    .add(R.id.container, CardFrontFragment())
                    .commit()
        }
    }
    ...
}

Java

public class CardFlipActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_card_flip);

        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.container, new CardFrontFragment())
                    .commit();
        }
    }
    ...
}

カードの前面が表示されている状態であり、フリップ アニメーションを使用すると、適切なタイミングでカードの背面を表示することができます。カードの反対側を表示するメソッドを作成して、以下を行います。

  • フラグメント遷移用に作成したカスタム アニメーションを設定します。
  • 表示されているフラグメントを新しいフラグメントに置き換えて、作成したカスタム アニメーションを使用してこのイベントをアニメーション化します。
  • 以前表示されていたフラグメントをフラグメント バックスタックに追加して、ユーザーが [戻る] ボタンをタップすると、カードがもう一度反転するようにします。

Kotlin

class CardFlipActivity : FragmentActivity() {
    ...
    private fun flipCard() {
        if (showingBack) {
            supportFragmentManager.popBackStack()
            return
        }

        // Flip to the back.

        showingBack = true

        // Create and commit a new fragment transaction that adds the fragment
        // for the back of the card, uses custom animations, and is part of the
        // fragment manager's back stack.

        supportFragmentManager.beginTransaction()

                // Replace the default fragment animations with animator
                // resources representing rotations when switching to the back
                // of the card, as well as animator resources representing
                // rotations when flipping back to the front, such as when the
                // system Back button is tapped.
                .setCustomAnimations(
                        R.animator.card_flip_right_in,
                        R.animator.card_flip_right_out,
                        R.animator.card_flip_left_in,
                        R.animator.card_flip_left_out
                )

                // Replace any fragments in the container view with a fragment
                // representing the next page, indicated by the just-incremented
                // currentPage variable.
                .replace(R.id.container, CardBackFragment())

                // Add this transaction to the back stack, letting users press
                // the Back button to get to the front of the card.
                .addToBackStack(null)

                // Commit the transaction.
                .commit()
    }
}

Java

public class CardFlipActivity extends FragmentActivity {
    ...
    private void flipCard() {
        if (showingBack) {
            getSupportFragmentManager().popBackStack();
            return;
        }

        // Flip to the back.

        showingBack = true;

        // Create and commit a new fragment transaction that adds the fragment
        // for the back of the card, uses custom animations, and is part of the
        // fragment manager's back stack.

        getSupportFragmentManager()
                .beginTransaction()

                // Replace the default fragment animations with animator
                // resources representing rotations when switching to the back
                // of the card, as well as animator resources representing
                // rotations when flipping back to the front, such as when the
                // system Back button is pressed.
                .setCustomAnimations(
                        R.animator.card_flip_right_in,
                        R.animator.card_flip_right_out,
                        R.animator.card_flip_left_in,
                        R.animator.card_flip_left_out)

                // Replace any fragments in the container view with a fragment
                // representing the next page, indicated by the just-incremented
                // currentPage variable.
                .replace(R.id.container, new CardBackFragment())

                // Add this transaction to the back stack, letting users press
                // Back to get to the front of the card.
                .addToBackStack(null)

                // Commit the transaction.
                .commit();
    }
}

円形出現エフェクト アニメーションを作成する

出現エフェクト アニメーションを使用すると、UI 要素グループの表示 / 非表示を切り替える際に連続した印象をユーザーに与えることができます。ViewAnimationUtils.createCircularReveal() メソッドを使用すると、切り抜き円形領域をアニメーション化して、ビューの表示 / 非表示を切り替えることができます。このアニメーションは、Android 5.0(API レベル 21)以降で利用可能な ViewAnimationUtils クラスを使用します。

アニメーション前には見えていなかったビューを、アニメーション後に表示する方法の例を以下に示します。

Kotlin

// A previously invisible view.
val myView: View = findViewById(R.id.my_view)

// Check whether the runtime version is at least Android 5.0.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // Get the center for the clipping circle.
    val cx = myView.width / 2
    val cy = myView.height / 2

    // Get the final radius for the clipping circle.
    val finalRadius = Math.hypot(cx.toDouble(), cy.toDouble()).toFloat()

    // Create the animator for this view. The start radius is 0.
    val anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0f, finalRadius)
    // Make the view visible and start the animation.
    myView.visibility = View.VISIBLE
    anim.start()
} else {
    // Set the view to invisible without a circular reveal animation below
    // Android 5.0.
    myView.visibility = View.INVISIBLE
}

Java

// A previously invisible view.
View myView = findViewById(R.id.my_view);

// Check whether the runtime version is at least Android 5.0.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // Get the center for the clipping circle.
    int cx = myView.getWidth() / 2;
    int cy = myView.getHeight() / 2;

    // Get the final radius for the clipping circle.
    float finalRadius = (float) Math.hypot(cx, cy);

    // Create the animator for this view. The start radius is 0.
    Animator anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0f, finalRadius);

    // Make the view visible and start the animation.
    myView.setVisibility(View.VISIBLE);
    anim.start();
} else {
    // Set the view to invisible without a circular reveal animation below
    // Android 5.0.
    myView.setVisibility(View.INVISIBLE);
}

ViewAnimationUtils.createCircularReveal() アニメーションは 5 つのパラメータを取ります。1 番目のパラメータは、画面上で非表示 / 表示のいずれかに設定しておくビューを示します。次の 2 つのパラメータは、切り抜き円形領域の中心の X 座標と Y 座標を示します。通常はこの座標がビューの中心になりますが、ユーザーがタップしたポイントを利用して、ユーザーが選択した場所からアニメーションを開始することもできます。4 番目のパラメータは、切り抜き円形領域の開始半径を示します。

上記の例では、初期半径が 0 に設定されているため、アニメーション後に表示するビューは、アニメーション前は円形領域によって隠されています。最後のパラメータは、円形領域の最終半径を示します。アニメーション後にビューを表示する場合は、必ず、最終半径がビュー自体よりも大きくなるように指定してください。これにより、アニメーションが終了する前に、ビューが完全に表示されるようになります。

アニメーション前に表示されていたビューを非表示にするには:

Kotlin

// A previously visible view.
val myView: View = findViewById(R.id.my_view)

// Check whether the runtime version is at least Android 5.0.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // Get the center for the clipping circle.
    val cx = myView.width / 2
    val cy = myView.height / 2

    // Get the initial radius for the clipping circle.
    val initialRadius = Math.hypot(cx.toDouble(), cy.toDouble()).toFloat()

    // Create the animation. The final radius is 0.
    val anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, initialRadius, 0f)

    // Make the view invisible when the animation is done.
    anim.addListener(object : AnimatorListenerAdapter() {

        override fun onAnimationEnd(animation: Animator) {
            super.onAnimationEnd(animation)
            myView.visibility = View.INVISIBLE
        }
    })

    // Start the animation.
    anim.start()
} else {
    // Set the view to visible without a circular reveal animation below
    // Android 5.0.
    myView.visibility = View.VISIBLE
}

Java

// A previously visible view.
final View myView = findViewById(R.id.my_view);

// Check whether the runtime version is at least Android 5.0.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // Get the center for the clipping circle.
    int cx = myView.getWidth() / 2;
    int cy = myView.getHeight() / 2;

    // Get the initial radius for the clipping circle.
    float initialRadius = (float) Math.hypot(cx, cy);

    // Create the animation. The final radius is 0.
    Animator anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, initialRadius, 0f);

    // Make the view invisible when the animation is done.
    anim.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            myView.setVisibility(View.INVISIBLE);
        }
    });

    // Start the animation.
    anim.start();
} else {
    // Set the view to visible without a circular reveal animation below Android
    // 5.0.
    myView.setVisibility(View.VISIBLE);
}

この場合、切り抜き円形領域の初期半径がビューと同じ大きさに設定されているため、アニメーションの開始前はビューが表示された状態になっています。最終半径が 0 に設定されているため、アニメーションが終了するとビューは非表示になります。アニメーションの完了時にビューの表示を INVISIBLE に設定できるように、リスナーをアニメーションに追加します。

参考情報