浅谈Android动画之属性动画ValueAnimator和ObjectAnimator

前言

动画的使用 是 Android 开发中常用的知识
本文将详细介绍 Android 动画中 属性动画的原理 & 使用

简介

在这里插入图片描述

具体使用

在这里插入图片描述
而我们主要使用的就是ValueAnimator和ObjectAnimator类
ValueAnimator:可以设置开始值和结束值来动态改变view的移动位置
ObjectAnimator:功能更加强大,可以控制位移、透明度、旋转、缩放。

ValueAnimator的使用

实例说明:

1.设置布局文件在activity_main.xml修改

<?xml version="1.0" encoding="utf-8"?>
<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">
    <Button
        android:id="@+id/buttonValue"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="时间动画"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonSequentially" />

2.设置动画在MainActicity.java中修改

     // 创建动画作用对象:此处以Button为例
        buttonValue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // ValueAnimator.ofInt()内置了整型估值器,直接采用默认的.不需要设置,即默认设置了如何从初始值 过渡到 结束值
                ValueAnimator valueAnimator = ValueAnimator.ofInt(0,999);
                // 设置动画运行的时长
                valueAnimator.setDuration(5000);
                // 设置动画延迟播放时间
                valueAnimator.setStartDelay(100);
                // 设置动画重复播放次数 = 重放次数+1
                // 动画播放次数 = infinite时,动画无限重复
                valueAnimator.setRepeatCount(1);
                // 设置重复播放动画模式
                // ValueAnimator.RESTART(默认):正序重放
                // ValueAnimator.REVERSE:倒序回放
                valueAnimator.setRepeatMode(ValueAnimator.RESTART);

                //将改变的值手动赋值给对象的属性值:通过动画的更新监听器
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        // 获得改变后的值
                        Integer value = (Integer) valueAnimator.getAnimatedValue();

                        buttonValue.setText(""+value);
                    }
                });
                // 启动动画
                valueAnimator.start();
            }
        });

效果图

在这里插入图片描述

ObjectAnimato的使用

1、单一动画

旋转

实例说明:

1.设置布局文件在activity_main.xml修改

<?xml version="1.0" encoding="utf-8"?>
<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=".Ma
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值