View Animation — 是什么,Android中的Tween动画

作者: IT Sectr 发布日期: 2026-03-01 阅读时间: 8 分钟

View Animation — 一个过时的Android API 1+动画系统,通过Tween Animation(Alpha、Translate、Scale、Rotate)操作View对象。Google为所有新项目推荐Property Animation(Animator)。本文解释了其原理、类型、XML配置以及弃用原因。

要点

  • View Animation — 通过Tween Animation实现的View动画:Alpha、Translate、Scale、Rotate
  • XML定义 — 动画在res/anim/中使用描述
  • AnimationSet — 多个动画组合在一个集合中,具有共同参数
  • 限制 — View Animation不改变属性,只改变渲染:View保持在原始位置
  • Property Animation — 现代替代方案(Animator),与对象的真实属性一起工作

什么是View Animation?

View Animation — Android的动画子系统,在View级别工作(API 1+)。它使用四种Tween Animation:AlphaAnimation(透明度)、TranslateAnimation(平移)、ScaleAnimation(缩放)和RotateAnimation(旋转)。每个动画在res/anim/文件夹的XML文件中描述,或在Kotlin代码中以编程方式创建。

View Animation机制不改变View的真实属性——它在新的位置、缩放或透明度下重新绘制View,但onTouchEvent()回调接收原始位置的坐标。这是一个关键限制,称为点击不匹配(click mismatch)。

根据Android Developer Documentation(2026),Google从Android 3.0(API 11)开始将View Animation标记为过时的API。对于新项目,推荐使用Property Animation——Animator、AnimatorSet和ObjectAnimator,它们通过setter方法与对象的真实属性一起工作。

Tween动画的类型

Tween Animation — “之间”关键帧的动画:您设置起始和结束状态,系统计算中间帧。Android提供四种标准类型,每种都有自己的参数集。

AlphaAnimation

AlphaAnimation将View的透明度从fromAlpha更改为toAlpha。值从0.0(完全透明)到1.0(完全可见)。默认情况下,动画持续300毫秒。参数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将宽度加倍。参数pivotType确定相对于什么计算pivot:self、parent或absolute。缩放动画不改变View的真实尺寸——只改变渲染。

RotateAnimation

RotateAnimation围绕pivot点将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)或四种类型之一。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(毫秒)、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年具有狭窄的应用范围:支持Android 4.x(API 16-19),其中Property Animation可能不稳定;不需要更改LayoutParams的简单出现/消失效果;点击不匹配不重要的动画(例如背景元素)。对于所有其他情况,Google推荐迁移到ObjectAnimator或AnimatorSet。

根据Android Developers,Property Animation在内存使用方面效率提高15-20%,因为它不创建中间canvas帧。从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过时。原因——点击不匹配(click mismatch)以及无法动画化对象的任意属性。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
  • XML定义在res/anim/中,使用元素和根
  • AnimationSet将动画与共同的startOffset、duration、interpolator组合
  • 点击不匹配 — 动画后View在触摸事件的原始位置保持不变
  • Property Animation — 通过ObjectAnimator、AnimatorSet的现代替代方案,与真实属性一起工作
  • fillAfter使View保持在最终状态,但不改变LayoutParams
  • 迁移到Property Animation消除了点击不匹配并将性能提高15-20%

我们将开发一款交钥匙移动应用程序

IT Sectr自2017年以来为初创企业和企业打造iOS和Android应用程序。我们将为您提供咨询并提出最佳解决方案。

讨论项目

另请阅读