MontionLayout:打开动画新世界大门(其一,附小技巧

2024-02-29 05:20

本文主要是介绍MontionLayout:打开动画新世界大门(其一,附小技巧,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

MotionLayout 基础


首先,我们需要从 MotionLayout 的一些基本属性和用法讲起,这样对于我们后面的实际操作将会很有帮助。

引入 MotionLayout 库

1dependencies {

2    implementation ‘com.android.support.constraint:constraint-layout:2.0.0-beta2’

3}

4复制代码

目前,MotionLayout 仍处于 beta 版本,虽然官方之前说过 MotionLayout 的动画辅助工具将会在 beta``ConstraintLayout

在布局文件中使用 MotionLayout

想要使用 MotionLayout,只需要在布局文件中作如下声明即可:

1<android.support.constraint.motion.MotionLayout

2        xmlns:android=“http://schemas.android.com/apk/res/android”

3        xmlns:tools=“http://schemas.android.com/tools”

4        xmlns:app=“http://schemas.android.com/apk/res-auto”

5        android:layout_width=“match_parent”

6        android:layout_height=“match_parent”

7        app:layoutDescription="@xml/scene1">

8

9</android.support.constraint.motion.MotionLayout>

10复制代码

由于 MotionLayout 作为 ConstraintLayout 的子类,那么就自然而然地可以像 ConstraintLayout``Motio![](https://www.hualigs.cn/image/61dba891ed8ee.jpg) nLayout 的用处可远不止这些。我们先来看看 MotionLayout 的构成:

640?wx_fmt=png

由上图可知,MotionLayout 可分为 <View> 和 <Helper> 两个部分。 <View> 部分可简单理解为一个ConstraintLayout,至于 <Helper> 其实就是我们的“动画层”了。MotionLayout 为我们提供了layoutDescription 属性,我们需要为它传入一个 MotionScene 包裹的 XML

MotionScene:传说中的“百宝袋”


什么是 MotionScene?结合上图 MotionScene 主要由三部分组成: StateSet 、ConstraintSet 和 Transition 。为了让大家快速理解和使用 MotionScene,本文将重点讲解ConstarintSet 和 Transition,至于 StateSet

首先,我们从实现下面这个简单的效果讲起:

640?wx_fmt=gif

GIF 画质有点渣,见谅,但从上图我们可以发现这是一个简单的平移动画,通过点击自身(篮球)来触发,让我们来通过 MotionLayout

1<?xml version=“1.0” encoding=“utf-8”?>

2<android.support.constraint.motion.MotionLayout

3        xmlns:android=“http://schemas.android.com/apk/res/android”

4        xmlns:tools=“http://schemas.android.com/tools”

5        xmlns:app=“http://schemas.android.com/apk/res-auto”

6        android:layout_width=“match_parent”

7        android:layout_height=“match_parent”

8        app:layoutDescription="@xml/step1"

9        tools:context=".practice.MotionSampleActivity">

10    <ImageView

11        android:id="@+id/ball"

12        android:layout_width=“wrap_content”

13        android:layout_height=“wrap_content”

14        android:src="@drawable/ic_basketball"/>

15</android.support.constraint.motion.MotionLayout>

16复制代码

布局文件很简单,只不过你可能会注意到,我们对 ImageView 并没有添加任何约束,原因在于:我们会在 MotionScene 中声明ConstraintSet,里面将包含该 ImageView 的“运动”起始点和终点的约束信息。当然你也可以在布局文件中对其加以约束,MotionScene 中对于控件约束的优先级会高于布局文件中的设定 。这里我们通过 layoutDescription 来为MotionLayout 设置它的 MotionScene 为 step1,接下来就让我们一睹 MotionScene 的芳容:

1<?xml version=“1.0” encoding=“utf-8”?>

2<!–describe the animation for activity_motion_sample_step1.xml–>

3<MotionScene xmlns:android=“http://schemas.android.com/apk/res/android”

4             xmlns:app=“http://schemas.android.com/apk/res-auto”>

5    <!-- A transition describes an animation via start and end state -->

6    <Transition

7        app:constraintSetStart="@id/start"

8        app:constraintSetEnd="@id/end"

9        app:duration=“2200”>

10        <OnClick

11                app:targetId="@id/ball"

12                app:clickAction=“toggle” />

13    </Transition>

14

15    <!-- Constraints to apply at the start of the animation -->

16    <ConstraintSet android:id="@+id/start">

17        <Constraint

18                android:id="@+id/ball"

19                android:layout_width=“48dp”

20                android:layout_height=“48dp”

21                android:layout_marginStart=“12dp”

22                android:layout_marginTop=“12dp”

23                app:layout_constraintStart_toStartOf=“parent”

24                app:layout_constraintTop_toTopOf=“parent”/>

25    </ConstraintSet>

26

27    <!-- Constraints to apply at the end of the animation -->

28    <ConstraintSet android:id="@+id/end">

29        <Constraint

30                android:id="@+id/ball"

31                android:layout_width=“48dp”

32                android:layout_height=“48dp”

33                android:layout_marginEnd=“12dp”

34                android:layout_marginBottom=“12dp”

35                app:layout_constraintEnd_toEndOf=“parent”

36                app:layout_constraintBottom_toBottomOf=“parent”/>

37    </ConstraintSet>

38

39</MotionScene>

40复制代码

首先,可以发现我们定义了两个 <ConstraintSet> ,分别描述了这个? ImageView<Transition> 元素了。事实上,我们都知道,动画都是有开始位置和结束位置的,而 MotionLayoutPath

回到上面这个例子,我们只需要为 Transition 设置起始位置和结束位置的 ConstraintSet 并设置动画时间即可,剩下的都交给MotionLayout 自动去帮我们完成。当然你也可以通过 onClick 点击事件来触发动画,绑定目标控件的 id 以及通过clickAction 属性来设置点击事件的类型,这里我们设置的是toggle,即通过反复点击控件来切换动画的状态,其他还有很多属性可以参照官方文档去研究,比较简单,这里就不一一讲解它们的效果了。如此一来,运行一下就能看到上面的效果了。另外,为了方便测试,我们可以给MotionLayout 加上调试属性: app:motionDebug="SHOW_PATH" ,然后就能轻易的查看其动画内部的运动轨迹:

640?wx_fmt=gif

什么?你说这个动画效果太基础?那好,我就来个简陋版的“百花齐放”效果吧,比如下面这样:

640?wx_fmt=gif

首先,让我们分析一下这个效果:仔细看我们可以发现,通过向上滑动蓝色的 Android

1<?xml version=“1.0” encoding=“utf-8”?>

2<android.support.constraint.motion.MotionLayout

3        xmlns:android=“http://schemas.android.com/apk/res/android”

4        xmlns:tools=“http://schemas.android.com/tools”

5        xmlns:app=“http://schemas.android.com/apk/res-auto”

6        android:layout_width=“match_parent”

7        android:layout_height=“match_parent”

8        app:motionDebug=“SHOW_PATH”

9        app:layoutDescription="@xml/step2"

10        tools:context=".practice.MotionSampleActivity">

11    <ImageView

12            android:id="@+id/ic_android_blue"

13            android:layout_width=“42dp”

14            android:layout_height=“42dp”

15            android:src="@mipmap/android_icon_blue"/>

16    <ImageView

17            android:id="@+id/ic_android_left"

18            android:layout_width=“42dp”

19            android:layout_height=“42dp”

20            android:src="@mipmap/android_icon_purple"/>

21    <ImageView

22            android:id="@+id/ic_android_right"

23            android:layout_width=“42dp”

24            android:layout_height=“42dp”

25            android:src="@mipmap/android_icon_orange"/>

26    <TextView

27            android:id="@+id/tipText"

28            android:text=“Swipe the blue android icon up”

29            android:layout_width=“wrap_content”

30            android:layout_height=“wrap_content”

31            app:layout_constraintEnd_toEndOf=“parent”

32            android:layout_marginEnd=“16dp”

33            android:layout_marginTop=“16dp”

34            app:layout_constraintTop_toTopOf=“parent”/>

35</android.support.constraint.motion.MotionLayout>

36复制代码

下面我们来看下 step2 中的 MotionScene:

1<?xml version=“1.0” encoding=“utf-8”?>

2<!–describe the animation for activity_motion_sample_step2.xml–>

3<!–animate by dragging target view–>

4<MotionScene xmlns:android=“http://schemas.android.com/apk/res/android”

5             xmlns:app=“http://schemas.android.com/apk/res-auto”>

6    <!–At the start, all three stars are centered at the bottom of the screen.–>

7    <ConstraintSet android:id="@+id/start">

8        <Constraint

9                android:id="@+id/ic_android_blue"

10                android:layout_width=“42dp”

11                android:layout_height=“42dp”

12                android:layout_marginBottom=“20dp”

13                app:layout_constraintStart_toStartOf=“parent”

14                app:layout_constraintEnd_toEndOf=“parent”

15                app:layout_constraintBottom_toBottomOf=“parent”/>

16        <Constraint

17                android:id="@+id/ic_android_left"

18                android:layout_width=“42dp”

19                android:layout_height=“42dp”

20                android:alpha=“0.0”

21                android:layout_marginBottom=“20dp”

22                app:layout_constraintStart_toStartOf=“parent”

23                app:layout_constraintEnd_toEndOf=“parent”

24                app:layout_constraintBottom_toBottomOf=“parent”/>

25        <Constraint

26                android:id="@+id/ic_android_right"

27                android:layout_width=“42dp”

28                android:layout_height=“42dp”

29                android:layout_marginBottom=“20dp”

30                android:alpha=“0.0”

31                app:layout_constraintStart_toStartOf=“parent”

32                app:layout_constraintEnd_toEndOf=“parent”

33                app:layout_constraintBottom_toBottomOf=“parent”/>

34    </ConstraintSet>

35

36    <!–Define the end constraint to set use a chain to position all three stars together below @id/tipText.–>

37    <ConstraintSet android:id="@+id/end">

38        <Constraint

39                android:id="@+id/ic_android_left"

40                android:layout_width=“58dp”

41                android:layout_height=“58dp”

42                android:layout_marginEnd=“90dp”

43                android:alpha=“1.0”

44                app:layout_constraintHorizontal_chainStyle=“packed”

45                app:layout_constraintStart_toStartOf=“parent”

46                app:layout_constraintEnd_toStartOf="@id/ic_android_blue"

47                app:layout_constraintTop_toBottomOf="@id/tipText"/>

48        <Constraint

49                android:id="@+id/ic_android_blue"

50                android:layout_width=“58dp”

51                android:layout_height=“58dp”

52                app:layout_constraintEnd_toStartOf="@id/ic_android_right"

53                app:layout_constraintStart_toEndOf="@id/ic_android_left"

54                app:layout_constraintTop_toBottomOf="@id/tipText"/>

55        <Constraint

56                android:id="@+id/ic_android_right"

57                android:layout_width=“58dp”

58                android:layout_height=“58dp”

59                android:layout_marginStart=“90dp”

60                android:alpha=“1.0”

61                app:layout_constraintStart_toEndOf="@id/ic_android_blue"

62                app:layout_constraintEnd_toEndOf=“parent”

63                app:layout_constraintTop_toBottomOf="@id/tipText"/>

64    </ConstraintSet>

65    <!-- A transition describes an animation via start and end state -->

66    <Transition

67            app:constraintSetStart="@id/start"

68            app:constraintSetEnd="@id/end">

69        <!-- MotionLayout will track swipes relative to this view -->

70        <OnSwipe app:touchAnchorId="@id/ic_android_blue"/>

71    </Transition>

72</MotionScene>

73复制代码

上面代码其实很好理解,之前我们定义了一个控件的 Constraint,现在只需要多加两个即可。由于三个 AndroidConstraintLayout 的基础,就不多说了。接着将结束位置的左、右 Android 机器人透明度设置为MotionLayout 会自动处理目标控件 alpha 属性的变化效果,让其看起来依旧丝滑。

另外,我们这里没有再通过 <OnClick> 来触发动画效果,类似的,我们使用了 <OnSwipe> 手势滑动来触发动画,只需要指定touchAnchorId 为蓝色小机器人即可,怎么样,是不是有种“拍案惊奇”的感觉?。此外,你可以通过指定 touchAnchorSide 和dragDirection

到这里,你可能会说:前面两个示例的动画轨迹一直是"直线",如果想要某段动画过程的轨迹是"曲线"效果可以吗?当然没问题! Keyframes

这篇关于MontionLayout:打开动画新世界大门(其一,附小技巧的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/757691

相关文章

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

Pandas中多重索引技巧的实现

《Pandas中多重索引技巧的实现》Pandas中的多重索引功能强大,适用于处理多维数据,本文就来介绍一下多重索引技巧,具有一定的参考价值,感兴趣的可以了解一下... 目录1.多重索引概述2.多重索引的基本操作2.1 选择和切片多重索引2.2 交换层级与重设索引3.多重索引的高级操作3.1 多重索引的分组聚

Redis多种内存淘汰策略及配置技巧分享

《Redis多种内存淘汰策略及配置技巧分享》本文介绍了Redis内存满时的淘汰机制,包括内存淘汰机制的概念,Redis提供的8种淘汰策略(如noeviction、volatile-lru等)及其适用场... 目录前言一、什么是 Redis 的内存淘汰机制?二、Redis 内存淘汰策略1. pythonnoe

怎么关闭Ubuntu无人值守升级? Ubuntu禁止自动更新的技巧

《怎么关闭Ubuntu无人值守升级?Ubuntu禁止自动更新的技巧》UbuntuLinux系统禁止自动更新的时候,提示“无人值守升级在关机期间,请不要关闭计算机进程”,该怎么解决这个问题?详细请看... 本教程教你如何处理无人值守的升级,即 Ubuntu linux 的自动系统更新。来源:https://

将Python应用部署到生产环境的小技巧分享

《将Python应用部署到生产环境的小技巧分享》文章主要讲述了在将Python应用程序部署到生产环境之前,需要进行的准备工作和最佳实践,包括心态调整、代码审查、测试覆盖率提升、配置文件优化、日志记录完... 目录部署前夜:从开发到生产的心理准备与检查清单环境搭建:打造稳固的应用运行平台自动化流水线:让部署像

最好用的WPF加载动画功能

《最好用的WPF加载动画功能》当开发应用程序时,提供良好的用户体验(UX)是至关重要的,加载动画作为一种有效的沟通工具,它不仅能告知用户系统正在工作,还能够通过视觉上的吸引力来增强整体用户体验,本文给... 目录前言需求分析高级用法综合案例总结最后前言当开发应用程序时,提供良好的用户体验(UX)是至关重要

基于Python实现PDF动画翻页效果的阅读器

《基于Python实现PDF动画翻页效果的阅读器》在这篇博客中,我们将深入分析一个基于wxPython实现的PDF阅读器程序,该程序支持加载PDF文件并显示页面内容,同时支持页面切换动画效果,文中有详... 目录全部代码代码结构初始化 UI 界面加载 PDF 文件显示 PDF 页面页面切换动画运行效果总结主

Java 枚举的常用技巧汇总

《Java枚举的常用技巧汇总》在Java中,枚举类型是一种特殊的数据类型,允许定义一组固定的常量,默认情况下,toString方法返回枚举常量的名称,本文提供了一个完整的代码示例,展示了如何在Jav... 目录一、枚举的基本概念1. 什么是枚举?2. 基本枚举示例3. 枚举的优势二、枚举的高级用法1. 枚举

不删数据还能合并磁盘? 让电脑C盘D盘合并并保留数据的技巧

《不删数据还能合并磁盘?让电脑C盘D盘合并并保留数据的技巧》在Windows操作系统中,合并C盘和D盘是一个相对复杂的任务,尤其是当你不希望删除其中的数据时,幸运的是,有几种方法可以实现这一目标且在... 在电脑生产时,制造商常为C盘分配较小的磁盘空间,以确保软件在运行过程中不会出现磁盘空间不足的问题。但在

Python中列表的高级索引技巧分享

《Python中列表的高级索引技巧分享》列表是Python中最常用的数据结构之一,它允许你存储多个元素,并且可以通过索引来访问这些元素,本文将带你深入了解Python列表的高级索引技巧,希望对... 目录1.基本索引2.切片3.负数索引切片4.步长5.多维列表6.列表解析7.切片赋值8.删除元素9.反转列表