首先,我们需要从 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``MotionLayout
的用处可远不止这些。我们先来看看 MotionLayout 的构成:
由上图可知,MotionLayout 可分为 <View>
和 <Helper>
两个部分。 <View>
部分可简单理解为一个ConstraintLayout
,至于 <Helper>
其实就是我们的“动画层”了。MotionLayout 为我们提供了layoutDescription
属性,我们需要为它传入一个 MotionScene
包裹的 XML
MotionScene:传说中的“百宝袋”
什么是 MotionScene?结合上图 MotionScene 主要由三部分组成: StateSet
、ConstraintSet
和 Transition
。为了让大家快速理解和使用 MotionScene,本文将重点讲解ConstarintSet
和 Transition
,至于 StateSet
首先,我们从实现下面这个简单的效果讲起:
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>
元素了。事实上,我们都知道,动画都是有开始位置和结束位置的,而 MotionLayout
Path
回到上面这个例子,我们只需要为 Transition
设置起始位置和结束位置的 ConstraintSet
并设置动画时间即可,剩下的都交给MotionLayout
自动去帮我们完成。当然你也可以通过 onClick
点击事件来触发动画,绑定目标控件的 id 以及通过clickAction
属性来设置点击事件的类型,这里我们设置的是toggle
,即通过反复点击控件来切换动画的状态,其他还有很多属性可以参照官方文档去研究,比较简单,这里就不一一讲解它们的效果了。如此一来,运行一下就能看到上面的效果了。另外,为了方便测试,我们可以给MotionLayout
加上调试属性: app:motionDebug="SHOW_PATH"
,然后就能轻易的查看其动画内部的运动轨迹:
什么?你说这个动画效果太基础?那好,我就来个简陋版的“百花齐放”效果吧,比如下面这样:
首先,让我们分析一下这个效果:仔细看我们可以发现,通过向上滑动蓝色的 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”
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
总结
笔者之前工作是在金融公司可能并不是特别追求技术,而笔者又是喜欢追求技术的人,所以格格不入,只能把目标放在互联网大厂了。也希望大家都去敢于尝试和追逐自己的梦想!
BATJ大厂Android高频面试题
觉得有收获的记得点赞,关注+收藏哦!你们的点赞就是我的动力!
、源码讲义、实战项目、讲解视频,并且后续会持续更新**
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-b8zR5MXE-1711691688087)]
总结
笔者之前工作是在金融公司可能并不是特别追求技术,而笔者又是喜欢追求技术的人,所以格格不入,只能把目标放在互联网大厂了。也希望大家都去敢于尝试和追逐自己的梦想!
BATJ大厂Android高频面试题
[外链图片转存中…(img-UhE3jVaY-1711691688087)]
[外链图片转存中…(img-sxh1FJRp-1711691688087)]
[外链图片转存中…(img-2EUwadnj-1711691688088)]
[外链图片转存中…(img-4RWUKp4l-1711691688088)]
觉得有收获的记得点赞,关注+收藏哦!你们的点赞就是我的动力!