1. ViewAnimation(视图动画)
1.1 Tween Animation
1.2 Frame Animation
2. PropertyAnimation(属性动画)
先说ViewAnimation
简单地说,View Animation主要提供的是平移、旋转、缩放和透明这几种效果。
现在看看View Animation怎么定义的:
放缩
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.5"
android:toYScale="0.5"
android:repeatCount="1"
android:repeatMode="reverse" />
平移:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:toXDelta="100"
android:fillAfter="true" />
旋转:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromDegrees="-180"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="1"
android:repeatMode="reverse"
android:toDegrees="0" />
透明:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse"
android:toAlpha="0" />
共有的属性:
每个动画我们都必须设置通过Duration来设置其时长,不然的话,没有时间,谈何动呢。
duration:动画的时长
而repeatCount和repeatMode等则不是必须的,但可以应用到每个动画效果上。
repeatCount:重复的次数
repeatMode:reverse (从结束位置反过来进行动画),restart(在开始位置重新开始动画)。
fillAfter :当为true的时候,动画会停在结束的那一刻。
fillBefore:当为true的时候,动画结束后,当前View会停留在开始的那一刻。
在java中调用XML定义的动画:
animation1 = AnimationUtils.loadAnimation(MainActivity.this,R.anim.transform1);
animation2 = AnimationUtils.loadAnimation(MainActivity.this,R.anim.transform2);
animation3 = AnimationUtils.loadAnimation(MainActivity.this,R.anim.transform3);
animation4 = AnimationUtils.loadAnimation(MainActivity.this,R.anim.transform4);
把定义的动画效果通过 AnimationUtils.loadAnimation这个方法拿出来,将通过view的startAnimation函数,将animation设置给view,并开始运行。
animation1.setStartOffset(0);
v.startAnimation(animation1);
在一个XML文件当中同时定义多种效果:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<rotate
android:duration="2000"
android:fromDegrees="-180"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="0" />
<scale
android:duration="2000"
android:fromXScale="0.5"
android:fromYScale="0.5"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="2.0"
android:startOffset="2000"<!--偏移量-->
android:toYScale="2.0" />
</set>
在Java中定义
animation1 = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f,50,50); //Scale Animation
animation1.setDuration(DURATION);
animation1.setRepeatCount(1);
animation1.setInterpolator(interpolator);
animation1.setRepeatMode(Animation.REVERSE);
animation2 = new RotateAnimation(0, 180, 50, 50); //Rotate Animation
animation2.setDuration(DURATION);
animation2.setRepeatCount(1);
animation2.setInterpolator(interpolator);
animation2.setRepeatMode(Animation.REVERSE);
animation3 = new TranslateAnimation(0, 100, 0, 0); //Translate Animation
animation3.setDuration(DURATION);
animation3.setRepeatCount(1);
animation3.setInterpolator(interpolator);
animation3.setRepeatMode(Animation.REVERSE);
animation4 = new AlphaAnimation(1.0f, 0f); //Alpha Animation
animation4.setDuration(DURATION);
animation4.setRepeatCount(1);
animation4.setInterpolator(interpolator);
animation4.setRepeatMode(Animation.REVERSE);
animationSet = new AnimationSet(false); //Animation Set
animationSet.addAnimation(animation1);
animationSet.addAnimation(animation2);
animationSet.addAnimation(animation3);
View Animation其实是很简单的,而且很容易使用,一般不需要太复杂的效果,其实是够用了得。
那么它到底有什么优缺点呢,为什么在3.0后还要再加入Property Animation呢?
1)View Animation,顾名思义,只能用在View上面,没法用在非View的对象上,比如我们自定义出来的对象
2)只是提供了有限的几种效果,比如旋转,平移,缩放和透明,如果我们要实现其他的效果,则没有办法。
3)动画效果执行完之后,只是在界面上重画了这个View,而View的实际位置跟属性其实是没有变化的。怎么理解呢,比如一个按钮,我们将它从左边移到右边了,在界面上看到这个按钮已经在右边了,但是当我们去点击它的时候,却没有事件响应,只有当回原来的位置的时候,事件才被响应,就是说,人跑了,心还留在原地。
而property Animation就不会存在这个问题