View Animationは、Tween Animation(Alpha、Translate、Scale、Rotate)を介してViewオブジェクトを操作するAndroid API 1+のレガシーアニメーションシステムです。Googleはすべての新しいプロジェクトにProperty Animation(Animator)を推奨しています。この記事では、その原理、種類、XMLマークアップ、および非推奨の理由について説明します。
重要なポイント
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はキーフレーム“間”のアニメーションです:開始状態と終了状態を定義すると、システムが中間フレームを計算します。Androidは4つの標準タイプを提供し、それぞれに独自のパラメーターセットがあります。
AlphaAnimationはViewの透明度をfromAlphaからtoAlphaまで変更します。値は0.0(完全に透明)から1.0(完全に表示)までです。デフォルトでは、アニメーションは300 ms続きます。fillAfter=trueパラメーターはViewを最終的な透明度状態に保ちます。
<!-- 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はViewを開始位置(fromXDelta、fromYDelta)から終了位置(toXDelta、toYDelta)に移動します。値はピクセル、親のパーセンテージ、またはView自体のパーセンテージで指定されます。たとえば、100%はViewの幅の100%を意味します。
ScaleAnimationはpivotX/pivotYポイントを基準にViewを拡大縮小します。fromXScale=1.0、toXScale=2.0は幅を2倍にします。pivotTypeパラメーターは、ピボットが何を基準に計算されるかを決定します:self、parent、またはabsolute。拡大縮小アニメーションはViewの実際の寸法を変更せず、レンダリングのみを変更します。
RotateAnimationはViewをピボットポイントを中心にfromDegreesからtoDegreesの角度で回転します。角度は度で測定されます:0は元の位置、360は完全な回転です。無限回転にはrepeatCount=INFINITEを使用します。
View AnimationとProperty Animation(Animator)の違いは、Androidアニメーションアーキテクチャにおける根本的な違いです。View Animationはレンダリングのみを操作しますが、Property Animationはsetterメソッドを通じて実際のオブジェクトプロパティを変更し、レイアウトの更新につながります。
| 基準 | View Animation | Property Animation |
|---|---|---|
| APIレベル | API 1+(非推奨) | API 11+(推奨) |
| アニメーション対象 | Viewのレンダリング(canvas) | オブジェクトのプロパティ(setter) |
| アニメーション後のクリック | 元の位置 | 新しい位置 |
| アニメーションタイプ | 4 Tween + AnimationSet | ObjectAnimator、ValueAnimator、AnimatorSet |
| LayoutParams | 変更しない | setLayoutParamsで変更 |
| パフォーマンス | 高い(canvasのみ) | 中程度(setterに依存) |
View Animationは通常、res/anim/の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がアニメーションの最終状態を維持するかどうかを決定します。
アニメーションは通常XMLで定義されますが、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は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はViewのレンダリングのみを変更します — アニメーション後、Viewはタッチイベントに対して元の位置に留まります。Property Animationはオブジェクトの実際のプロパティを変更し、LayoutParamsを更新して新しい位置でのクリックを正しく処理します。
GoogleはAndroid 3.0(API 11)以降、View Animationを非推奨と宣言しました。理由はクリックミスマッチと、任意のオブジェクトプロパティをアニメーション化できないことです。Property Animationはこれらの問題を解決し、すべての新しいプロジェクトに推奨されています。
使用できますが推奨されません。TranslateAnimation後のボタンは新しい場所に表示されますが、クリックは元の位置で登録されます。ボタンには、Property AnimationまたはJetpack ComposeのAnimatedVisibilityを備えたコンポーザブルを使用してください。
view.clearAnimation()を呼び出します。fillAfter=trueでアニメーションが開始された場合、Viewは最終状態を維持します。強制的に元の状態に戻すには、clearAnimation()とinvalidate()を呼び出します。
まとめ
ターンキー方式のモバイルアプリケーションを開発します
IT Sectrは2017年からスタートアップや企業向けにiOS・Androidアプリケーションを開発しています。私たちがご相談に乗り、最適なソリューションをご提案します。