前言
动画的使用 是 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