MotionLayout 运动动画
MotionLayout 简介
Section titled “MotionLayout 简介”MotionLayout 是 ConstraintLayout 的子类,在丰富的约束布局能力之上构建而成,缩小了普通布局转换与复杂交互动画之间的差距,并在属性动画框架、TransitionManager 和 CoordinatorLayout 之间提供了统一能力。
MotionLayout 通过 app:layoutDescription 属性引用一个独立的 MotionScene XML 资源文件,文件中集中描述了该布局的所有运动状态与过渡过程。
MotionScene 核心标签与属性
Section titled “MotionScene 核心标签与属性”1. <Transition>(过渡定义)
Section titled “1. <Transition>(过渡定义)”包含一次运动过渡的基本起止端点与时长定义:
motion:constraintSetStart/motion:constraintSetEnd:运动的起点与终点状态集。motion:duration:完成该次过渡运动所需的时长(单位:ms)。
2. <OnSwipe>(手势拖动触发)
Section titled “2. <OnSwipe>(手势拖动触发)”嵌套在 <Transition> 内部,用于响应用户的触摸滑动交互:
motion:touchAnchorId:跟随手指拖动的锚点视图 ID。motion:touchAnchorSide:从视图的哪一侧开始计算拖动。motion:dragDirection:使动画进度增加的拖动方向。
3. <ConstraintSet> 与 <Constraint>
Section titled “3. <ConstraintSet> 与 <Constraint>”<ConstraintSet>:定义一组完整的约束状态集(如起始状态、结束状态)。<Constraint>:写在<ConstraintSet>内部,描述单个视图在该状态下的位置、尺寸与可见性约束。
4. <CustomAttribute>(自定义属性插值)
Section titled “4. <CustomAttribute>(自定义属性插值)”写在 <Constraint> 内部,用于对视图的自定义属性做过渡插值(每个 <CustomAttribute> 包含两个属性):
motion:attributeName(必需):属性名称,必须对应对象上存在的标准 getter/setter 方法(例如backgroundColor)。- 另一项从以下对应类型的值属性中选择:
motion:customColorValue:颜色值motion:customIntegerValue:整数值motion:customFloatValue:浮点值motion:customStringValue:字符串值motion:customDimension:尺寸值motion:customBoolean:布尔值