Android动画(四)——属性动画ValueAnimator的妙用

目录

介绍

效果图

 代码实现

xml文件


介绍

        ValueAnimator是ObjectAnimator的父类,它继承自Animator。ValueAnimaotor同样提供了ofInt、ofFloat、ofObject等静态方法,传入的参数是动画过程的开始值、中间值、结束值来构造动画对象。可以将ValueAnimator看着一个值变化器,即在给定的时间内将一个目标值从给定的开始值变化到给定的结束值。

        上一篇中我们提到,在使用ValueAnimator时通常需要添加一个动画更新的监听器,在监听器中能够获取到执行过程中的每一个动画值。

privatevoidstartValueAnimator() {
    ValueAnimatorvalueAnimator= ValueAnimator.ofFloat(0, 1);
    valueAnimator.setDuration(300);
    valueAnimator.start();
    valueAnimator.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {
        @OverridepublicvoidonAnimationUpdate(ValueAnimator animation) {
            // 动画更新过程中的动画值,可以根据动画值的变化来关联对象的属性,实现属性动画floatvalue= (float) animation.getAnimatedValue();
            Log.d("ValueAnimator", "动画值:" + value);
        }
    });
}复制代码

        ValueAnimator的使用一般会结合更新监听器AnimatorUpdateListener,大多数时候是在自定义控件时使用。

        我们可以利用ValueAnimator自定义控件实现动画打开关闭效果。

效果图

 代码实现

package com.example.animationstudy;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.app.ActionBar;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity4 extends AppCompatActivity implements View.OnClickListener {

    Button button;
    ImageView imageView;
    TextView textView;

    boolean isClose = true;

    ValueAnimator animator1;
    ValueAnimator animator2;

    LinearLayout.LayoutParams params;
//LinearLayout.LayoutParams 是 Android 中用于定义 LinearLayout(线性布局)中子视图的布局参数的类。它继承自 ViewGroup.MarginLayoutParams 类,因此包含了 Margin 相关的属性。
    //
    //LinearLayout 是一种常用的布局容器,可以水平或垂直排列子视图。而 LinearLayout.LayoutParams 则是用于描述子视图在 LinearLayout 中的布局行为,例如子视图在父布局中的位置、大小、权重等。

    int hight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        button = (Button) findViewById(R.id.button4);
        imageView = (ImageView) findViewById(R.id.imageView4);
        textView = (TextView) findViewById(R.id.text42);
        button.setOnClickListener(this);
        imageView.setOnClickListener(this);

        textView.post(new Runnable() {
            @Override
            public void run() {
                hight = textView.getMeasuredHeight();
                init();
            }
        });
//注意,在调用 getMeasuredHeight() 方法前,TextView 控件必须已经完成布局和测量,否则获取到的高度值可能是 0,因此在此之前需要确保 TextView 控件已经被添加到父容器中并已经完成了布局和测量。
//这个方法可以确保 TextView 控件完成了布局,因为它是通过 post 方法将一个 Runnable 对象发送到主线程的消息队列中,并在主线程空闲时执行。在主线程中执行的代码会在 UI 线程的消息循环中得到处理,因此可以保证在布局完成后才执行。
    }

    public void init(){
        animator1 = isClose ? ValueAnimator.ofFloat(0,180) : ValueAnimator.ofFloat(180,0);
        animator1.setDuration(500);
        animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator valueAnimator) {
                float value = (float) animator1.getAnimatedValue();
                imageView.setRotation(value);
            }
        });
        animator1.start();

        params = (LinearLayout.LayoutParams) textView.getLayoutParams();
        animator2 = isClose ? ValueAnimator.ofInt(hight,0) : ValueAnimator.ofInt(0,hight);
        animator2.setDuration(500);
        animator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                Log.d("TextView4", "onAnimationUpdate: " + value);
                params.height = value;
                textView.setLayoutParams(params);
            }
        });
        animator2.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                isClose = !isClose;
            }
        });
        animator2.start();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.button4){
            init();
        }
        if (view.getId() == R.id.imageView4){
            init();
        }
    }
}

xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity4">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:text="播放"
        android:layout_gravity="center"
        android:layout_margin="20dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/tetx41"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:padding="8dp"
            android:text="冥王语录"
            android:layout_marginLeft="20dp"
            android:textColor="#999999"
            android:textSize="16sp"/>


        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="30dp"
            android:src="@drawable/up"/>

    </LinearLayout>


    <TextView
        android:id="@+id/text42"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="         美,只不过是一瞬间的感觉,只有真实才是永恒的,而真实绝不会美



                                                爱能创造一切,也能毁灭一切。当你用爱保护羊群不受狼的伤害,那么对于狼,这种爱心就等于毁灭,因为他们会因此而活活饿死。这个世界本就如此,不是狼死就是羊死,不是弱小的狼被饿死,就是弱小的羊被咬死。或许,这世界太过残酷,然而,却因此而美丽。"
        android:textColor="#999999"
        android:textSize="16sp" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello world"
        android:layout_margin="20dp"
        android:layout_gravity="center"/>

</LinearLayout>

最后的Button没有设置点击事件,起到一个造型上的作用

上一篇:Android动画(三)——属性动画-CSDN博客

本文参考:Android 动画-CSDN博客

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

11.1.0iPortal之新增【增强其他服务注册能力】

作者&#xff1a;yx 文章目录 前言 一、使用场景二、功能说明三、举例说明 前言 11.1.0版本以前&#xff0c;注册服务的地址必须是可以访问的&#xff0c;否则会注册失败&#xff0c;如下图所示&#xff1a; 11.1.0版本开始新增“服务在线检测”功能&#xff0c;即可以实现注…

QT QWidget - 跑马灯

简介 关于前面画了个圆&#xff0c;怎么样也得跑个灯, 只是基于布局创建LED Widget而非 QTableView/QTableWidget;实现步骤 实现LED Widget LEDWidget.cpp LEDWidget::LEDWidget(QWidget *parent): QWidget(parent), m_on(false) {}void LEDWidget::paintEvent(QPaintEvent …

THEMIS---Beta Sprint Summary Essay Blog

Which course does this assignment belong to2301-MUSE社区-CSDN社区云What are the requirements for this assignmentbeta SprintThe goal of this assignmentTo summarize the beta task progress and the teams sprintsTeam NameThemisTop-of-the-line collection of essa…

单变量、双变量、多变量分析(基于iris数据集)

目录 一、数据处理 二、单变量分析 三、双变量分析 四、多变量分析 利用padas、numpy、matplotlib、seaborn库&#xff0c;对数据进行分析。 Iris数据集是非常著名的机器学习数据集之一&#xff0c;在统计学和机器学习领域被广泛应用。该数据集包含了150个样本&#xff0c;分…

如何查看PHP信息

创建一个 PHP 文件&#xff0c;比如 info.php&#xff0c;在其中添加以下代码&#xff1a; <?php phpinfo(); ?>访问这个文件&#xff08;例如&#xff0c;在浏览器中输入 http://localhost/info.php&#xff09;&#xff0c;它会显示 PHP 的所有配置信息。在这个页面…

华为配置基本QinQ示例

组网需求 如图1所示&#xff0c;网络中有两个企业&#xff0c;企业1有两个分支&#xff0c;企业2有两个分支。这两个企业的各办公地的企业网都分别和运营商网络中的SwitchA和SwitchB相连&#xff0c;且公网中存在其它厂商设备&#xff0c;其外层VLAN Tag的TPID值为0x9100。 现…

java_web_电商项目

java_web_电商项目 1.登录界面2.注册界面3. 主界面4.分页界面5.商品详情界面6. 购物车界面7.确认订单界面8.个人中心界面9.收货地址界面10.用户信息界面11.用户余额充值界面12.后台首页13.后台商品增加14.后台用户增加15.用户管理16.源码分享1.登录页面的源码2.我们的主界面 1.…

Scrapy爬虫学习

Scrapy爬虫学习一 1 scrapy框架1.1 scrapy 是什么1.2 安装scrapy 2 scrapy的使用2.1创建scrapy项目2.2 创建爬虫文件2.3爬虫文件的介绍2.4 运行爬虫文件 3 爬取当当网前十页数据3.1 dang.py&#xff1a;爬虫的主文件3.2 items.py 定义数据结构3.3 pipelines.py 管道3.4 执行命令…

万兆网络之屏蔽线序接法(中)

在介绍优质网线选购之前&#xff0c;先简单介绍一下水晶头 1毛钱一颗跟1元一颗的水晶头&#xff0c;往往是金手指厚度差别&#xff0c;你可以想象压制的时候可能会有什么情况 另外&#xff0c;一些3元一颗的镀金水晶头会有15U、30U之类的是电镀厚度单位&#xff0c;数值越大镀…

Maui blazor与sqlite开发一个增删改查

在android端增删改不能运行。也看不出来是什么&#xff0c;但运行到windows可以运行。 引入sqlite-net-pcl 开发Model using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.T…

工具在手,创作无忧:一键下载安装Auto CAD工具,让艺术创作更加轻松愉悦!

不要再浪费时间在网上寻找Auto CAD的安装包了&#xff01;因为你所需的一切都可以在这里找到&#xff01;作为全球领先的设计和绘图软件&#xff0c;Auto CAD为艺术家、设计师和工程师们提供了无限的创作潜力。不论是建筑设计、工业设计还是室内装饰&#xff0c;Auto CAD都能助…

HYDRA爆破之王(服务多)(用法简单)

#江南的江 #每日鸡汤&#xff1a;如果你向神求助&#xff0c;说明你相信神的能力。如果神没有帮助你&#xff0c;说明神相信你的能力。 #初心和目标&#xff1a;善用网络安全。。。 HYDRA 1.Hydra的简介 --------------------------------------------------------------------…

写递归函数的一些思考

当编写递归函数时&#xff0c;有几个关键的思考点可以帮助你设计和实现递归算法&#xff1a; 定义递归的基本情况&#xff1a;确定递归函数应该在何时终止&#xff0c;即递归的基本情况。这是一个递归的出口条件&#xff0c;确保递归不会无限进行下去。基本情况应该是可以直接求…

RabbitMq交换机详解

目录 1.交换机类型2.Fanout交换机2.1.声明队列和交换机2.2.消息发送2.3.消息接收2.4.总结 3.Direct交换机3.1.声明队列和交换机3.2.消息接收3.3.消息发送3.4.总结 4.Topic交换机4.1.说明4.2.消息发送4.3.消息接收4.4.总结 5.Headers交换机5.1.说明5.2.消息发送5.3.消息接收5.4.…

Ubuntu 常用命令之 ln 命令用法介绍

ln命令在Ubuntu系统中用于创建硬链接或符号链接。硬链接是指向文件的物理地址&#xff0c;而符号链接&#xff08;也称为软链接&#xff09;是指向文件路径的引用。 命令格式&#xff1a;ln [选项]... [-T] 目标&#xff08;源文件&#xff09; 链接&#xff08;目标文件&…

12.16_黑马数据结构与算法笔记Java

目录 167 B树 remove 168 B树 remove 搭架子 169 B树 remove case1-4 170 B树 remove case5-6分析 171 B树 remove case5 旋转 172 B树 remove case5 合并 173 B树 remove case6 174 B树 remove 演示1 175 B树 remove 演示2 176 哈希表 概述 177 哈希表 hash码映射索…

将html的radio单选框自定义样式为正方形和对号

将html的radio单选框自定义样式为正方形和对号 背景&#xff1a; 如何能把html的<input type"radio" name"option">改成自定义的样式呢&#xff1f;比如想要把他变成正方形&#xff0c;选中的时候是对号。默认的样式太丑了 默认样式&#xff1a; 自…

LCR 146. 螺旋遍历二维数组

解题思路&#xff1a; class Solution {public int[] spiralArray(int[][] array) {if(array.length 0) return new int[0];int l 0, r array[0].length - 1;int t 0, b array.length - 1;int x 0;int[] res new int[(r 1) * (b 1)];while(true) {for(int i l; i <…

Linux(23):Linux 核心编译与管理

编译前的任务&#xff1a;认识核心与取得核心原始码 Linux 其实指的是核心。这个【核心(kernel)】是整个操作系统的最底层&#xff0c;他负责了整个硬件的驱动&#xff0c;以及提供各种系统所需的核心功能&#xff0c;包括防火墙机制、是否支持 LVM 或 Quota 等文件系统等等&a…

C语言入门基础(二)

基本概念 地址 计算机的内存是一块用于存储数据的空间&#xff0c;由一系列连续的存储单元组成&#xff0c;就像下面这样&#xff0c; 每一个单元格都表示1个Bit&#xff0c;一个bit在EE专业的同学看来就是高低电位&#xff0c;而在CS同学看来就是0&#xff0c;1两种状态。 …