当前位置:网站首页>MotionLayout的使用
MotionLayout的使用
2022-08-04 06:32:00 【Mr_Tony】
一、前言
MotionLayout
可以管理应用中的运动轨迹和控件的动画。因为是ConstraintLayout
的子类,所以很多属性是和ConstraintLayout
一样的。这个动画相较于属性动画、逐帧动画有更丰富的功能,而且还可以进行拓展,并且可以和CoordinatorLayout
进行配合使用。而且用法上会简单很多,有些动画效果完全可以使用MotionLayout
而不用去自定义View。可以提高一部分的工作效率,这里记录下基本的使用方式,进行下简单的了解。其余的用法可以在文末查看官方文档
二、基本用法
需要添加依赖
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
这里先创建一个布局,让里面的TextView
做动画效果,注意其id
activity_montion.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutDescription="@xml/scene_01" tools:context=".MotionActivity">
<TextView android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题" android:layout_marginTop="100dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.motion.widget.MotionLayout>
创建res/xml/scene_01.xml
,内容如下,注意其运动的id与上述布局运动的id保持一致
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android" xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition motion:constraintSetStart="@+id/start" motion:constraintSetEnd="@+id/end" motion:duration="1000">
<OnSwipe motion:touchAnchorId="@+id/button" motion:touchAnchorSide="right" motion:dragDirection="dragRight" />
</Transition>
<ConstraintSet android:id="@+id/start">
<Constraint 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" />
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint 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" />
</ConstraintSet>
</MotionScene>
以下来自官方文档
请注意以下几点:
<Transition>
包含运动的基本定义。
motion:constraintSetStart
和motion:constraintSetEnd
指的是运动的端点。这些端点在 MotionScene 后面的<ConstraintSet>
元素中定义。
motion:duration
指定完成运动所需的毫秒数。
<OnSwipe>
可让您通过轻触控制运动。
motion:touchAnchorId
指的是您可以滑动并拖动的视图。
motion:touchAnchorSide
表示我们从右侧拖动视图。
motion:dragDirection
表示拖动的进度方向。例如,motion:dragDirection="dragRight"
表示当您向右拖动时,进度会增加。
<ConstraintSet>
是定义描述您的运动的各种限制条件的位置。在此示例中,我们为运动的每个端点定义一个ConstraintSet
。这些端点垂直居中(通过app:layout_constraintTop_toTopOf="parent"
和app:layout_constraintBottom_toBottomOf="parent"
)。在水平方向上,端点位于屏幕最左侧和最右侧。
这里需要注意motion:touchAnchorId
和Constraint
中的android:id
与布局中运动的控件的id保持一致
然后直接在Activity
中加载布局,加载后,手势拖动控件会发现出现动画效果
三、参考链接
边栏推荐
猜你喜欢
MMDeploy部署实战系列【第四章】:onnx,tensorrt模型推理
mysql基础(4)
开发小技巧 navicate如何点击单元格显示全部的文本内容或通过图像查看内容
如何用matlab做高精度计算?【第一辑】
HbuilderX 启动微信小程序 无法打开项目
串口监听 - 软件方案
Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
Centos通过Docker搭建MySQL的PXC集群
花了近70美元入手的学生版MATLAB体验到底如何?
七夕送礼,心愿直抵!
随机推荐
两日总结八
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
原型图总结规范
Triton部署mmdeploy导出的TensorRT模型失败篇
C语言实现-华为太空人手表
设置el-table自动向下滑动(不多解释,直接代码实现)
fanuc机器人IO分配报警信号分配无效
unity webgl报 Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON
经典宋诗排行榜
JVM 快速检测死锁
舍不得花钱买1stOpt,不妨试试这款免费的拟合优化神器【openLU】
千古第一文人苏轼的众CP
Amazon亚马逊 Vendor Central Label详解
NelSon:一款新的适配matlab编程语法的编程工具
什么是多态。
两日总结七
LAN技术-3iStack
缓存穿透、击穿、雪崩
打破千篇一律,DIY属于自己独一无二的商城
MySQL重置root密码