这里我们首先看看简单的用法
1.透明度变化的animator(alpha)
/**
* @param tvName view对象
* @param alpha 动画改变的类型也可以是scaleX,scaleY等等
* @param 0f 开始透明度
* @param 1f 结束透明度
**/
ObjectAnimator alpha = ObjectAnimator.ofFloat(tvName, "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
/**
* @param tvName view对象
* @param Y 动画改变的类型
* @param tvName.getY() 起始属性位置
* @param 0 结束属性位置
**/
ObjectAnimator translationUp = ObjectAnimator.ofFloat(tvName , "Y", tvName.getY(), 0);
translationUp.setInterpolator(new DecelerateInterpolator());
translationUp.setDuration(1000);
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();
5.综合
PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0.3f, 1f);
PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.3f, 1f);
PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.3f, 1f);
ObjectAnimator objectAnimator1 = ObjectAnimator.ofPropertyValuesHolder(tvName, alpha, scaleX, scaleY);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofPropertyValuesHolder(ivLogo, alpha, scaleX, scaleY);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(objectAnimator1, objectAnimator2);
animatorSet.setInterpolator(new AccelerateInterpolator());
animatorSet.setDuration(2000);
animatorSet.start();