前面一篇博客讲解了 android 补间动画,这里来讲解一下android 3.0之后添加的一些动画 animator 中的 ObjectAnimator 。
属性动画概念:
所谓属性动画:改变一切能改变的对象的属性值,不同于补间动画:只能改变 alpha,scale,rotate,translate。听着有点抽象,举例子说明
补间动画能实现的:
1.alpha
-
- ObjectAnimator alpha = ObjectAnimator.ofFloat(text, "alpha", 0f, 1f);
- alpha.setDuration(2000);
- alpha.setInterpolator(new DecelerateInterpolator());
- alpha.setRepeatCount(-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);
- set.play(anim3).before(anim4) ;
- set.start();
补间动画不能实现的:
5.android 改变背景颜色的动画实现如下
- ObjectAnimator translationUp = ObjectAnimator.ofInt(button,
- "backgroundColor", Color.RED, Color.BLUE, Color.GRAY,
- Color.GREEN);
- translationUp.setInterpolator(new DecelerateInterpolator());
- translationUp.setDuration(1500);
- translationUp.setRepeatCount(-1);
- translationUp.setRepeatMode(Animation.REVERSE);
-
-
-
-
-
-
-
-
-
- translationUp.setEvaluator(new ArgbEvaluator());
- translationUp.start();
更多关于android 属性动画的相关知识请参考详细博客 Android属性动画 ValueAnimator