注:属性动画主要用到的类是 ObjectAnimator 一般单个动画用到的方法主要是 .setDuration(); .start();
综合的要用到 AnimatorSet类 把各个效果结合在一起的方法: animatorSet.play(rotate).with(alpha).after(translation);
上代码:
public class MainActivity extends AppCompatActivity {
private TextView tv;
private Button start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.text_view);
start=(Button)findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// tv.setBackgroundColor(Color.YELLOW);
//设置属性动画的颜色
// ObjectAnimator animation=ObjectAnimator.ofArgb(tv,"BackgroundColor",Color.RED,Color.GREEN,Color.BLUE);
//平移
// ObjectAnimator animation=ObjectAnimator.ofFloat(tv,"translationX",0,200);
//渐变
// ObjectAnimator animation=ObjectAnimator.ofFloat(tv,"alpha",1.0f,0.0f);
//旋转
/* ObjectAnimator animation=ObjectAnimator.ofFloat(tv,"rotation",0,360);
animation.setDuration(5000);
animation.start();*/
//属性动画综合
//平移
ObjectAnimator translation=ObjectAnimator.ofFloat(tv,"translationX",0,200);
//渐变
ObjectAnimator alpha=ObjectAnimator.ofFloat(tv,"alpha",1.0f,0.0f,1.0f,0.0f);
//旋转
ObjectAnimator rotate=ObjectAnimator.ofFloat(tv,"rotation",0,360);
AnimatorSet animatorSet=new AnimatorSet();
animatorSet.setDuration(5000);
animatorSet.play(rotate).with(alpha).after(translation);
animatorSet.start();
}
});
}
}
public class MainActivity extends AppCompatActivity { private TextView tv; private Button start; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv=(TextView)findViewById(R.id.text_view); start=(Button)findViewById(R.id.start); start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // tv.setBackgroundColor(Color.YELLOW); //设置属性动画的颜色 // ObjectAnimator animation=ObjectAnimator.ofArgb(tv,"BackgroundColor",Color.RED,Color.GREEN,Color.BLUE); //平移 // ObjectAnimator animation=ObjectAnimator.ofFloat(tv,"translationX",0,200); //渐变 // ObjectAnimator animation=ObjectAnimator.ofFloat(tv,"alpha",1.0f,0.0f); //旋转 /* ObjectAnimator animation=ObjectAnimator.ofFloat(tv,"rotation",0,360); animation.setDuration(5000); animation.start();*/ //属性动画综合 //平移 ObjectAnimator translation=ObjectAnimator.ofFloat(tv,"translationX",0,200); //渐变 ObjectAnimator alpha=ObjectAnimator.ofFloat(tv,"alpha",1.0f,0.0f,1.0f,0.0f); //旋转 ObjectAnimator rotate=ObjectAnimator.ofFloat(tv,"rotation",0,360); AnimatorSet animatorSet=new AnimatorSet(); animatorSet.setDuration(5000); animatorSet.play(rotate).with(alpha).after(translation); animatorSet.start(); } }); } }