MotionLayout 的使用

一、概述

MotionLayout 是 ConstraintLayout 的子类(ConstraintLayout 的使用可参考这里)。

每个 MotionLayout 需引用一个 MotionScene,其中包含相应布局的所有运动描述。

MotionLayout:布局信息
在这里插入图片描述

MotionScene:运动描述
在这里插入图片描述

二、设置项

2.1 MotionScene 设置项

2.1.1 基础项

<Transition> 包含运动的基本定义。

  • motion:constraintSetStartmotion:constraintSetEnd 指运动的起终点
  • motion:duration 指运动所需的毫秒数

<OnSwipe> 包含运动行为的定义。

  • motion:touchAnchorId 指要拖动的 View

  • motion:touchAnchorSide 指要拖动的 View 的哪一条边(上下左右)

  • motion:dragDirection 指可以拖动的方向

    touchAnchorSidedrugDirection 的关系:

    起点的「side 中点」和终点的「side 中点」组成的向量中,有 direction 方向的向量,才可以 dragDirection,拖动的距离就是该方向上向量的长度。

    如果 side 无变化,会在起始点间跳动。
    如果 view 只是颜色变化,上下左右都没变化,那不会有动画,会在起终点间跳动。

<ConstraintSet> 描述运动起终点的各种约束条件。

2.1.2 可配置属性

在 ConstraintSet 中,可配置的属性包括:

  • 位置
  • 边界
  • alpha
  • visibility
  • elevation
  • rotation、rotationX、rotationY
  • translationX、translationY、translationZ
  • scaleX、scaleY

也可以使用自定义属性:
自定义属性要在 ConstraintSet 中插入 ,并在其中声明:

  • motion:attributeName(属性需要在类中提供相应的 get、set 方法)
  • motion:customColorValue(或 Integer、Float、String、Dimension、Boolean)

2.2 MotionLayout 设置项

app:applyMotionScene="boolean" 是否应用 motionScene

app:showPaths="boolean" 是否显示运动路径

app:progress="float" 指定当前进度

app:currentState="reference" 指定当前 ConstraintSet

app:motionDebug="SHOW_PROGRESS/SHOW_PATH/SHOW_ALL" 用于显示调试信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Android MotionLayout 使用示例: 1. 在 XML 布局文件中添加 MotionLayout: ```xml <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/motion_layout" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutDescription="@xml/scene_main"> <!-- 添加子视图 --> <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </android.support.constraint.motion.MotionLayout> ``` 2. 在 res/xml 目录下创建 MotionScene 文件 scene_main.xml,定义 MotionLayout 的动画场景: ```xml <MotionScene xmlns:android="http://schemas.android.com/apk/res/android" xmlns:motion="http://schemas.android.com/apk/res-auto"> <!-- 定义起始状态 --> <ConstraintSet android:id="@+id/start"> <Constraint android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginStart="16dp" motion:layout_constraintStart_toStartOf="parent" motion:layout_constraintTop_toTopOf="parent" /> </ConstraintSet> <!-- 定义结束状态 --> <ConstraintSet android:id="@+id/end"> <Constraint android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginBottom="16dp" motion:layout_constraintBottom_toBottomOf="parent" motion:layout_constraintEnd_toEndOf="parent" /> </ConstraintSet> <!-- 定义转换过程 --> <Transition motion:constraintSetEnd="@id/end" motion:constraintSetStart="@id/start"> <OnSwipe motion:dragDirection="dragDown" motion:touchAnchorId="@id/text_view" motion:touchAnchorSide="top" /> </Transition> </MotionScene> ``` 3. 在 Activity 或 Fragment 中获取 MotionLayout,并启动动画: ```java MotionLayout motionLayout = findViewById(R.id.motion_layout); motionLayout.transitionToEnd(); ``` 这是一个简单的 Android MotionLayout 使用示例,你可以根据自己的需要进行更复杂的场景和动画定义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值