android 属性动画
ObjectAnimator.ofFloat
- 第一个参数用于指定这个动画要操作的是哪个控件
- 第二个参数用于指定这个动画要操作这个控件的哪个属性
- 第三个参数是可变长参数,这个就跟ValueAnimator中的可变长参数的意义一样了,就是指这个属性值是从哪变到哪。像我们上面的代码中指定的就是将textview的alpha属性从0变到1再变到0;
1.渐变动画:
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mTv1,"alpha",1f,0f,1f); objectAnimator.setDuration(2000); objectAnimator.start();
2.旋转动画
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mTv1,"rotation",0f,360f); //设置一次动画持续时间 objectAnimator.setDuration(2500); //setRepeatCount设置重复次数 objectAnimator.setRepeatCount(1); //设置延迟时间,多久之间之后开始动画 objectAnimator.setStartDelay(0); //setRepeatMode重复类型 有两个值,reverse表示倒序回放,restart表示从头播放 objectAnimator.setRepeatMode(REVERSE); objectAnimator.start();
3.组合动画:
//旋转 Animator anim1 = ObjectAnimator.ofFloat(mTv1,"rotation",0f,360f); anim1.setDuration(3000); //平移 Animator anim2 = ObjectAnimator.ofFloat(mTv1,"translationX",0f,200f,0f); anim2.setDuration(3000); Animator anim3 = ObjectAnimator.ofFloat(mTv1,"translationY",0f,350f,0f); anim3.setDuration(3000); //伸缩 Animator anim4 = ObjectAnimator.ofFloat(mTv1,"scaleX",1f,0f,1f); anim4.setDuration(3000);
//1 旋转,平移,伸缩一起 playSequentially依次执行 //animatorSet.playSequentially(anim1,anim2,anim3,anim4); //2. 平移 playTogether一起执行 // animatorSet.playTogether(anim1,anim2,anim3,anim4); //3,在动画2和3一起 //animatorSet.play(anim2).with(anim3); //4,在动画2只后执行动画4 animatorSet.play(anim4).after(anim2); //开始执行 animatorSet.start();
ObjectAnimator
/** * 透明度渐变动画 */ private void alphaObjectAnimator() { ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mTv1,"alpha",1f,0f,1f); objectAnimator.setDuration(2000); objectAnimator.start(); }
/** * 数字从0-1000渐变 */ private void countValueAnimator() { ValueAnimator valueAnimator = ValueAnimator.ofInt(0,1000); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { int value = (int) valueAnimator.getAnimatedValue(); mTv1.setText("value : " + value); } }); valueAnimator.setDuration(3500); valueAn
/** * 位移动画 */ private void translationValueAnimator() { ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f,500f,0f); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { float value = (float) valueAnimator.getAnimatedValue(); mTv1.setTranslationY(value); } }); valueAnimator.setDuration(3000); valueAnimator.start(); }
private void fireAnimation(){ // (float fromXDelta 动画开始的点离当前View X坐标上的差值 , float toXDelta 动画结束的点离当前View X坐标上的差值 , float fromYDelta, float toYDelta) TranslateAnimation tAnim = new TranslateAnimation(0, 400, 0, 400); tAnim.setRepeatCount(1);//设置重复次数 tAnim.setRepeatMode(Animation.REVERSE);//设置反方向执行 /** * fromDegrees旋转的开始角度 toDegrees旋转的结束角度 */ RotateAnimation rAnima = new RotateAnimation(0, 70); /** * fromXScale 属性为动画起始时 X坐标上的伸缩尺寸 toXScale 属性为动画结束时 X坐标上的伸缩尺寸 fromYScale 属性为动画起始时Y坐标上的伸缩尺寸 toYScale 属性为动画结束时Y坐标上的伸缩尺寸 */ ScaleAnimation sAnima = new ScaleAnimation(0, 5, 0, 5); AlphaAnimation aAnima = new AlphaAnimation(1.0f, 0.2f); // 3: »∑∂®≥÷–¯ ±º‰ tAnim.setDuration(2000); rAnima.setDuration(2000); sAnima.setDuration(2000); aAnima.setDuration(2000); // 4: »∑∂®Interpolator tAnim.setInterpolator(new AccelerateDecelerateInterpolator()); // ∆Ù∂Ø∂ت≠ mTv1.startAnimation(tAnim); // tAnim.cancel(); // mTv1.startAnimation(rAnima); // mTv1.startAnimation(sAnima); // mTv1.startAnimation(aAnima); }