android实现动画的方式:
1. View Animation:补间动画
1. 位移,缩放,旋转,渐变,集合
2. Drawable Animation:帧动画
3. 自定义动画
1. 自己通过继承Animation自己实现动画类,但是有了属性动画,可以用属性动画替代这种做法.
4. Property Animation:属性动画
1. 补间动画可以做的属性动画都可以做,反之则不是
2. android 3.0才有
如果让android 3.0也可以执行属性动画?
使用nineoldandroids-2.2.0.jar
属性动画和补间动画的最大区别是?
补间动画,控件看似改变了位置,但是控件的焦点原来的位置
属性动画2个关键类
* public class ValueAnimator extends Animator
public static ValueAnimator ofFloat(float... values)
public static ValueAnimator ofInt(int... values)
* public final class ObjectAnimator extends ValueAnimator
public static ObjectAnimator ofFloat(Object target, String propertyName, float... values)
public static <T> ObjectAnimator ofInt(T target, Property<T, Integer> property, int... values)
属性动画2个关键监听
* 监听动画过程
public void addListener(AnimatorListener listener)
* 得到动画执行过程中的渐变值
public void addUpdateListener(AnimatorUpdateListener listener)
-
位移动画(translationX,translationY,translation)
ObjectAnimator animator = ObjectAnimator.ofFloat(mIv, "translationX", 0, 100, 200, 100); animator.setInterpolator(new AccelerateDecelerateInterpolator()); animator.setDuration(300); animator.start();
-
缩放(scrollX,scrollY,scroll)
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(mIv, "scaleX", 0, 1, 2, 1, 0); scaleXAnimator.setDuration(1000); scaleXAnimator.start();
-
透明度(alpha)
ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mIv, "alpha", 0, 1, 0); alphaAnimator.setDuration(2000); alphaAnimator.start();
-
旋转(rotateX,rotateY,rotation)
ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(mIv, "rotation", 0, 360, 0); rotationAnimator.setDuration(1000); rotationAnimator.start();
-
设置背景颜色渐变
ObjectAnimator backgroundColorAnimator = ObjectAnimator.ofObject(mIv, "backgroundColor", new ArgbEvaluator(), Color.RED, Color.BLUE); (设置动画循环展示的方式和次数 )backgroundColorAnimator.setRepeatMode(ObjectAnimator.REVERSE); backgroundColorAnimator.setRepeatCount(ObjectAnimator.INFINITE); backgroundColorAnimator.start();
-
组合动画
ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(mIv, "rotation", 0, 360, 0); ObjectAnimator translationAnimator = ObjectAnimator.ofFloat(mIv, "translationX", 0, 100, 200, 100, 0); AnimatorSet set = new AnimatorSet(); set.playSequentially(rotationAnimator, translationAnimator);// 顺序播放 set.playTogether(rotationAnimator, translationAnimator);// 一起播放 set.setDuration(2000); set.start();
-
xml定义组合动画
Animator animator =AnimatorInflater.loadAnimator(MainActivity.this, R.animator.setrotationtranslate);animator.setTarget(mIv);animator.start();
-
监听动画执行过程
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100); valueAnimator.addListener(new AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { } @Override public void onAnimationCancel(Animator animation) { } }); valueAnimator.start(); }
-
得到动画渐变值
valueAnimator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int progress = (Integer) animation.getAnimatedValue(); ((Button) findViewById(R.id.btn10)).setText(progress + "%"); } });