属性
程序员文章站
2022-02-05 19:28:47
...
package com.example.valueanimation;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* java写法 对单一对象使用动画
*/
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button4 = findViewById(R.id.button4);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "ccc", Toast.LENGTH_SHORT).show();
}
}
);
/**
* ObjectAnimator extend ValueAnimator extend Animator
*/
//将button1从不透明改为透明再 变为不透明
ObjectAnimator animator1 = ObjectAnimator.ofFloat(button1, "alpha", 1f, 0f, 1f);
animator1.setDuration(5000);
animator1.start();
//将button2旋转360度
ObjectAnimator animator2 = ObjectAnimator.ofFloat(button2, "rotation", 0.0f, 360.0f);
animator2.setDuration(3000);
animator2.start();
//将button3移动
ObjectAnimator animator3 = ObjectAnimator.ofFloat(button3, "translationY",
0, 200);
animator3.setDuration(3000);
animator3.start();
//将Button4缩放
ObjectAnimator animator4 = ObjectAnimator.ofFloat(button4, "scaleY", 1f, 5f, 2f);
animator4.setDuration(2000);
animator4.start();
/**
* 组合动画
* after(Animator anim) 将现有动画插入到传入的动画之后执行
* after(long delay) 将现有动画延迟指定毫秒后执行
* before(Animator anim) 将现有动画插入到传入的动画之前执行
* with(Animator anim) 将现有动画和传入的动画同时执行
*/
AnimatorSet animSet = new AnimatorSet();
animSet.play(animator1).with(animator2).after(animator3);
animSet.setDuration(5000);
animSet.start();
/**
* 对单一对象进行处理 ValueAnimator 添加监听处理方法
*/
// ValueAnimator valueAnimator = ValueAnimator.ofFloat(0.0f,360.0f);
// valueAnimator.setDuration(5000);
// valueAnimator.setTarget(button1);
// valueAnimator.setTarget(button2);
// valueAnimator.start();
// valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
// @Override
// public void onAnimationUpdate(ValueAnimator animation) {
// button1.setRotationY((Float)animation.getAnimatedValue()); //
// button2.setRotation((Float)animation.getAnimatedValue());
// }
// });
// final ValueAnimator valueAnimator = ValueAnimator.ofInt(0,100);
// valueAnimator.setDuration(5000);
// valueAnimator.setRepeatCount(2);
// valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
// @Override
// public void onAnimationUpdate(ValueAnimator animation) {
// int value = (int)valueAnimator.getAnimatedValue();
// textView.setText(value+"");
// }
// });
//valueAnimator.start();
/**
* Animator事件监听器
*/
animator1.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
//开始
Toast.makeText(MainActivity.this, "动画开始", Toast.LENGTH_SHORT).show();
button1.setText("kais");
//Log.d("kasi","开始");
}
@Override
public void onAnimationEnd(Animator animation) {
//结束
Toast.makeText(MainActivity.this, "动画结束", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationCancel(Animator animation) {
//取消
}
@Override
public void onAnimationRepeat(Animator animation) {
//重复
}
});
/**
* xml写法
*/
// View view =getWindow().getDecorView();
// Animator animator = AnimatorInflater.loadAnimator(MainActivity.this,R.animator.anim_drawble);
// animator.setTarget(button1);
// animator.start();
}
}
上一篇: New Document