Android:随笔—— ConstraintLayout 动画

简介: 谷歌之前推出了 ConstraintLayout 动画,做一些基本的动画让我们更省时省力了,今天我们就看一下这个小知识点。首先你需要引入 constraint-layout 这就不用我多说了然后还需要引入 transition 包,如果不引入,从而...

谷歌之前推出了 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>

预览下原始布局


img_9e471a2ab737e7ef13c4a7d77165538b.png
原始布局预览

然后贴上标定位置的布局

<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>
img_642c9b7369ad0385f634a95df3ef006f.png
image.png

大家发现了没有这个布局颜色,按钮文字什么属性我都没有写,因为不需要,这个布局只提供一个控件的位置与约束信息,做动画就是从默认布局到这个布局的一个渐变

我放的图片有颜色是我故意加的,为了能让大家直观的看到两个控件的位置,代码中其实是并不需要的

再看一下 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;
        }
    }
img_8cfbbda6db1e3e3e9c1bdf3ea5bdd8a6.gif
动画演示

首先动画效果看了,看了一眼代码是不是觉得这种写法代码冗余很严重有木有,这个动画不止有这一种写法,先不要放弃他,我们就来逼叨逼叨!

  1. 首先我们有两个布局,第一个是正常的布局,包括文字颜色什么的都有,第二个布局只有与约束相关的代码和相关控件以及 ID。我们第二个布局就算你写了其他的属性系统再做动画的时候他也不会读,所以就是没什么卵用。

看到这里大家心里肯定有一个疑问,我们平常设置 View 的字体、文字、颜色、背景什么的会不会出现什么问题,我这里试过了,什么问题都没有,包括 View 的 ID 只要我们默认布局有就不会有问题。

  1. 再说一点本来应该和第一点写一起的但是我觉得这一点很重要:我们第一个布局里面各个控件以及 ID 在第二个布局一定要保持不变,切记 ID 不要变,因为变了系统去做动画的时候就达不到预期效果了,举个例子:
    本来我们有两个控件 ID 分别为 A,B ,但是我们在第二个布局改成了 B,C 那么做动画 A 就会因为没有约束而乱做动画,只有 B 做动画效果

  2. 想做动画必须用 ConstraintLayout ,并且子布局里面的控件是不会做动画的

用法简述

  1. 有几种动画状态就需要创建几个 ConstraintSet 因为约束布局的动画都依赖于 ConstraintSet
  2. 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)
  1. 我们上面说的如果不想再画一个布局那就是用代码自己编辑约束关系,例如:
mConstraintSet2.clone(constraintLayout);
mConstraintSet2.centerVertically(R.id.a, 0)

ConstraintSet 里面的 API 可以自己看一下,基本就是 ConstraintLayout 里面的那些属性,如果懒得话,乖乖再画一遍布局吧

  1. TransitionManager.beginDelayedTransition(constraintLayout) 准备开始动画
    我们还可以自定义动画时长等等操作,具体 AutoTransition API ,例子:
AutoTransition transition = new AutoTransition();
transition.setDuration(500);
TransitionManager.beginDelayedTransition(constraintLayout,transition);
  1. mConstraintSet1.applyTo(constraintLayout); 然后把 ConstraintSet 位置和约束信息应用到 ConstraintLayout 布局,并且做动画

如果手动修改了 ConstraintLayout 中元素的布局位置,那么动画不会同步这个变化,因为他们的位置等信息都存在 ConstraintSet 中,你就算改了 ConstraintLayout 中元素位置也没什么用,除非你自己改了,记得要重新记录到 ConstraintSet 中。

小结

虽然这个知识点很小,但是用好了能给我们的 APP 添加很多逼格,学习这个知识点的收益真的是很划算

目录
相关文章
|
6月前
|
Android开发 开发者
Android利用SVG实现动画效果
本文介绍了如何在Android中利用SVG实现动画效果。首先通过定义`pathData`参数(如M、L、Z等)绘制一个简单的三角形SVG图形,然后借助`objectAnimator`实现动态的线条绘制动画。文章详细讲解了从配置`build.gradle`支持VectorDrawable,到创建动画文件、关联SVG与动画,最后在Activity中启动动画的完整流程。此外,还提供了SVG绘制原理及工具推荐,帮助开发者更好地理解和应用SVG动画技术。
314 30
|
6月前
|
Android开发 UED 计算机视觉
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
本文介绍了一款受游戏“金铲铲之战”启发的Android自定义View——线条等待动画的实现过程。通过将布局分为10份,利用`onSizeChanged`测量最小长度,并借助画笔绘制动态线条,实现渐变伸缩效果。动画逻辑通过四个变量控制线条的增长与回退,最终形成流畅的等待动画。代码中详细展示了画笔初始化、线条绘制及动画更新的核心步骤,并提供完整源码供参考。此动画适用于加载场景,提升用户体验。
522 5
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
|
6月前
|
API Android开发 开发者
Android颜色渐变动画效果的实现
本文介绍了在Android中实现颜色渐变动画效果的方法,重点讲解了插值器(TypeEvaluator)的使用与自定义。通过Android自带的颜色插值器ArgbEvaluator,可以轻松实现背景色的渐变动画。文章详细分析了ArgbEvaluator的核心代码,并演示了如何利用Color.colorToHSV和Color.HSVToColor方法自定义颜色插值器MyColorEvaluator。最后提供了完整的源码示例,包括ColorGradient视图类和MyColorEvaluator类,帮助开发者更好地理解和应用颜色渐变动画技术。
225 3
|
6月前
|
Android开发 开发者
Android SVG动画详细例子
本文详细讲解了在Android中利用SVG实现动画效果的方法,通过具体例子帮助开发者更好地理解和应用SVG动画。文章首先展示了动画的实现效果,接着回顾了之前的文章链接及常见问题(如属性名大小写错误)。核心内容包括:1) 使用阿里图库获取SVG图形;2) 借助工具将SVG转换为VectorDrawable;3) 为每个路径添加动画绑定属性;4) 创建动画文件并关联SVG;5) 在ImageView中引用动画文件;6) 在Activity中启动动画。文末还提供了完整的代码示例和源码下载链接,方便读者实践操作。
341 65
|
6月前
|
XML Java Maven
Android线条等待动画JMWorkProgress(可添加依赖直接使用)
这是一篇关于Android线条等待动画JMWorkProgress的教程文章,作者计蒙将其代码开源至GitHub,提升可读性。文章介绍了如何通过添加依赖库使用该动画,并详细讲解了XML与Java中的配置方法,包括改变线条颜色、宽度、添加文字等自定义属性。项目已支持直接依赖集成(`implementation &#39;com.github.Yufseven:JMWorkProgress:v1.0&#39;`),开发者可以快速上手实现炫酷的等待动画效果。文末附有GitHub项目地址,欢迎访问并点赞支持!
206 26
|
6月前
|
XML Android开发 数据格式
Android中SlidingDrawer利用透明动画提示效果
本文介绍了在Android中使用`SlidingDrawer`实现带有透明动画提示效果的方法。通过XML布局配置`SlidingDrawer`的把手(handle)和内容(content),结合Activity中的代码实现动态动画效果。最终实现了交互性强、视觉效果良好的滑动抽屉功能。
Android中SlidingDrawer利用透明动画提示效果
|
6月前
|
XML Java Android开发
Android 动画之帧动画 + 补间动画 + 属性动画
本文介绍了Android开发中的三种动画类型:帧动画、补间动画和属性动画。帧动画通过依次播放一系列静态图片实现动态效果,支持Java代码与XML两种实现方式。补间动画基于起始和结束位置自动生成过渡效果,涵盖透明度、位移、旋转、缩放及组合动画等多种形式,并可搭配插值器优化动画过程。属性动画则通过改变对象属性实现动画,支持透明度、位移、旋转、缩放及组合动画,灵活性更高且适用于更复杂的场景。文中提供了详细的代码示例,帮助开发者快速上手。
371 15
|
6月前
|
Android开发 开发者
Android自定义view之围棋动画(化繁为简)
本文介绍了Android自定义View的动画实现,通过两个案例拓展动态效果。第一个案例基于`drawArc`方法实现单次动画,借助布尔值控制动画流程。第二个案例以围棋动画为例,从简单的小球直线运动到双向变速运动,最终实现循环动画效果。代码结构清晰,逻辑简明,展示了如何化繁为简实现复杂动画,帮助读者拓展动态效果设计思路。文末提供完整源码,适合初学者和进阶开发者学习参考。
129 0
Android自定义view之围棋动画(化繁为简)
|
存储 Shell Android开发
基于Android P,自定义Android开机动画的方法
本文详细介绍了基于Android P系统自定义开机动画的步骤,包括动画文件结构、脚本编写、ZIP打包方法以及如何将自定义动画集成到AOSP源码中。
561 2
基于Android P,自定义Android开机动画的方法