安卓属性动画,
一般使用ObjectAnimator,例如:
ObjectAnimator translation = ObjectAnimator.ofFloat(tv, View.TRANSLATION_X, 0f, 100f);这就是一个平移的属性动画,
它可以设置执行时间
translation.setDuration(500); 可以设置重复次数 translation.setRepeatCount(3); 可以设置插值器,使之按照一定的规律运动 translation.setInterpolator(new AccelerateDecelerateInterpolator()); 开始方法 translation.start();
组合动画:
1).一个组件同时执行多个动画,使用ObjectAnimator.ofPropertyValuesHolser,
详细代码:
PropertyValuesHolder scale_x = PropertyValuesHolder.ofFloat( View.SCALE_X, 0f, 2f, 0.5f); PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat(View.ALPHA, 0f, 0.7f); ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(tv, scale_x, alpha); animator.setDuration(3000); animator.start();2),多个组件同时执行多个动画,
使用AnimatorSet:具体代码如下:
ObjectAnimator tranLeft = ObjectAnimator.ofFloat(mIvLeft, "TranslationX", 0f, -100f);
ObjectAnimator tranRight = ObjectAnimator.ofFloat(mIvRight, "TranslationX", 0f, 100f);
ObjectAnimator tranTop = ObjectAnimator.ofFloat(mIvTop, "TranslationY", 0f, -100f);
ObjectAnimator tranBottom = ObjectAnimator.ofFloat(mIvBottom, "TranslationY", 0f, 100f);
一起执行
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(tranBottom, tranLeft, tranRight, tranTop);
animatorSet.setDuration(2000);
animatorSet.start();
先后顺序
- animSet.play(anim1).with(anim2);
- animSet.play(anim2).with(anim3);
- animSet.play(anim4).after(anim3);