Android:随笔—— ConstraintLayout 动画
谷歌之前推出了 ConstraintLayout 动画,做一些基本的动画让我们更省时省力了,今天我们就看一下这个小知识点。
首先你需要引入 constraint-layout 这就不用我多说了
然后还需要引入 transition 包,如果不引入,从而使用系统自带的 transition 的话,则你的约束布局动画只能兼容到 API 19
implementation 'com.android.support.constraint:constraint-layout:1.1.2' implementation 'com.android.support:transition:27.1.1'
我们先说一说最简单的实现方式,使用两个布局,第一个是我们正常的布局,另一个是记录控件变换后位置与约束信息的布局
首先贴上正常的布局
<android.support.constraint.ConstraintLayout 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" android:id="@+id/root_layout" tools:context=".MainActivity"> <View android:id="@+id/a" android:layout_width="0dp" android:layout_height="60dp" android:background="@android:color/holo_red_light" app:layout_constraintHorizontal_weight="1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/b" /> <View android:id="@+id/b" android:layout_width="0dp" android:layout_height="60dp" android:background="@android:color/holo_blue_light" app:layout_constraintHorizontal_weight="2" app:layout_constraintLeft_toRightOf="@+id/a" app:layout_constraintRight_toRightOf="parent" /> <Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始" android:onClick="onClick" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" /> <Button android:id="@+id/btn_reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="恢复" android:onClick="onClick" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" /> </android.support.constraint.ConstraintLayout>
预览下原始布局
然后贴上标定位置的布局
<android.support.constraint.ConstraintLayout 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" tools:context=".MainActivity"> <View android:id="@+id/a" android:layout_width="0dp" android:layout_height="60dp" android:layout_margin="20dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/b" android:layout_width="0dp" android:layout_height="60dp" android:layout_margin="20dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/a" /> <Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" /> <Button android:id="@+id/btn_reset" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" /> </android.support.constraint.ConstraintLayout>
大家发现了没有这个布局颜色,按钮文字什么属性我都没有写,因为不需要,这个布局只提供一个控件的位置与约束信息,做动画就是从默认布局到这个布局的一个渐变
我放的图片有颜色是我故意加的,为了能让大家直观的看到两个控件的位置,代码中其实是并不需要的
再看一下 Activity 的写法
private ConstraintSet mConstraintSet1, mConstraintSet2, mConstraintReset; private ConstraintLayout constraintLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mConstraintSet1 = new ConstraintSet(); mConstraintSet2 = new ConstraintSet(); constraintLayout = findViewById(R.id.root_layout); //把默认 constraintLayout 布局放到 mConstraintSet1 中 mConstraintSet1.clone(constraintLayout); //把标定位置变换的 constraintLayout 布局放到 mConstraintSet2 中 mConstraintSet2.clone(this, R.layout.activity_main_anim); } public void onClick(View view) { switch (view.getId()) { case R.id.btn_start: TransitionManager.beginDelayedTransition(constraintLayout); mConstraintSet2.applyTo(constraintLayout); break; case R.id.btn_reset: AutoTransition transition = new AutoTransition(); transition.setDuration(500); TransitionManager.beginDelayedTransition(constraintLayout,transition); mConstraintSet1.applyTo(constraintLayout); break; } }
首先动画效果看了,看了一眼代码是不是觉得这种写法代码冗余很严重有木有,这个动画不止有这一种写法,先不要放弃他,我们就来逼叨逼叨!
- 首先我们有两个布局,第一个是正常的布局,包括文字颜色什么的都有,第二个布局只有与约束相关的代码和相关控件以及 ID。我们第二个布局就算你写了其他的属性系统再做动画的时候他也不会读,所以就是没什么卵用。
看到这里大家心里肯定有一个疑问,我们平常设置 View 的字体、文字、颜色、背景什么的会不会出现什么问题,我这里试过了,什么问题都没有,包括 View 的 ID 只要我们默认布局有就不会有问题。
再说一点本来应该和第一点写一起的但是我觉得这一点很重要:我们第一个布局里面各个控件以及 ID 在第二个布局一定要保持不变,切记 ID 不要变,因为变了系统去做动画的时候就达不到预期效果了,举个例子:
本来我们有两个控件 ID 分别为 A,B ,但是我们在第二个布局改成了 B,C 那么做动画 A 就会因为没有约束而乱做动画,只有 B 做动画效果想做动画必须用 ConstraintLayout ,并且子布局里面的控件是不会做动画的
用法简述
- 有几种动画状态就需要创建几个 ConstraintSet 因为约束布局的动画都依赖于 ConstraintSet
-
ConstraintSet.clone();
使用这个方法把布局位置以及约束关系记录到 ConstraintSet 中。该方法有以下几种构造方法自己按需求选择
public void clone(Context context, int constraintLayoutId)
public void clone(ConstraintSet set)
public void clone(ConstraintLayout constraintLayout)
public void clone(Constraints constraints)
- 我们上面说的如果不想再画一个布局那就是用代码自己编辑约束关系,例如:
mConstraintSet2.clone(constraintLayout); mConstraintSet2.centerVertically(R.id.a, 0)
ConstraintSet 里面的 API 可以自己看一下,基本就是 ConstraintLayout 里面的那些属性,如果懒得话,乖乖再画一遍布局吧
-
TransitionManager.beginDelayedTransition(constraintLayout)
准备开始动画
我们还可以自定义动画时长等等操作,具体 AutoTransition API ,例子:
AutoTransition transition = new AutoTransition(); transition.setDuration(500); TransitionManager.beginDelayedTransition(constraintLayout,transition);
-
mConstraintSet1.applyTo(constraintLayout);
然后把 ConstraintSet 位置和约束信息应用到 ConstraintLayout 布局,并且做动画
如果手动修改了 ConstraintLayout 中元素的布局位置,那么动画不会同步这个变化,因为他们的位置等信息都存在 ConstraintSet 中,你就算改了 ConstraintLayout 中元素位置也没什么用,除非你自己改了,记得要重新记录到 ConstraintSet 中。
小结
虽然这个知识点很小,但是用好了能给我们的 APP 添加很多逼格,学习这个知识点的收益真的是很划算

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
微信‘类’之测试方法汇总
前言 小程序优势之一,是可以同时在不同设备上运行,但这会带来潜在兼容性问题。这个问题对于个人开发者而言很麻烦,因为他们通常只会用自己的手机来测试小程序。当然,微信早就意识到这个问题,并在早先的新能力更新中,为小程序开发者提供了免费的真机测试工具。 微信提供的这个工具,不需要自己准备测试手机。这个工具已经免费为你提供了大量 Android 机型供你测试,你需要做的,就是点点鼠标、申请测试。 在最新版、测试版「微信 Web 开发者工具」中,都内置了这个工具。在开发者工具中,点击右上角的「测试」按钮,就可以查看以往申请过的测试报告。 一、真机工具下载地址 1、beta 版本微信 Web 开发者工具 2、最新版微信 Web 开发者工具正式版 二、Airtest AirtestIDE是一个跨平台的UI自动化测试编辑器,适用于游戏和App。 1、自动化脚本录制、一键回放、报告查看,轻而易举实现自动化测试流程 2、支持基于图像识别的Airtest框架,适用于所有Android和Windows游戏 3、支持基于UI控件搜索的Poco框架,适用于Unity3d,Cocos2d与Android App 4...
- 下一篇
阅读手札:《Android开发艺术探索》(一)
《Android开发艺术探索》这本书在Android开发界内可谓口碑极佳,他的作者任玉刚在百度担任Android资深开发工程师。我的一个初中朋友现在深圳百度做Android研发,上周去深圳找他玩的时候碰巧在他电脑桌上看到了这本书,他谈到这本书内容多、有深度且知识点非常系统值得反复阅读。的确,《Android开发艺术探索》这本书去年看过一遍就感觉非常不错,下面将一些书中的重要知识点归纳并分享出来,这是本系列的第一篇,主要介绍的是 Activity的生命周期和启动模式 评语:本节也从原理解释了为什么每次继承Activity后,需要重写onCreate(Bundle savedInstanceState),方法里面的savedInstanceState虽然用的少,但是很重要!谷歌Android团队的这种设计策略可以从侧面反映其思虑之远、逻辑健壮性以及降低特殊情况的损耗。正文有详细解释 下面是我们熟悉的Activity的生命周期流程图: Activity的生命周期 下面就生命周期逐个分析: (1)onCreate:create 表示Activity正在被创建,这是Activity生命周期的第一...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Docker安装Oracle12C,快速搭建Oracle学习环境
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS7设置SWAP分区,小内存服务器的救世主
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- Linux系统CentOS6、CentOS7手动修改IP地址