Android View动画之LayoutAnimation的使用

news2024/9/22 21:28:39

接前篇 Android View动画整理 ,本篇介绍 LayoutAnimation 的使用。

参考《安卓开发艺术探索》。

View 动画作用于 View 。
LayoutAnimation 则作用于 ViewGroup , 为 ViewGoup 指定一个动画,ViewGoup 的子 View 出场时就具体动画效果。
简言之,LayoutAnimation 是为 ViewGroup 的子View指定出场动画。

开始使用,两种方式,xml 方式 和 java 方式 。

xml 方式

创建 R/anim/layout_anim_item.xml ,这个是子View的出场动画,实际的动画效果,本例为平移加透明度动画。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:shareInterpolator="true"
    android:interpolator="@android:anim/accelerate_interpolator">

    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"/>

    <translate android:fromXDelta="800" android:toXDelta="0"/>
</set>

定义 LayoutAnimation ,创建 R/anim/layout_anim.xml ,

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:animationOrder="normal"
    android:delay="0.5"
    android:animation="@anim/layout_anim_item">
</layoutAnimation>
  • android:animationOrder :子 View 动画的顺序,可选 normal 、reverse 、random 。normal 是排在前面的子View先开始动画;reverse 是排在后面的子View先开始动画;random 是子View随机播放动画。

  • android:delay :子View 开始动画的时间延迟。源码里的说明 child animation delay = child index * delay * animation duration ,即 1000 毫秒的动画,第1个子View 延迟 500 毫秒(1 * 0.5 * 1000)播放入场动画,第2个子View 延迟 1000 毫秒(2 * 0.5 * 1000)播放入场动画。

  • android:animation :指定子View的出场动画,实际的动画效果。

为 ViewGoup 的指定 android:layoutAnimation 属性,如

<LinearLayout
        <!-- -->
        android:layoutAnimation="@anim/layout_anim"
        <!-- --> >

贴下本例的 xml ,是一个 LinearLayout 包含多个其他控件,

<LinearLayout
        android:id="@+id/ll_layout_anim"
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:orientation="vertical"
        android:layoutAnimation="@anim/layout_anim"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.129">

        <TextView
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView1"
            android:textColor="@color/my_red"/>

        <TextView
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView2"
            android:textColor="@color/purple_500"/>

        <TextView
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView3"
            android:textColor="@color/black"/>

        <TextView
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView4"
            android:textColor="@android:color/holo_green_light"/>

        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/ic_red_cycle"/>

        <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <ImageView
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:background="@drawable/pic_beauty"/>


    </LinearLayout>

至此,OK。运行效果,
在这里插入图片描述

java 方式

通过 LayoutAnimationController 实现。

用前面提到的 R/anim/layout_anim_item.xml 动画文件,

创建动画 animation ,创建 LayoutAnimationController ,ViewGroup.setLayoutAnimation(LayoutAnimationController controller) ,

	public void onLAButtonClick(View view) {
        if (view.getId() == R.id.button_la_hide) {
            mLinearLayout.setVisibility(View.INVISIBLE);

        } else if (view.getId() == R.id.button_la_show) {
            mLinearLayout.setVisibility(View.VISIBLE);

            Animation animation = AnimationUtils.loadAnimation(this, R.anim.layout_anim_item);
            
            LayoutAnimationController controller = new LayoutAnimationController(animation);
            controller.setDelay(0.5f);
            //controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
            controller.setOrder(LayoutAnimationController.ORDER_REVERSE);
            //controller.setOrder(LayoutAnimationController.ORDER_RANDOM);
            
            mLinearLayout.setLayoutAnimation(controller);
        }

    }

很简单,完成,运行效果:
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/931588.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

YOLO目标检测——真实和人工智能生成的合成图像数据集下载分享

YOLO真实和人工智能生成的合成图像数据集&#xff0c;真实场景的高质量图片数据&#xff0c;图片格式为jpg&#xff0c;数据场景丰富。可用于检测图像是真实的还是由人工智能生成。 数据集点击下载&#xff1a;YOLO真实和人工智能生成的合成图像数据集120000图片数据说明.rar

【MYSQL8.0从入门到精通】

文章目录 MySQL 8.0一.MySQL的多表操作1.外键约束&#xff08;一对多&#xff09;2.外键约束&#xff08;多对多&#xff09; MySQL 8.0 一.MySQL的多表操作 1.外键约束&#xff08;一对多&#xff09; 方式1 在创建表的同时创建外键约束 -- 1.创建主表 create table if no…

聚类分析 | MATLAB实现基于LP拉普拉斯映射的聚类可视化

聚类分析 | MATLAB实现基于LP拉普拉斯映射的聚类可视化 目录 聚类分析 | MATLAB实现基于LP拉普拉斯映射的聚类可视化效果一览基本介绍程序设计参考资料 效果一览 基本介绍 聚类分析 | MATLAB实现基于LP拉普拉斯映射的聚类可视化&#xff0c;聚类结果可视化&#xff0c;MATLAB程…

【GAMES 101】图形学入门——向量与变换(Vector Transform)

一、向量&#xff08;Vector&#xff09; 点乘&#xff08;Dot Product&#xff09; 定义&#xff1a;计算两个单位向量的夹角用途&#xff1a; 描述两个向量是否足够接近 叉乘&#xff08;Cross Product&#xff09; 定义&#xff1a;用于判断法向量的防线&#xff0c;一…

C语言:指针和数组(看完拿捏指针和数组)

目录 数组名的理解&#xff1a; 一维数组&#xff1a; 解析&#xff1a; 字符数组&#xff1a; 解析&#xff1a; 解析&#xff1a; 字符串数组&#xff1a; 解析&#xff1a; 解析&#xff1a; 一级指针&#xff1a; 解析&#xff1a; 解析&#xff1a; 二维数组&a…

minion在ubuntu上的搭建步骤

在Ubuntu上搭建MinIO可以按照以下步骤进行&#xff1a; 下载MinIO服务器二进制文件&#xff1a; 通过浏览器访问 https://min.io/download 或使用以下命令获取最新的MinIO二进制文件&#xff1a;wget https://dl.min.io/server/minio/release/linux-amd64/minio赋予二进制文件…

顺序表之初

欢迎来到我的&#xff1a;世界 希望作者的文章对你有所帮助&#xff0c;有不足的地方还请指正&#xff0c;大家一起学习交流 ! 目录 线性表简介顺序表定义动态顺序表的初始化尾插头插Cheak 判断是否增容尾删&#xff1a;头删&#xff1a;打印在pos位置前插入x删除pos位置的值查…

Matlab之智能优化算法函数调用

1.句柄函数 句柄函数即我们要求的目标函数&#xff0c;以下三种算法的调用仅是求解最小值&#xff0c;若要求目标函数的最大值&#xff0c;可在返回结果中加负号。 function value Get_Fitness(x,y)value x^2 y^2;% 若要求x^2 y^2最大值可设value -(x^2 y^2); end句柄函数…

WPF基础入门-Class6-WPF通知更改

WPF基础入门 Class6-WPF通知 1、显示页面&#xff1a; <Grid><StackPanel><TextBox Text"{Binding Name}"></TextBox><TextBox Text"{Binding Title}"></TextBox><Button Command"{Binding ShowCommand}&qu…

VUE笔记(一)初识vue

一、vue的简介 1、什么是vue 官网地址:Vue.js Vue (读音 /vjuː/&#xff0c;类似于 view) 是一套用于构建用户界面的渐进式框架。 构建用户界面&#xff1a;之前在学习vue之前通过原生js对DOM操作进行构建用户界面的 使用原生js构建用户界面的不足 - 没有规范&#xff0c…

mysql sql 执行流程

监控查询缓存的命中率 show status like ‘%qcache%’; mysql 缓存机制&#xff0c;以及 8.0 为啥取消 select sql_NO_Cache * from 表 where xxx; # 不使用缓存

如何开发一款唯一艺术平台 区块链 /数字藏品

艺术作品是人类文化的瑰宝&#xff0c;而艺术平台则是连接艺术家与观众的桥梁。如何开发一款独一无二的艺术平台&#xff0c;既要满足专业艺术作品展示的要求&#xff0c;又要提供深度思考的空间&#xff0c;这是我们所面临的挑战。本文将从专业性、思考深度和逻辑性等多个方面…

几个nlp的小任务(抽取式问答)

几个nlp的小任务(抽取式问答) 安装库抽取式问答介绍、SQuAD数据集初始化参数加载、导入数据集查看数据集示例加载tokenizer对长文本处理的演示对答案的位置进行验证整合刚才的步骤对数据集中的数据进行预处理加载微调模型设置args 参数使用数据清洗设置训练函数,开始训练安装…

浅谈信息论和信息编码

目录 背景 信息是什么 信息度量 小白鼠实验 哈夫曼编码 密码学 其它应用 背景 克劳德艾尔伍德香农&#xff08;Claude Elwood Shannon&#xff09;出生于 1916 年 美国密歇根州。1936 年毕业于密歇根大学&#xff0c;获得数学和电子工程学士学位。之后&#xff0c;他在麻…

将数据类型,类名作为参数传递使用的方法

在开发中遇到一个问题&#xff0c;就是给了很多的数据类型&#xff0c;需要找出当前数据属于哪个数据类型。 举个例子&#xff0c;我们现在有一组数据类型<int, double, float, char, bool>&#xff0c;并给定一个数据char c&#xff1b;需要通过一个函数找出变量c是类型…

fegin实现方法级别注解超时配置

fegin实现方法级别注解超时配置 测试的3.18新版本已经支持方法中参数带有Options 也可以自定义配置, Options options findOptions(argv);; 使用该注解方式需配合AOP使用! 原理是包装自己的client客户端, 替换框架的客户端! 应用到生产环境需自己充验证测试 1.0 注解 Target(…

Banana Pi 开源社区在深圳国际电子展(2023)上展示全系列新产品

Banana Pi 开源社区在深圳国际电子展(2023)上展示全系列新产品 Banana Pi开源硬件社区是由广东比派科技主导、台湾鸿海科技&#xff08;富士康&#xff09;全面战略支持的开源硬件项目。Banana Pi开源硬件系列开发板&#xff0c;完成核心系统和架构设计。开发文档、软件、硬件…

远程调试环境

一、远程调试 1.安装vscode 2.打开vscode&#xff0c;下载插件Remote-SSH,用于远程连接 3.安装php debug 4.远程连接&#xff0c;连接到远端服务器 注&#xff1a;连接远程成功后&#xff0c;在远程依然要进行安装xdebug&#xff0c;刚才只是在vscode中进行的安装。 5.配置la…

Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required

项目场景&#xff1a; 最近因为公司业务需要在搭一个新架构&#xff0c;用的springboot3和jdk17,在整合mybatis多数据源的时候报错 &#xff08;引用的mybatisplus 和 mybatisplusjion的是最新的包-2023-08-26&#xff09; Error creating bean with name ‘XXXServiceImpl’:…

Python中的format()函数详细讲解

注&#xff1a;所以代码皆成功运行&#xff0c;可直接复制运行 一、基本使用 1、Python中的format()函数是一个格式字符串的函数&#xff0c;通过花括号{}识别替换字段&#xff0c;从而完成字符串的格式化。 #format后面放数字、字符串都可以 print("{}喜欢{}岁的{}&qu…