属性动画概念:
所谓属性动画:改变一切能改变的对象的属性值,不同于补间动画:只能改变 alpha,scale,rotate,translate。
/* * 第一个参数:目标 * 第二个参数:动画的类型 * 第三个参数和第四个参数:改变的值 * */
1、alpha:
- ObjectAnimator alpha = ObjectAnimator.ofFloat(text, "alpha", 0f, 1f);
- alpha.setDuration(2000);//设置动画时间
- alpha.setInterpolator(new DecelerateInterpolator());//设置动画插入器,减速
- alpha.setRepeatCount(-1);//设置动画重复次数,这里-1代表无限
- alpha.setRepeatMode(Animation.REVERSE);//设置动画循环模式。
- alpha.start();//启动动画。
2、scale:
- AnimatorSet animatorSet = new AnimatorSet();//组合动画
- ObjectAnimator scaleX = ObjectAnimator.ofFloat(text, "scaleX", 1f, 0f);
- ObjectAnimator scaleY = ObjectAnimator.ofFloat(text, "scaleY", 1f, 0f);
- animatorSet.setDuration(2000);
- animatorSet.setInterpolator(new DecelerateInterpolator());
- animatorSet.play(scaleX).with(scaleY);//两个动画同时开始
- animatorSet.start();
3、translate:
- ObjectAnimator translationUp = ObjectAnimator.ofFloat(button, "Y",
- button.getY(), 0);
- translationUp.setInterpolator(new DecelerateInterpolator());
- translationUp.setDuration(1500);
- translationUp.start();
4、rotate:
- AnimatorSet set = new AnimatorSet() ;//组合动画
- ObjectAnimator anim = ObjectAnimator .ofFloat(phone, "rotationX", 0f, 180f);
- anim.setDuration(2000);
- ObjectAnimator anim2 = ObjectAnimator .ofFloat(phone, "rotationX", 180f, 0f);
- anim2.setDuration(2000);
- ObjectAnimator anim3 = ObjectAnimator .ofFloat(phone, "rotationY", 0f, 180f);
- anim3.setDuration(2000);
- ObjectAnimator anim4 = ObjectAnimator .ofFloat(phone, "rotationY", 180f, 0f);
- anim4.setDuration(2000);
- set.play(anim).before(anim2); //先执行anim动画之后在执行anim2
- set.play(anim3).before(anim4) ;
- set.start();