创建 MotionScene:在之前的 MotionLayout 示例中,app:layoutDescription 属性引用一个 MotionScene。MotionScene 是一个 XML 资源文件,其中包含相应布局的所有运动描述。为了将布局信息与运动描述分开,每个 MotionLayout 都引用一个单独的 MotionScene。请注意,MotionScene 中的定义优先于 MotionLayout 中的任何类似定义。
下面的示例 MotionScene 文件描述了图 1 中的基本水平运动:
xmlns:motion="http://schemas.android.com/apk/res-auto">
motion:constraintSetStart="@+id/start"
motion:constraintSetEnd="@+id/end"
motion:duration="1000">
motion:touchAnchorId="@+id/button"
motion:touchAnchorSide="right"
motion:dragDirection="dragRight" />
android:id="@+id/button"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginStart="8dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent" />
android:id="@+id/button"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginEnd="8dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintTop_toTopOf="parent" />
请注意以下几点:
包含运动的基本定义。
motion:constraintSetStart 和 motion:constraintSetEnd 指的是运动的端点。这些端点在 MotionScene 后面的 元素中定义。
motion:duration 指定完成运动所需的毫秒数。
可让您通过轻触控制运动。
motion:touchAnchorId 指的是您可以滑动并拖动的视图。
motion:touchAnchorSide 表示我们从右侧拖动视图。
motion:dragDirection 表示拖动的进度方向。例如,motion:dragDirection="dragRight" 表示当您向右拖动时,进度会增加。
是定义描述您的运动的各种限制条件的位置。在此示例中,我们为运动的每个端点定义一个 ConstraintSet。这些端点垂直居中(通过 app:layout_constraintTop_toTopOf="parent" 和 app:layout_constraintBottom_toBottomOf="parent")。在水平方向上,端点位于屏幕最左侧和最右侧。
如需详细了解 MotionScene 支持的各种元素,请参阅 MotionLayout 示例。