View Animation — とは、AndroidのTween Animation

著者: IT Sectr 公開日: 2026-03-01 読了時間: 8 分

View Animationは、Tween Animation(Alpha、Translate、Scale、Rotate)を介してViewオブジェクトを操作するAndroid API 1+のレガシーアニメーションシステムです。Googleはすべての新しいプロジェクトにProperty Animation(Animator)を推奨しています。この記事では、その原理、種類、XMLマークアップ、および非推奨の理由について説明します。

重要なポイント

  • View Animation — Tween Animationを使用してViewをアニメーション化: Alpha、Translate、Scale、Rotate
  • XML定義 — アニメーションはres/anim/でを使用して記述されます
  • AnimationSet — 共通パラメーターを持つ1つのセットに複数のアニメーションを組み合わせる
  • 制限 — View Animationはプロパティを変更せず、レンダリングのみを変更: Viewは元の位置に留まる
  • Property Animation — 実際のオブジェクトプロパティを操作する最新の代替(Animator)

View Animationとは?

View Animationは、Viewレベル(API 1+)で動作するAndroidのアニメーションサブシステムです。AlphaAnimation(透明度)、TranslateAnimation(移動)、ScaleAnimation(拡大縮小)、RotateAnimation(回転)の4種類のTween Animationを使用します。各アニメーションはres/anim/フォルダーのXMLファイルで記述されるか、Kotlinコードでプログラム的に作成されます。

View AnimationのメカニズムはViewの実際のプロパティを変更しません — 新しい位置、拡大縮小率、または透明度でViewを再描画しますが、onTouchEvent()コールバックは元の位置の座標を受け取ります。これがクリックミスマッチとして知られる主要な制限です。

Androidデベロッパードキュメント(2026年)によると、GoogleはAndroid 3.0(API 11)以降、View Animationを非推奨APIとしてマークしています。新しいプロジェクトには、Property Animation — Animator、AnimatorSet、ObjectAnimatorが推奨されており、これらはsetterメソッドを通じて実際のオブジェクトプロパティを操作します。

Tween Animationの種類

Tween Animationはキーフレーム“間”のアニメーションです:開始状態と終了状態を定義すると、システムが中間フレームを計算します。Androidは4つの標準タイプを提供し、それぞれに独自のパラメーターセットがあります。

AlphaAnimation

AlphaAnimationはViewの透明度をfromAlphaからtoAlphaまで変更します。値は0.0(完全に透明)から1.0(完全に表示)までです。デフォルトでは、アニメーションは300 ms続きます。fillAfter=trueパラメーターはViewを最終的な透明度状態に保ちます。

xml
<!-- res/anim/fade_out.xml -->
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="500"
    android:fillAfter="true" />

TranslateAnimation

TranslateAnimationはViewを開始位置(fromXDelta、fromYDelta)から終了位置(toXDelta、toYDelta)に移動します。値はピクセル、親のパーセンテージ、またはView自体のパーセンテージで指定されます。たとえば、100%はViewの幅の100%を意味します。

ScaleAnimation

ScaleAnimationはpivotX/pivotYポイントを基準にViewを拡大縮小します。fromXScale=1.0、toXScale=2.0は幅を2倍にします。pivotTypeパラメーターは、ピボットが何を基準に計算されるかを決定します:self、parent、またはabsolute。拡大縮小アニメーションはViewの実際の寸法を変更せず、レンダリングのみを変更します。

RotateAnimation

RotateAnimationはViewをピボットポイントを中心にfromDegreesからtoDegreesの角度で回転します。角度は度で測定されます:0は元の位置、360は完全な回転です。無限回転にはrepeatCount=INFINITEを使用します。

View Animation vs Property Animation

View AnimationとProperty Animation(Animator)の違いは、Androidアニメーションアーキテクチャにおける根本的な違いです。View Animationはレンダリングのみを操作しますが、Property Animationはsetterメソッドを通じて実際のオブジェクトプロパティを変更し、レイアウトの更新につながります。

基準View AnimationProperty Animation
APIレベルAPI 1+(非推奨)API 11+(推奨)
アニメーション対象Viewのレンダリング(canvas)オブジェクトのプロパティ(setter)
アニメーション後のクリック元の位置新しい位置
アニメーションタイプ4 Tween + AnimationSetObjectAnimator、ValueAnimator、AnimatorSet
LayoutParams変更しないsetLayoutParamsで変更
パフォーマンス高い(canvasのみ)中程度(setterに依存)

アニメーションのXML設定

View Animationは通常、res/anim/のXMLファイルでルート要素(AnimationSet)または4つのタイプのいずれかで定義されます。AnimationSetを使用すると、startOffset、duration、interpolatorなどの共有パラメーターでアニメーションを組み合わせることができます。

xml
<!-- res/anim/bounce.xml -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/bounce_interpolator">

    <translate
        android:fromYDelta="0%"
        android:toYDelta="-100%"
        android:duration="600" />

    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.3"
        android:duration="600" />
</set>

XMLファイルはAnimationUtils.loadAnimation()を介して読み込まれます。Interpolatorは、パラメーターが時間とともにどのように変化するかを定義します:accelerate、decelerate、bounce、overshoot。カスタムInterpolatorはXMLリソース@anim/interpolator_nameを介して作成されます。

共通属性

すべてのTweenアニメーションは属性をサポートします:duration(ms)、startOffset(開始前の遅延)、repeatCount、repeatMode(restart/reverse)、fillEnabled、fillBefore、fillAfter、interpolator。fillAfter属性は、完了後にViewがアニメーションの最終状態を維持するかどうかを決定します。

Kotlinコード例

アニメーションは通常XMLで定義されますが、Kotlinを使用してプログラム的に作成することもできます。プログラム的なアプローチは動的パラメーターに役立ちます:透明度の値は静的に定義されるのではなく、アプリケーションの状態に依存します。

kotlin
// KotlinでのAlphaAnimationのプログラム的作成
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.View

fun View.fadeOut(duration: Long = 500L, onEnd: () -> Unit = {}) {
    val animation = AlphaAnimation(1.0f, 0.0f).apply {
        this.duration = duration
        fillAfter = true
        setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationEnd(animation: Animation?) { onEnd() }
            override fun onAnimationStart(animation: Animation?) {}
            override fun onAnimationRepeat(animation: Animation?) {}
        })
    }
    startAnimation(animation)
}

// XMLからのアニメーションの読み込み
val bounceAnim = AnimationUtils.loadAnimation(context, R.anim.bounce)
view.startAnimation(bounceAnim)

// AnimationSet — アニメーションの組み合わせ
val set = AnimationSet(true).apply {
    addAnimation(TranslateAnimation(0f, 200f, 0f, 0f).apply { duration = 400 })
    addAnimation(RotateAnimation(0f, 360f).apply {
        duration = 400
        pivotX = view.width / 2f
        pivotY = view.height / 2f
    })
}
view.startAnimation(set)

startAnimation()メソッドはアニメーションをViewのレンダリングキューに追加します。キャンセルするにはclearAnimation()を使用します。重要:fillAfter=trueはLayoutParamsを変更しません — startAnimationが再度呼び出されると、アニメーションは最後にレンダリングされた状態ではなく、Viewの元の状態から開始されます。

View Animationを使用するタイミング

View Animationは2026年において適用範囲が狭いです:Property Animationが不安定に動作する可能性があるAndroid 4.x(API 16-19)のサポート、LayoutParamsの変更を必要としない単純なフェードイン/フェードアウト効果、クリックミスマッチが重要でないアニメーション(例:背景要素)。その他すべてのケースでは、GoogleはObjectAnimatorまたはAnimatorSetへの移行を推奨しています。

Android Developersによると、Property Animationは中間canvasフレームを作成しないため、メモリ効率が15-20%向上します。View AnimationからProperty Animationへの移行は、クリックミスマッチの問題も解消します:アニメーション後のViewは新しい位置でタッチを受け取ります。

よくある質問

View AnimationとProperty Animationの違いは?

View AnimationはViewのレンダリングのみを変更します — アニメーション後、Viewはタッチイベントに対して元の位置に留まります。Property Animationはオブジェクトの実際のプロパティを変更し、LayoutParamsを更新して新しい位置でのクリックを正しく処理します。

なぜView Animationは非推奨と見なされるのですか?

GoogleはAndroid 3.0(API 11)以降、View Animationを非推奨と宣言しました。理由はクリックミスマッチと、任意のオブジェクトプロパティをアニメーション化できないことです。Property Animationはこれらの問題を解決し、すべての新しいプロジェクトに推奨されています。

View Animationはボタンのアニメーションに使用できますか?

使用できますが推奨されません。TranslateAnimation後のボタンは新しい場所に表示されますが、クリックは元の位置で登録されます。ボタンには、Property AnimationまたはJetpack ComposeのAnimatedVisibilityを備えたコンポーザブルを使用してください。

プログラム的にView Animationをキャンセルするには?

view.clearAnimation()を呼び出します。fillAfter=trueでアニメーションが開始された場合、Viewは最終状態を維持します。強制的に元の状態に戻すには、clearAnimation()とinvalidate()を呼び出します。

まとめ

  • View Animation — AndroidのレガシーTweenアニメーションシステム:Alpha、Translate、Scale、Rotateの4タイプ
  • XMLマークアップ res/anim/で要素とルートを使用
  • AnimationSetは共有のstartOffset、duration、interpolatorでアニメーションを組み合わせる
  • クリックミスマッチ — アニメーション後のViewはタッチイベントに対して元の位置に留まる
  • Property Animation — ObjectAnimator、AnimatorSetによる最新の代替、実際のプロパティを操作
  • fillAfterはViewを最終状態に保つが、LayoutParamsは変更しない
  • 移行 Property Animationへの移行はクリックミスマッチを解消し、パフォーマンスを15-20%向上

ターンキー方式のモバイルアプリケーションを開発します

IT Sectrは2017年からスタートアップや企業向けにiOS・Androidアプリケーションを開発しています。私たちがご相談に乗り、最適なソリューションをご提案します。

プロジェクトについて相談

こちらもお読みください