MotionLayout简介(动画的一种实现方式)?
MotionLayout 是一个 Google出品用于制作过渡动画的框架。用来它就能轻松的做出一些较为复杂的动画效果。MotionLayout 是约束布局ConstraintLayout的子类。
MotionLayout最低支持到 Android 4.3(API 18),还有就是MotionLayout 是ConstraintLayout 2.0添加的,因此必须确保支持库的版本不低于 2.0。
MotionLayout 与 ConstraintLayout不同的是:
MotionLayout 需要链接到一个MotionScene 文件。使用MotionLayout的app:layoutDescription属性将 MotionLayout 链接到一个MotionScene 文件。
1.开始使用MotionLayout
添加支持库:
如果您使用了 AndroidX
dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1'
}
如果您没有使用 AndroidX
dependencies {
implementation 'com.android.support.constraint:constraint-layout:2.0.0-beta1'
}
由于 MotionLayout 类继承自 ConstraintLayout 类,因此可以在布局中使用 MotionLayout替换掉ConstraintLayout
2.MotinScene介绍
MotionScene 是一个 XML 资源文件,其中包含相应布局的所有运动描述。为了将布局信息与运动描述分开,每个 MotionLayout 都引用一个单独的 MotionScene。请注意,MotionScene 中的定义优先于 MotionLayout 中的任何类似定义。
MotionScene文件描述了两个场景间的过渡动画,存放在 res/xml 目录下。
要使用 MotionLayout 创建过渡动画,你需要创建两个 layout 布局文件来描述两个不同场景的属性。当从一个场景切换到另一个场景时,MotionLayout 框架会自动检测这两个场景中具有相同 id 的 View 的属性差别,然后针对这些差别属性应用过渡动画(类似于 TransitionManger)
MotionLayout 框架支持的标准属性:MationLayout 除了支持下面列出的标准属性外,还支持全部的 ConstraintLayout 属性。
android:visibility
android:alpha
android:elevation
android:rotation
android:rotationX
android:rotationY
android:scaleX
android:scaleY
android:translationX
android:translationY
android:translationZ