在Android系统中,提供了两种动画框架:
1)View Animation(视图动画),而View Animation 里面又包含了下面两种模式
1.1)Tween Animation (也称之为View Animation,狭义的,不过现在官方也将之为View Animation了)
1.2)Frame Animation (也称之为Drawable Animation)
2)Property Animation(属性动画),这是在Android 3.0(API Level 11)之后才支持的框架。
所以,不考虑层次,只考虑实现,Android中就存在以下三种动画框架:
1)View Animation
2)Drawable Animation
3)Property Animation
今天我们就来讲一下View Animation(Tween Animation)的用法,然后最后总结一下关于View Animation 的优缺点。
简单地说,View Animation提供了一些在View上简单的动画效果,包括Tranlsate(平移),Rotate(旋转),Scale(缩放)和 Alpha(透明)这几种动画效果。
我们先来看一下效果图:
简单说明一下:
1)前面四个按钮分别代表了四种效果,点击执行对应的效果
2)如果fromXml被选中的话,表明效果是从xml加载的,如果没有选中,则是在Java中定义的
3)每种效果都可以通过Interpolator按钮来选择对应的Interpolator,这里只简单用了五种插值器。
4)Set为了方便展示,没有添加Alpha的效果,其它三种都加了,可以通过下面的Sequence和simultaneous单选按钮来选择是顺序播放还是同时播放各种效果。
下面我们说说View Animation都是怎么实现的。
View Animation的实现方式有两种:一是在XML中定义;二是在Java 代码中定义
XML 定义
1. Scale(缩放)
首先我们要先在 res/amin/中创建一个定义动画的xml文件,就叫transform.xml吧(这里要注意,关于View Animation的动画效果,都是要放在 /res/anim 路径下的) 代码如下:
- <?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"?>
<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" />
关于scale的参数有:
fromXScale, fromYScale:X/Y 方向开始的比例(1.0 就是原来比例的1.0倍)
toXScale, toYScale:X/Y 方向最终的比例
pivotX, pivotY:缩放的轴(50则是在其父容器50%的位置,,加上百分号,50%,则是相对于其本身,也就是绕其中心)
2. Translate (平移)
- <?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"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:toXDelta="100"
android:fillAfter="true" />
关于translate的参数有:
fromXDelta : 平移开始的位置
toXDelta: 平移结束的位置
3. Rotate (旋转)
- <?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"?>
<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" />
fromDegrees : 开始的角度
toDegrees : 结束的角度
pivotX, pivotY:旋转的轴
4. Alpha (透明 )
- <?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" />
<?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" />
fromAlpha: 开始时的透明度(1.0 完全不透明, 0 完全透明,等于不见了)
toAlpha:结束时的透明度
5. 共有的属性
每个动画我们都必须设置通过Duration来设置其时长,不然的话,没有时间,谈何动呢。
duration:动画的时长
而repeatCount和repeatMode等则不是必须的,但可以应用到每个动画效果上。
repeatCount:重复的次数
repeatMode:reverse (从结束位置反过来进行动画),restart(在开始位置重新开始动画)。
fillAfter :当为true的时候,动画会停在结束的那一刻。fillBefore:当为true的时候,动画结束后,当前View会停留在开始的那一刻。
等等等,就不多说了。
6.在Java中调用
当把这些参数都定义好之后,那么就可以在Java中调用我们定义好的这个动画文件了,具体代码如下:
- if (cbFromXml.isChecked()) {
- 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);
- }
if (cbFromXml.isChecked()) {
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,并开始运行。
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.button1:
- animation1.setStartOffset(0);
- v.startAnimation(animation1);
- break;
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
animation1.setStartOffset(0);
v.startAnimation(animation1);
break;
这样,我们就可以在上面的效果图中看到各种的效果了。
当然,这只是一种效果的动画,那我们如果要将各种效果同时进行,或者按顺序来进行,怎么办呢?
我们可以通过定义Set,来实现这个效果:
- <?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>
<?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中,调用方法是一样的。在上面的设置中,每个动画效果都有一个startOffset参数,是设置动画开始的偏移量的,如果我们要动画顺序执行,那我们就必须设置各个效果的偏移时间,如上面,rotate是马上执行的,而scale效果会在2秒后才执行,刚好在rotate效果执行之后。
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);
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);
设置的各种属性其实是一一对应于我们在XML中定义的各种属性的,定义好了之后,其实就跟XML中定义好并加载到Java代码中之后一样,直接调用View.startAnimation函数就可以了,在这里就不详细说了。
总结
而property Animation就不会存在这个问题,请看下一篇文章关于Property Animation的用法:
Android动画学习Demo(2) 关于Property Animation的用法及总结
转载自:http://blog.csdn.net/linmiansheng/article/details/18676845