码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • MotionLayout的使用


    文章目录

    • 一、前言
    • 二、基本用法
    • 三、参考链接

    一、前言

    MotionLayout可以管理应用中的运动轨迹和控件的动画。因为是ConstraintLayout的子类,所以很多属性是和ConstraintLayout一样的。这个动画相较于属性动画、逐帧动画有更丰富的功能,而且还可以进行拓展,并且可以和CoordinatorLayout进行配合使用。而且用法上会简单很多,有些动画效果完全可以使用MotionLayout而不用去自定义View。可以提高一部分的工作效率,这里记录下基本的使用方式,进行下简单的了解。其余的用法可以在文末查看官方文档

    二、基本用法

    需要添加依赖

    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

    这里先创建一个布局,让里面的TextView做动画效果,注意其id

    activity_montion.xml

    
    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    创建res/xml/scene_01.xml,内容如下,注意其运动的id与上述布局运动的id保持一致

    
    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    以下来自官方文档

    请注意以下几点:

    • 包含运动的基本定义。

      • motion:constraintSetStart 和 motion:constraintSetEnd 指的是运动的端点。这些端点在 MotionScene 后面的 元素中定义。

      • motion:duration 指定完成运动所需的毫秒数。

    • 可让您通过轻触控制运动。

      • motion:touchAnchorId 指的是您可以滑动并拖动的视图。

      • motion:touchAnchorSide 表示我们从右侧拖动视图。

      • motion:dragDirection 表示拖动的进度方向。例如,motion:dragDirection="dragRight" 表示当您向右拖动时,进度会增加。

    • 是定义描述您的运动的各种限制条件的位置。在此示例中,我们为运动的每个端点定义一个 ConstraintSet。这些端点垂直居中(通过 app:layout_constraintTop_toTopOf="parent" 和 app:layout_constraintBottom_toBottomOf="parent")。在水平方向上,端点位于屏幕最左侧和最右侧。

    这里需要注意motion:touchAnchorId和Constraint中的android:id与布局中运动的控件的id保持一致

    然后直接在Activity中加载布局,加载后,手势拖动控件会发现出现动画效果

    三、参考链接

    1. 使用 MotionLayout 管理运动和微件动画

    2. MotionLayout 参考文档 | Android 开发者 | Android Developers

    3. 官方的MotionLayout动画示例

    4. github上使用MotionLayout做的视差动画

    5. Android MotionLayout详解

    6. KeyCycles的使用(可以使运动轨迹使用波形轨迹,可视化操作,可以导出代码)

  • 相关阅读:
    基于SSM+MySQL+Boostrap简单的员工信息管理系统
    你必须知道的十大漏洞之失效的访问控制
    第二&三章-项目运行环境&项目经理角色
    Pycharm2024版,更换安装源
    w010基于Springboot大学生入学审核系统的设计与实现
    【Rust日报】2023-11-08 RustyVault -- 基于 rust 的现代秘密管理系统
    [附源码]Python计算机毕业设计本科生外出请假管理信息系统
    前端js实现将数组对象组装成自己需要的属性,或者去掉对象中不必要的属性
    Yolov5的类激活图
    【LeetCode题目详解】第十章 单调栈part01 739. 每日温度 ● 496.下一个更大元素 I (day58补)
  • 原文地址:https://blog.csdn.net/Mr_Tony/article/details/126136752
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号