秒懂Android开发之MotionLayout简单上手

2023-12-12 20:48

本文主要是介绍秒懂Android开发之MotionLayout简单上手,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【版权申明】非商业目的注明出处可自由转载
博文地址:https://blog.csdn.net/ShuSheng0007/article/details/106182408
出自:shusheng007

文章目录

  • 概述
  • 动画布局基本元素
    • MotionLayout
    • MotionScene
    • ConstraintSet
    • Transition
  • MotionLayout基础使用
  • 扩展
  • 总结

概述

前段时间升级了Android Studio到4.0, 发现新增了一个动画编辑器的功能,又一次感叹Android开发真的是越来越简单了。看了一下感觉挺有意思,所以就简单上手一下这个动画布局MotionLayout,通过这个布局我们可以以非常简单的方式作出很炫的动画。

动画布局基本元素

先上一张动图吧,图中的动画使用MotionLayout不到两分钟就可以完成,在此感谢老婆贡献自己的美照。

在这里插入图片描述
我们只要搞清楚几个关键元素,上手就易如反掌了,当然高级特性还需要熟练后深入探索,这个成年人都明白。

我们可以用一句话来描述一个View的动画:此View从开始状态到达结束状态的一个过程

MotionLayout

MotionLayout是整个动画的容器,正是它提供了让其内部的直接嵌套View 做动画的能力。它是ConstraintLayout 的子类, 可以从AS中通过ConstraintLayout转换而来。

MotionScene

动画场景,顾名思义,它的作用就是描述整个动画。是一个xml文件,放在xml 文件夹内,作为MotionLayoutlayoutDescription属性的值,本例中我写了个一个名为activity_motion_scene.xml的MotionScence文件并设置给MothionLayout,如下所示

<androidx.constraintlayout.motion.widget.MotionLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"...app:layoutDescription="@xml/activity_motion_scene">...
</androidx.constraintlayout.motion.widget.MotionLayout>

ConstraintSet

动画开始时或者结束时的快照,其用来描述一个View开始和结束时候的状态。例如一个View动画开始前在哪里,多大尺寸,什么颜色等等。 其嵌套在MotionScene 里面,一般有两个,一个开始一个结束。

<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><!--    动画开始快照--><ConstraintSet android:id="@+id/start"><Constraintandroid:id="@+id/my_wife"android:layout_width="60dp"android:layout_height="60dp"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintBottom_toBottomOf="parent"/></ConstraintSet><!--    动画结束快照--><ConstraintSet android:id="@+id/end"><Constraintandroid:id="@+id/my_wife"android:layout_width="150dp"android:layout_height="150dp"android:layout_marginStart="5dp"android:layout_marginEnd="5dp"app:layout_constraintStart_toEndOf="@id/my_wife_left"app:layout_constraintEnd_toStartOf="@id/my_wife_right"app:layout_constraintTop_toTopOf="parent"/></ConstraintSet>...
</MotionScene>

Transition

描述View从开始状态变换到结束状态的过程,且响应用户的操作,其嵌套在MotionScene 里,与ConstraintSet平级,是整个动画布局的精华部分。

Transition 的constraintSetEnd 属性设置开始ConstraintSet ,constraintSetStart属性设置结束那个ConstraintSet ,再设置一个动画时间即可。

其可以包含一个<KeyFrameSet> 负责在动画过程中改变view的位置和属性。如果不设置此标签,那么我们的动画就会线性的从开始运动到结束。

    <Transitionapp:constraintSetEnd="@+id/end"app:constraintSetStart="@id/start"app:duration="2000"><KeyFrameSet><KeyPositionapp:motionTarget="@id/my_wife_left"app:framePosition="40"app:keyPositionType="parentRelative"app:percentX="0"/>...<KeyAttributeapp:motionTarget="@id/my_wife"app:framePosition="40"android:rotationY="360"/></KeyFrameSet><OnSwipeapp:touchAnchorId="@+id/my_wife"app:touchAnchorSide="top"app:dragDirection="dragUp" /><OnClick app:targetId="@+id/my_wife" app:clickAction="toggle" /></Transition>

MotionLayout基础使用

明白了以上几个元素使用就非常简单了,让我们完成文章最开始的动画

  • 配置环境
    需要升级constraintlayout控件库到2.0以上,现在是beta6了,已经过了alpha,说明接口稳定了。
implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta6"
  • 具体使用
  1. 在layout文件中用MotionLayout 包裹需要做动画的View。
       <androidx.constraintlayout.motion.widget.MotionLayoutxmlns: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/activity_motion_scene"app:motionDebug="SHOW_PATH"tools:context=".animation.MotionActivity"><ImageViewandroid:id="@+id/my_wife_left"android:layout_width="60dp"android:layout_height="60dp"android:src="@drawable/my_wife"/><ImageViewandroid:id="@+id/my_wife"android:layout_width="60dp"android:layout_height="60dp"android:src="@drawable/my_wife"/><ImageViewandroid:id="@+id/my_wife_right"android:layout_width="60dp"android:layout_height="60dp"android:src="@drawable/my_wife" /></androidx.constraintlayout.motion.widget.MotionLayout>
    
  2. 在res/xml 文件夹中新建一个名为:activity_motion_scene的MotionScene文件,并设置到MotionLayout的layoutDescription属性中
  3. 在MotionScene中定义两个ConstraintSet,一个为View的开始状态,一个为View结束状态。
  4. 在MotionScene中定义一个Transition,并设置开始与结束的ConstraintSet 。
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><Transitionapp:constraintSetEnd="@+id/end"app:constraintSetStart="@id/start"app:duration="2000"><KeyFrameSet><KeyPositionapp:motionTarget="@id/my_wife_left"app:framePosition="40"app:keyPositionType="parentRelative"app:percentX="0"/><KeyPositionapp:motionTarget="@id/my_wife_right"app:framePosition="40"app:keyPositionType="parentRelative"app:percentX="1"/></KeyFrameSet><!-- 使动画响应滑动事件--><OnSwipeapp:touchAnchorId="@+id/my_wife"app:touchAnchorSide="top"app:dragDirection="dragUp" /><!-- 使动画响应点击事件--><OnClick app:targetId="@+id/my_wife" app:clickAction="toggle" /></Transition><!--    动画开始快照--><ConstraintSet android:id="@+id/start"><Constraintandroid:id="@+id/my_wife"android:layout_width="60dp"android:layout_height="60dp"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintBottom_toBottomOf="parent"/><Constraintandroid:id="@+id/my_wife_left"android:layout_width="60dp"android:layout_height="60dp"android:alpha="0.0"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintBottom_toBottomOf="parent"/><Constraintandroid:id="@+id/my_wife_right"android:layout_width="60dp"android:layout_height="60dp"android:alpha="0.0"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintBottom_toBottomOf="parent"/></ConstraintSet><!--    动画结束快照--><ConstraintSet android:id="@+id/end"><Constraintandroid:id="@+id/my_wife_left"android:layout_width="100dp"android:layout_height="100dp"android:alpha="1.0"app:layout_constraintHorizontal_chainStyle="packed"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toStartOf="@id/my_wife"app:layout_constraintTop_toTopOf="@id/my_wife"app:layout_constraintBottom_toBottomOf="@id/my_wife"/><Constraintandroid:id="@+id/my_wife"android:layout_width="150dp"android:layout_height="150dp"android:layout_marginStart="5dp"android:layout_marginEnd="5dp"app:layout_constraintStart_toEndOf="@id/my_wife_left"app:layout_constraintEnd_toStartOf="@id/my_wife_right"app:layout_constraintTop_toTopOf="parent"/><Constraintandroid:id="@+id/my_wife_right"android:layout_width="100dp"android:layout_height="100dp"android:alpha="1.0"app:layout_constraintStart_toEndOf="@id/my_wife"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintTop_toTopOf="@id/my_wife"app:layout_constraintBottom_toBottomOf="@id/my_wife"/></ConstraintSet>
</MotionScene>

经过上面简单的几步动画就已经完成啦,是不是2分钟内就搞定了。

其中, <Transition>里的<OnClick>标签响应用户的点击动作,例如此例中我们点击一下中间的view,动画就开始了。

<OnClick app:targetId="@+id/my_wife" app:clickAction="toggle" />

扩展

上面只是可以让你快速上手,里面还有很多高级特性需要你继续探索,例如在Transition的 KeyFrameSet 里面还包括
<KeyCycle><KeyAttribute><KeyTimeCycle><KeyTrigger> 等对动画精细控制的标签

下面是一些很好的资源,可以帮助你更加详细的理解这个库

本文源码:AndroidDevMemo

官方示例:ConstraintLayoutExamples
Medium博客: Introduction to MotionLayout

总结

IT 技术更新换代实在是太快了,而且对于应用层会越变越简单,所以奉劝各位可爱的程序员们在习得日常求生技能后,最好还是多关注一下计算机科学基础。例如计算机系统、数据结构、算法、数据库、网络、编译原理、设计模式等

总的来说,照目前的情况来看,未来的社会会更加数字化、智能化,所以程序员会大有可为,加油。

这篇关于秒懂Android开发之MotionLayout简单上手的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(