使用动画移动视图

屏幕上的对象通常需要因用户互动或幕后处理而需要重新定位。您应使用动画将对象从起始位置移动到结束位置,而不是立即更新对象的位置(这会导致对象从一个区域闪烁到另一个区域)。

Android 允许您使用 ObjectAnimator 在屏幕上调整视图对象的位置。您需要提供希望对象停留的结束位置以及动画的时长。您还可以使用时间插值器来控制动画的加速或减速。

使用 ObjectAnimator 更改视图位置

ObjectAnimator API 可用于更改具有指定时长的视图的属性。它包含用于创建 ObjectAnimator 实例的静态方法,具体取决于您要添加动画效果的属性类型。在屏幕上重新定位视图时,请使用 translationXtranslationY 属性。

下例中的 ObjectAnimator 会在 2 秒内将视图移动到距离屏幕左侧 100 像素的位置:

Kotlin

ObjectAnimator.ofFloat(view, "translationX", 100f).apply {
    duration = 2000
    start()
}

Java

ObjectAnimator animation = ObjectAnimator.ofFloat(view, "translationX", 100f);
animation.setDuration(2000);
animation.start();

此示例使用 ObjectAnimator.ofFloat() 方法,因为平移值必须是浮点值。第一个参数是要添加动画效果的视图。第二个参数是要添加动画效果的属性。由于视图需要水平移动,因此使用了 translationX 属性。最后一个参数是动画的结束值。在此示例中,值为 100 表示位置与屏幕左侧相隔多个像素。

下一个方法用于指定动画的播放时长(以毫秒为单位)。在此示例中,动画运行 2 秒(2, 000 毫秒)。

最后一个方法会使动画运行,这会更新视图在屏幕上的位置。

如需详细了解如何使用 ObjectAnimator,请参阅使用 ObjectAnimator 添加动画

添加曲线动作

虽然使用 ObjectAnimator 很方便,但默认情况下,它会沿着起点和终点之间的直线调整视图位置。Material Design 利用曲线来呈现屏幕上对象的空间移动以及动画的播放时间。使用曲线运动可让您的应用更具质感,同时让动画更有趣。

定义您自己的路径

ObjectAnimator 类具有构造函数,可让您一次使用两个或多个属性以及路径为坐标添加动画效果。例如,以下 Animator 使用 Path 对象为视图的 X 和 Y 属性添加动画效果:

Kotlin

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    val path = Path().apply {
        arcTo(0f, 0f, 1000f, 1000f, 270f, -180f, true)
    }
    val animator = ObjectAnimator.ofFloat(view, View.X, View.Y, path).apply {
        duration = 2000
        start()
    }
} else {
    // Create animator without using curved path
}

Java

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  Path path = new Path();
  path.arcTo(0f, 0f, 1000f, 1000f, 270f, -180f, true);
  ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.X, View.Y, path);
  animator.setDuration(2000);
  animator.start();
} else {
  // Create animator without using curved path
}

以下是弧形动画的效果:

图 1. 曲线路径动画。

Interpolator 是缓和曲线的实现。如需详细了解缓和曲线的概念,请参阅 Material Design 文档Interpolator 定义如何根据时间计算动画中的特定值。系统会为 Material Design 规范中的三种基本曲线提供 XML 资源:

  • @interpolator/fast_out_linear_in.xml
  • @interpolator/fast_out_slow_in.xml
  • @interpolator/linear_out_slow_in.xml

使用 PathInterpolator

PathInterpolator 类是 Android 5.0 (API 21) 中引入的插值器。它基于贝塞尔曲线Path 对象。用于实现缓动的 Material Design 文档中的 Android 示例使用 PathInterpolator

PathInterpolator 具有基于不同类型的贝塞尔曲线的构造函数。所有贝塞尔曲线的起点和终点分别固定在 (0,0)(1,1)。其他构造函数参数取决于要创建的贝塞尔曲线的类型。

例如,对于二次贝塞尔曲线,只需要一个控制点的 X 坐标和 Y 坐标:

Kotlin

val myInterpolator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    PathInterpolator(0.67f, 0.33f)
} else {
    LinearInterpolator()
}

Java

Interpolator myInterpolator = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  myInterpolator = new PathInterpolator(0.67f, 0.33f);
} else {
  myInterpolator = new LinearInterpolator();
}

这会生成一条开始快速且在接近结尾时减速的缓和曲线。

同样,三次贝塞尔构造函数也有固定的起点和终点,但它需要两个控制点:

Kotlin

val myInterpolator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    PathInterpolator(0.5f, 0.7f, 0.1f, 1.0f)
} else {
    LinearInterpolator()
}

Java

Interpolator myInterpolator = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  myInterpolator = new PathInterpolator(0.5f, 0.7f, 0.1f, 1.0f);
} else {
  myInterpolator = new LinearInterpolator();
}

这是 Material Design 强调减速缓和曲线的实现。

为了更好地进行控制,可使用任意 Path 来定义曲线:

Kotlin

val myInterpolator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  val path = Path().apply {
    moveTo(0.0f, 0.0f)
    cubicTo(0.5f, 0.7f, 0.1f, 1.0f, 1.0f, 1.0f)
  }
  PathInterpolator(path)
} else {
  LinearInterpolator()
}

Java

Interpolator myInterpolator = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  Path path = new Path();
  path.moveTo(0.0f, 0.0f);
  path.cubicTo(0.5f, 0.7f, 0.1f, 1.0f, 1.0f, 1.0f);
  myInterpolator = new PathInterpolator(path);
} else {
  myInterpolator = new LinearInterpolator();
}

这会生成与三次贝塞尔示例相同的加/减速曲线,但改用了 Path

您也可以将路径插值器定义为 XML 资源:

<pathInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    android:controlX1="0.5"
    android:controlY1="0.7"
    android:controlX2="0.1f"
    android:controlY2="1.0f"/>

创建 PathInterpolator 对象后,您可以将其传递给 Animator.setInterpolator() 方法。Animator 会在启动时使用插值器确定时间曲线或路径曲线。

Kotlin

val animation = ObjectAnimator.ofFloat(view, "translationX", 100f).apply {
    interpolator = myInterpolator
    start()
}

Java

ObjectAnimator animation = ObjectAnimator.ofFloat(view, "translationX", 100f);
animation.setInterpolator(myInterpolator);
animation.start();