当前位置:网站首页>Use of MotionLayout
Use of MotionLayout
2022-08-04 07:32:00 【Mr_Tony】
一、前言
MotionLayout
Can manage the animation of motion tracks and controls in the app.因为是ConstraintLayout
的子类,So many properties are andConstraintLayout
一样的.This animation is compared to property animation、Frame-by-frame animation has more features,And it can also be expanded,并且可以和CoordinatorLayout
进行配合使用.And it will be much simpler to use,Some animation effects are fully functionalMotionLayout
instead of customizingView.Part of the work efficiency can be improved,The basic usage is recorded here,Do a simple understanding.The rest of the usage can be viewed in the official documentation at the end of the article
二、基本用法
需要添加依赖
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
First create a layout here,让里面的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
,内容如下,pay attention to its movementidwith the above layout movementid保持一致
<?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>
Allows you to control movement with a touch.
motion:touchAnchorId
指的是您可以滑动并拖动的视图.
motion:touchAnchorSide
表示我们从右侧拖动视图.
motion:dragDirection
表示拖动的进度方向.例如,motion:dragDirection="dragRight"
表示当您向右拖动时,进度会增加.
<ConstraintSet>
is where the various constraints that describe your movement are defined.在此示例中,We define one for each endpoint of the motionConstraintSet
.These endpoints are centered vertically(通过app:layout_constraintTop_toTopOf="parent"
和app:layout_constraintBottom_toBottomOf="parent"
).在水平方向上,The endpoints are on the far left and far right of the screen.
这里需要注意motion:touchAnchorId
和Constraint
中的android:id
With controls in motion in the layoutid保持一致
然后直接在Activity
中加载布局,加载后,Gesture dragging the control will find an animation effect
三、参考链接
边栏推荐
猜你喜欢
随机推荐
DropBlock: Regularization method and reproduction code for convolutional layers
HbuilderX 启动微信小程序 无法打开项目
IDEA 控制台 中文乱码问题(如果网上教程都无法解决你的问题的话)
专属程序员的浪漫七夕
千古第一文人苏轼的众CP
mysql锁机制
创建数据库报错--MySQL server is running with the --super-read-only option
对产品设计,架构设计的一点思考
MAML principle explanation and code implementation
【深度学习实践(二)】上手手写数字识别
Detailed ResNet: What problem is ResNet solving?
unity webgl报 Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON
开发小技巧 navicate如何点击单元格显示全部的文本内容或通过图像查看内容
手把手教你Charles抓包工具使用
matlab让我的旧手机起死回生
a标签下载图片,不要预览
分布式计算实验1 负载均衡
JVM 快速检测死锁
零分贝超静音无线鼠标!数量有限!!先到先得!!!【元旦专享】
MySQL内存淘汰策略