为ViewGroup添加入场动画,LayoutAnimation使用概述

2024-03-27 23:50

本文主要是介绍为ViewGroup添加入场动画,LayoutAnimation使用概述,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本篇文章来一点好玩的效果。还记得之前的视图动画效果吗?之前我们控制的效果,都是针对单个视图,如果想要对一组视图使用相同的动画效果,这个时候,就需要使用到LayoutAnimationController了。

LayoutAnimationController介绍:

Android Developer LayoutAnimationController docment

  • LayoutAnimationController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果(即整个布局)
  • 每一个控件都有相同的动画效果
  • 这些控件的动画效果可在不同的时间显示出来

LayoutAnimationController的使用非常简单

xml直接使用

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"android:delay="0.1"  //表示动画播放的延时,既可以是百分比,也可以是float小数。android:animationOrder="reverse" //表示动画的播放顺序,//有三个取值normal(顺序)、reverse(反序)、random(随机)。android:animation="@anim/animation"  //指向了子控件所要播放的动画/>

然后将定义好的动画配置到ViewGroup的布局中即可。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/llViewGroupContainer"android:layout_width="match_parent"android:layout_height="match_parent"android:layoutAnimation="@anim/list_anim_layout"android:background="@android:color/white"android:orientation="vertical"><TextView...
</LinearLayout>    

代码中配置使用

val layoutAnimationController =LayoutAnimationController(LayoutAnimationHelper.getAnimationSetFromRight())layoutAnimationController.delay= 0.1FlayoutAnimationController.order = LayoutAnimationController.ORDER_NORMALllViewGroupContainer.layoutAnimation = layoutAnimationControllerllViewGroupContainer.scheduleLayoutAnimation()
/*** 从右侧进入,并带有弹性的动画** @return*/
public static AnimationSet getAnimationSetFromRight() {AnimationSet animationSet = new AnimationSet(true);TranslateAnimation translateX1 = new TranslateAnimation(RELATIVE_TO_SELF, 1.0f, RELATIVE_TO_SELF, -0.1f,RELATIVE_TO_SELF, 0, RELATIVE_TO_SELF, 0);translateX1.setDuration(300);translateX1.setInterpolator(new DecelerateInterpolator());translateX1.setStartOffset(0);TranslateAnimation translateX2 = new TranslateAnimation(RELATIVE_TO_SELF, -0.1f, RELATIVE_TO_SELF, 0.1f,RELATIVE_TO_SELF, 0, RELATIVE_TO_SELF, 0);translateX2.setStartOffset(300);translateX2.setInterpolator(new DecelerateInterpolator());translateX2.setDuration(50);TranslateAnimation translateX3 = new TranslateAnimation(RELATIVE_TO_SELF, 0.1f, RELATIVE_TO_SELF, 0f,RELATIVE_TO_SELF, 0, RELATIVE_TO_SELF, 0);translateX3.setStartOffset(350);translateX3.setInterpolator(new DecelerateInterpolator());translateX3.setDuration(50);AlphaAnimation alphaAnimation = new AlphaAnimation(0.5f, 1.0f);alphaAnimation.setDuration(400);alphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator());animationSet.addAnimation(translateX1);animationSet.addAnimation(translateX2);animationSet.addAnimation(translateX3);animationSet.addAnimation(alphaAnimation);animationSet.setDuration(400);return animationSet;
}

下面是效果

为了让大家看的时候更清晰效果,所以视频做了慢放处理,可以看到是有回弹会效果的,还是比较炫酷的。如果是recyclerView,也可以使用LayoutAnimationController来实现类似效果。

//为RecyclerView添加动画
val layoutAnimationController =LayoutAnimationController(LayoutAnimationHelper.getAnimationSetFromRight())
layoutAnimationController.delay = 0.1F
layoutAnimationController.order = LayoutAnimationController.ORDER_NORMALrvContentList.layoutAnimation = layoutAnimationController

可以看到动画的执行顺序是顺序执行的,ViewGroupRecyclerView中,都是顺序执行的,但是如果现在是GridView或者RecylcerView#GridLayoutManager方式,我们可能希望沿着对角线方向实现动画效果,而不是一个个来。则需要进行自定义动画的执行方向。LayoutAnimationController动画执行方向修改也很简单,只需要重写ViewGroup#protected void attachLayoutAnimationParameters(View child, LayoutParams params, int index, int count)方法即可。

下面是对于RecycleView#GridLayoutManager实现对角线方向动画效果的代码

public class GridRecyclerView extends RecyclerView {/** @see View#View(Context) */public GridRecyclerView(Context context) { super(context); }/** @see View#View(Context, AttributeSet) */public GridRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); }/** @see View#View(Context, AttributeSet, int) */public GridRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }@Overrideprotected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params,int index, int count) {final LayoutManager layoutManager = getLayoutManager();if (getAdapter() != null && layoutManager instanceof GridLayoutManager){GridLayoutAnimationController.AnimationParameters animationParams =(GridLayoutAnimationController.AnimationParameters) params.layoutAnimationParameters;if (animationParams == null) {// If there are no animation parameters, create new once and attach them to// the LayoutParams.animationParams = new GridLayoutAnimationController.AnimationParameters();params.layoutAnimationParameters = animationParams;}// Next we are updating the parameters// Set the number of items in the RecyclerView and the index of this itemanimationParams.count = count;animationParams.index = index;// Calculate the number of columns and rows in the gridfinal int columns = ((GridLayoutManager) layoutManager).getSpanCount();animationParams.columnsCount = columns;animationParams.rowsCount = count / columns;// Calculate the column/row position in the gridfinal int invertedIndex = count - 1 - index;animationParams.column = columns - 1 - (invertedIndex % columns);animationParams.row = animationParams.rowsCount - 1 - invertedIndex / columns;} else {// Proceed as normal if using another type of LayoutManagersuper.attachLayoutAnimationParameters(child, params, index, count);}}
}

使用上和之前使用没有任何区别。下面来看下效果。动画执行顺序还是区别很明显的。

这篇关于为ViewGroup添加入场动画,LayoutAnimation使用概述的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

使用Python实现图像LBP特征提取的操作方法

《使用Python实现图像LBP特征提取的操作方法》LBP特征叫做局部二值模式,常用于纹理特征提取,并在纹理分类中具有较强的区分能力,本文给大家介绍了如何使用Python实现图像LBP特征提取的操作方... 目录一、LBP特征介绍二、LBP特征描述三、一些改进版本的LBP1.圆形LBP算子2.旋转不变的LB

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H

Python中__init__方法使用的深度解析

《Python中__init__方法使用的深度解析》在Python的面向对象编程(OOP)体系中,__init__方法如同建造房屋时的奠基仪式——它定义了对象诞生时的初始状态,下面我们就来深入了解下_... 目录一、__init__的基因图谱二、初始化过程的魔法时刻继承链中的初始化顺序self参数的奥秘默认

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

《SpringBoot集成Quartz并使用Cron表达式实现定时任务》本篇文章介绍了如何在SpringBoot中集成Quartz进行定时任务调度,并通过Cron表达式控制任务... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启动 Sprin

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Java使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J

使用Python实现一个优雅的异步定时器

《使用Python实现一个优雅的异步定时器》在Python中实现定时器功能是一个常见需求,尤其是在需要周期性执行任务的场景下,本文给大家介绍了基于asyncio和threading模块,可扩展的异步定... 目录需求背景代码1. 单例事件循环的实现2. 事件循环的运行与关闭3. 定时器核心逻辑4. 启动与停

如何使用Nginx配置将80端口重定向到443端口

《如何使用Nginx配置将80端口重定向到443端口》这篇文章主要为大家详细介绍了如何将Nginx配置为将HTTP(80端口)请求重定向到HTTPS(443端口),文中的示例代码讲解详细,有需要的小伙... 目录1. 创建或编辑Nginx配置文件2. 配置HTTP重定向到HTTPS3. 配置HTTPS服务器

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm