您的位置:首页 > 移动开发 > Objective-C

【转】android 属性动画之 ObjectAnimator

2016-08-24 17:43 507 查看
原文网址:http://blog.csdn.net/feiduclear_up/article/details/39255083

前面一篇博客讲解了 android 简单动画之 animtion,这里来讲解一下android 3.0之后添加的一些动画 animator 中的 ObjectAnimator 。

属性动画概念:

所谓属性动画:改变一切能改变的对象的属性值,不同于补间动画:只能改变 alpha,scale,rotate,translate。听着有点抽象,举例子说明

补间动画能实现的:

1.alpha

[java] view plain copy







//第一个参数为 view对象,第二个参数为 动画改变的类型,第三,第四个参数依次是开始透明度和结束透明度。

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

[java] view plain copy







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

[java] view plain copy







ObjectAnimator translationUp = ObjectAnimator.ofFloat(button, "Y",

button.getY(), 0);

translationUp.setInterpolator(new DecelerateInterpolator());

translationUp.setDuration(1500);

translationUp.start();

4. rotate

[java] view plain copy







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();

补间动画不能实现的:

5.android 改变背景颜色的动画实现如下

[java] view plain copy







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);

/*

* ArgbEvaluator:这种评估者可以用来执行类型之间的插值整数值代表ARGB颜色。

* FloatEvaluator:这种评估者可以用来执行浮点值之间的插值。

* IntEvaluator:这种评估者可以用来执行类型int值之间的插值。

* RectEvaluator:这种评估者可以用来执行类型之间的插值矩形值。

*

* 由于本例是改变View的backgroundColor属性的背景颜色所以此处使用ArgbEvaluator

*/

translationUp.setEvaluator(new ArgbEvaluator());

translationUp.start();

更多关于android 属性动画的相关知识请参考详细博客 Android属性动画Property Animation系列一之ValueAnimator
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: