添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

MotionLayout 是 ConstraintLayout 2.0 版本引入进来的,目前还在测试版本中,但感觉还挺有意思的,就写一篇记录一下。

MotionLayout 类继承自 ConstraintLayout 类,允许你为各种状态之间的布局设置过渡动画。由于 MotionLayout 继承了 ConstraintLayout,因此可以直接在 XML 布局文件中使用 MotionLayout 替换 ConstraintLayout。MotionLayout 是完全声明式的,你可以完全在 XML 文件中描述一个复杂的过渡动画而无需任何代码。

MotionLayout 与 ConstraintLayout 不同的是 MotionLayout 需要链接到一个 MotionScene 文件。使用 MotionLayout 的 app:layoutDescription 属性将 MotionLayout 链接到一个 MotionScene 文件。另外, MotionLayout 所有的直接子 View 需要指定 id ,不然会报错:

1
All children of ConstraintLayout must have ids to use ConstraintSet.

  • 起始状态:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    <android.support.constraint.motion.MotionLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/motionLayout"
    app:layoutDescription="@xml/activity_main_motion_scene">

    <TextView
    android:id="@+id/button"
    android:layout_width="100dp"
    android:layout_height="56dp"
    android:text="这个View不动"
    android:textColor="#fff"
    android:gravity="center"
    android:background="#f00"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"/>

    <ImageView
    android:id="@+id/image"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:src="@mipmap/ic_launcher"
    android:background="@color/colorPrimary"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1"/>

    </android.support.constraint.motion.MotionLayout>
  • 结束状态:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <android.support.constraint.motion.MotionLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/motionLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_main_motion_scene">

    <ImageView
    android:id="@+id/image"
    android:layout_width="48dp"
    android:layout_height="48dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="100dp"
    app:layout_constraintTop_toTopOf="parent"/>

    </android.support.constraint.motion.MotionLayout>
  • MotionScene:文件名 activity_main_motion_scene.xml,存放在 res/xml 目录下。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <?xml version="1.0" encoding="utf-8"?>
    <MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Transition
    app:constraintSetStart="@layout/activity_main_scene1"
    app:constraintSetEnd="@layout/activity_main_scene2"
    app:duration="1000">

    <OnClick
    app:clickAction="toggle"
    app:targetId="@id/image" />

    </Transition>

    </MotionScene>

    看下效果:

    属性讲解

    MotionLayout 会自动检测这两个场景中具有相同 id 的 View 的属性差别,然后针对这些差别属性应用过渡动画。
    MotionLayout 除了支持一下的标准属性外,还支持全部的 ConstraintLayout 属性。

  • android:visibility
  • android:alpha
  • android:elevation
  • android:rotation
  • android:rotationX
  • android:rotationY
  • android:scaleX
  • android:scaleY
  • android:translationX
  • android:translationY
  • android:translationZ
  • MotionScene 文件的根元素是 MotionScene。在 MotionScene 元素中使用 Transition 子元素来描述一个过渡,使用 Transition 元素的 app:constraintSetStart 属性指定起始场景的布局文件,使用 app:constraintSetEnd 指定结束场景的布局文件。在 Transition 元素中使用 OnClick 或者 OnSwip 子元素来描述过渡的触发条件。使用 app:duration 指定动画时间, app:motionInterpolator 指定差值器,取值有 linear bounce 等等。

    OnClick 元素的属性:

  • app:targetId:Id 值,设置用来触发过渡的那个 View 的 Id,例如:@id/image。
  • app:clickAction:设置点击时执行的动作,有 5 个可选项:
  •