1. 属性动画
package com.example.hello;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class AnimatorActivity extends AppCompatActivity {
private TextView tvAnimator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animator);
tvAnimator = findViewById(R.id.tv_animator);
tvAnimator.animate()
// 按 x 方向旋转 +720 度
.rotationXBy(720)
// 按 y 向下移动 500
.translationY(500)
// 设置透明度
.alpha(0.2F)
// 经历时间 3s
.setDuration(3000).start();
}
}
<?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:padding="15dp"
tools:context=".AnimatorActivity">
<TextView
android:id="@+id/tv_animator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:gravity="center"
android:padding="15dp"
android:text="@string/animator_test"
android:textColor="@color/white" />
</LinearLayout>
2. 补充动画
第一个
ValueAnimator valueAnimator = ValueAnimator.ofArgb(0, 100).setDuration(2000);
valueAnimator.addUpdateListener(animation -> {
// animation.getAnimatedValue() 范围 valueAnimator 的实际值
Log.i("animation.getAnimatedValue(): ", animation.getAnimatedValue() + "");
// animation.getAnimatedFraction() 范围为 [0.0, 1.0] 进度值
Log.i("animation.getAnimatedFraction(): ", animation.getAnimatedFraction() + "");
// doing something...
});
valueAnimator.start();
第二个
// 以 tvAnimal 为对象, 进行 translationY 操作, 操作内容为 从 0 向下 400, 再向上 400 - 100, 再向下 400, 再向上 300, 再向下 400, 再向上 300.
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(tvAnimator, "translationY", 0, 400, 100, 500, 200, 600, 300);
objectAnimator.setDuration(5000);
objectAnimator.addUpdateListener(animation -> {
// animation.getAnimatedValue() 范围 objectAnimator 的实际值
Log.i("animation.getAnimatedValue(): ", animation.getAnimatedValue() + "");
// animation.getAnimatedFraction() 范围为 [0.0, 1.0] 进度值
Log.i("animation.getAnimatedFraction(): ", animation.getAnimatedFraction() + "");
// doing something...
});
objectAnimator.start();