android的动画相关参数说明

动画类型:

Android中animation由四种类型组成

在XML文件中:

  alpha         渐变透明度渐变效果

  scale          渐变尺寸伸缩渐变效果

  translate    画面转换位置移动渐变效果

  rotate        画面转移旋转渐变效果

在Java 源码中定义了相应的类,可以使用这些类的方法来获取和操作相应的属性:

1 AlphaAnimation          //渐变透明度渐变效果
2 ScaleAnimation //渐变尺寸伸缩渐变效果
3 TranslateAnimation //画面转换位置移动渐变效果
4 RotateAnimation // 画面转移旋转渐变效果

Animation 类及其子类的类图如下所示:

android类及其子类
Tween Animation:

一个tween动画将对视图对象中的内容进行一系列简单的转换(位置,大小,旋转,透明度)。如果你有一个文本视图对象,你可以移动它,旋转它,让它变大或让它变小,如果文字下面还有背景图像,背景图像也会随着文件进行转换。

使用XML来定义Tween Animation

动画的XML文件在工程中res/anim目录,这个文件必须包含一个根元素,可以使<alpha><scale> <translate> <rotate>插值元素或者是把上面的元素都放入<set>元素组中,默认情况下,所有的动画指令都是同时发生的,为了让他们按序列发生,需要设置一个特殊的属性startOffset。动画的指令定义了你想要发生什么样的转换,当他们发生了,应该执行多长时间,转换可以是连续的也可以使同时的。例如,你让文本内容从左边移动到右边,然后旋转180度,或者在移动的过程中同时旋转,没个转换需要设置一些特殊的参数(开始和结束的大小尺寸的大小变化,开始和结束的旋转角度等等,也可以设置些基本的参数(例如,开始时间与周期),如果让几个转换同时发生,可以给它们设置相同的开始时间,如果按序列的话,计算开始时间加上其周期。

Tween Animation共同的节点属性

属性[类型]功能备注
Duration[long]属性为动画持续时间时间以毫秒为单位
fillAfter [boolean]当设置为true ,该动画转化在动画结束后被应用
fillBefore[boolean]当设置为true ,该动画转化在动画开始前被应用

interpolator

指定一个动画的插入器有一些常见的插入器
accelerate_decelerate_interpolator
加速-减速 动画插入器
accelerate_interpolator
加速-动画插入器
decelerate_interpolator
减速- 动画插入器
其他的属于特定的动画效果
repeatCount[int]动画的重复次数
RepeatMode[int]定义重复的行为1:重新开始  2:plays backward
startOffset[long]动画之间的时间间隔,从上次动画停多少时间开始执行下个动画
zAdjustment[int]定义动画的Z Order的改变0:保持Z Order不变
1:保持在最上层
-1:保持在最下层

表二

XML节点功能说明
alpha渐变透明度动画效果
<alpha
android:fromAlpha=”0.1″
android:toAlpha=”1.0″
android:duration=”3000″ />
fromAlpha

属性为动画起始时透明度

0.0表示完全透明
1.0表示完全不透明
以上值取0.0-1.0之间的float数据类型的数字

duration为动画持续时间,ms单位

toAlpha

属性为动画结束时透明度

表三

scale渐变尺寸伸缩动画效果
<scale
android:interpolator= “@android:anim/accelerate_decelerate_interpolator”
android:fromXScale=”0.0″
android:toXScale=”1.4″
android:fromYScale=”0.0″
android:toYScale=”1.4″
android:pivotX=”50%”
android:pivotY=”50%”
android:fillAfter=”false”
android:startOffset=“700”
android:duration=”700″
android:repeatCount=”10″ />
fromXScale[float] fromYScale[float]为动画起始时,X、Y坐标上的伸缩尺寸0.0表示收缩到没有
1.0表示正常无伸缩
值小于1.0表示收缩
值大于1.0表示放大
toXScale [float]
toYScale[float]
为动画结束时,X、Y坐标上的伸缩尺寸
pivotX[float]
pivotY[float]
为动画相对于物件的X、Y坐标的开始位置属性值说明:50%为物件的X或Y方向坐标上的中点位置,相对于自身。”50“代表绝对位置,“50%p”这代表相对于父控件来说。

表四

translate画面转换位置移动动画效果
<translate
android:fromXDelta=”30″
android:toXDelta=”-80″
android:fromYDelta=”30″
android:toYDelta=”300″
android:duration=”2000″ />
fromXDelta
toXDelta
为动画、结束起始时 X坐标上的位置
fromYDelta
toYDelta
为动画、结束起始时 Y坐标上的位置

表五

rotate画面转移旋转动画效果
<rotate
android:interpolator=”@android:anim/accelerate_decelerate_interpolator”
android:fromDegrees=”0″
android:toDegrees=”+350″
android:pivotX=”50%”
android:pivotY=”50%”
android:duration=”3000″ />
fromDegrees为动画起始时物件的角度说明
当角度为负数——表示逆时针旋转
当角度为正数——表示顺时针旋转
(负数from——to正数:顺时针旋转)
(负数from——to负数:逆时针旋转)
(正数from——to正数:顺时针旋转)
(正数from——to负数:逆时针旋转)
toDegrees属性为动画结束时物件旋转的角度 可以大于360度
pivotX
pivotY
为动画相对于物件的X、Y坐标的开始位属性值说明:50%为物件的X或Y方向坐标上的中点位置,相对于自身。”50“代表绝对位置,“50%p”这代表相对于父控件来说。

下面给出一个完整的XML定义

 1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android"
3 android:interpolator="@android:anim/accelerate_interpolator"
4 android:shareInterpolator="true">
5 <alpha
6 android:fromAlpha="0.1"
7 android:toAlpha="1.0"
8 android:duration="3000"/>
9 <rotate
10 android:fromDegrees="0"
11 android:toDegrees="-360"
12 android:pivotX="50%"
13 android:pivotY="50%"
14 android:duration="3000"
15 />
16 <scale
17 android:fromXScale="0.1"
18 android:toXScale="2.0"
19 android:fromYScale="0.1"
20 android:toYScale="2.0"
21 android:pivotX="50%"
22 android:pivotY="50%"
23 android:duration="3000"
24 />
25 <translate
26 android:fromXDelta="0%p"
27 android:toXDelta="1000%p"
28 android:fromYDelta="0"
29 android:toYDelta="100"
30 android:duration="3000"
31 android:fillBefore="false"
32 android:fillAfter="true"
33 android:startOffset="500"
34 android:repeatCount="5"
35 android:interpolator="@android:anim/overshoot_interpolator"
36 android:zAdjustment="bottom"
37 android:detachWallpaper="false"
38 android:repeatMode="reverse"/>
39 </set>

这里的shareInterpolator是选择是否让所有的Animationd都具有相同的动画插入器。


在Anim中定义好了Animation,在java代码中如何引用呢?

使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件

1 Animation alphaAnimation = AnimationUtils.loadAnimation(
2 AnimationsAPIActivity2.this, R.anim.alpha);
3 imageView.startAnimation(alphaAnimation);

这样利用xml文件就实现了动画效果。


如何在Java代码中定义动画

有五步:

1.创建AnimationSet对象。

2.根据需要创建相应的Animation对象。

3.根据对动画的需求,为Animation对象设置相应的数据。

4.将Animation对象添加到AnimationSet对象中。

5.使用控件对象开始执行AnimationSet。

下面通过一个简单的例子来说明:

 1 package cn.yj3g.AnimationsAPI;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.view.View;
6 import android.view.View.OnClickListener;
7 import android.view.animation.AlphaAnimation;
8 import android.view.animation.Animation;
9 import android.view.animation.AnimationSet;
10 import android.view.animation.RotateAnimation;
11 import android.view.animation.ScaleAnimation;
12 import android.view.animation.TranslateAnimation;
13 import android.widget.Button;
14 import android.widget.ImageView;
15
16 public class AnimationsAPIActivity extends Activity implements OnClickListener {
17 private ImageView imageView;
18 private Button alphaButton;
19 private Button scaleButton;
20 private Button rotateButton;
21 private Button translateButton;
22
23 @Override
24 public void onCreate(Bundle savedInstanceState) {
25 super.onCreate(savedInstanceState);
26 setContentView(R.layout.main);
27 // 找到布局文件中的控件
28 imageView = (ImageView) findViewById(R.id.iv_image);
29 alphaButton = (Button) findViewById(R.id.alpha);
30 scaleButton = (Button) findViewById(R.id.scale);
31 rotateButton = (Button) findViewById(R.id.rotate);
32 translateButton = (Button) findViewById(R.id.translate);
33 // 为各个按钮设置监听
34 alphaButton.setOnClickListener(this);
35 scaleButton.setOnClickListener(this);
36 rotateButton.setOnClickListener(this);
37 translateButton.setOnClickListener(this);
38 }
39
40 @Override
41 public void onClick(View v) {
42 AnimationSet animationSet = new AnimationSet(true);
43 int id = v.getId();
44 switch (id) {
45 // 淡入淡出
46 case R.id.alpha:
47 AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
48 // (float fromAlpha, float toAlpha)中1代表全不透明,0为透明
49 alphaAnimation.setDuration(2000);// 设置持续事件
50 animationSet.addAnimation(alphaAnimation);
51 imageView.startAnimation(animationSet);
52 break;
53 case R.id.scale:
54 // 放缩
55 ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,
56 0.1f, Animation.RELATIVE_TO_SELF, 0.5f,
57 Animation.RELATIVE_TO_SELF, 0.5f);
58 // (float fromX, float toX, float fromY, float toY, int pivotXType,
59 // float pivotXValue, int pivotYType, float pivotYValue)
60 // 将X轴放缩到多少,将Y轴放缩到多少,设置X轴放缩的类型,可以是绝对,相对自身,相对父控件,设置值。设置Y轴放缩的类型,可以是绝对,相对自身,相对父控件,设置值
61 scaleAnimation.setDuration(2000);
62 animationSet.addAnimation(scaleAnimation);
63 imageView.startAnimation(animationSet);
64 break;
65 case R.id.rotate:
66 // 旋转
67 RotateAnimation rotateAnimation = new RotateAnimation(0, 180,
68 Animation.RELATIVE_TO_SELF, 0.5f,
69 Animation.RELATIVE_TO_SELF, 0.5f);
70 //从那个角度开始旋转,旋转多少度,X轴的旋转方式和值,Y轴的旋转方式和值
71 rotateAnimation.setDuration(2000);
72 animationSet.addAnimation(rotateAnimation);
73 imageView.startAnimation(animationSet);
74 break;
75 case R.id.translate:
76 // 平移
77 TranslateAnimation translateAnimation = new TranslateAnimation(
78 Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT,
79 1.0f, Animation.ABSOLUTE, 0f,
80 Animation.ABSOLUTE, 1.0f);
81 //设置X轴的平移方式和值,设置平移到的X轴的平移方式和值,Y轴的平移方式和值,设置平移到的Y轴的平移方式和值
82 translateAnimation.setDuration(2000);
83 animationSet.setFillAfter(true);
84 animationSet.setFillBefore(false);
85 animationSet.addAnimation(translateAnimation);
86 imageView.startAnimation(animationSet);
87 break;
88 default:
89 break;
90 }
91 }
92 }
93 在放缩,旋转,移动的过程中,有三种参数,Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT分别代表的是绝对位置,相对于自身的位置,相对于父控件的位置。

interpolator的解释

interpolator定义一个动画的变化率(the rate of change)。这使得基本的动画效果(alpha, scale, translate, rotate)得以加速,减速,重复等。

Interpolator 定义了动画的变化速度,可以实现匀速、正加速、负加速、无规则变加速等。Interpolator 是基类,封装了所有 Interpolator 的共同方法,它只有一个方法,即 getInterpolation (float input),该方法 maps a point on the timeline to a multiplier to be applied to the transformations of an animation。Android 提供了几个 Interpolator 子类,实现了不同的速度曲线,如下:

AccelerateDecelerateInterpolator在动画开始与介绍的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator在动画开始的地方速率改变比较慢,然后开始减速
LinearInterpolator在动画的以均匀的速率改变

Interpolator 类及其子类的类图如下所示:

Interpolator 类及其子类

 

Frame Animation

Frame Animation是顺序播放事先做好的图像,跟电影类似。不同于animation package, Android SDK提供了另外一个类AnimationDrawable来定义、使用Frame Animation。

Frame Animation可以在XML Resource定义(还是存放到res\anim文件夹下),也可以使用AnimationDrawable中的API定义。由于Tween Animation与Frame Animation有着很大的不同,因此XML定义的格式也完全不一样,其格式是:首先是animation-list根节点,animation-list根节点中包含多个item子节点,每个item节点定义一帧动画,当前帧的drawable资源和当前帧持续的时间。下面对节点的元素加以说明: 

XML属性说明
drawable当前帧引用的drawable资源
duration当前帧显示的时间(毫秒为单位)
oneshot如果为true,表示动画只播放一次停止在最后一帧上,如果设置为false表示动画循环播放。
variablePaddingIf true, allows the drawable’s padding to change based on the current state that is selected.
visible规定drawable的初始可见性,默认为flase;

下面就给个具体的XML例子,来定义一帧一帧的动画:

 1 <?xml version="1.0" encoding="utf-8"?>
2 <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
3 android:oneshot="false">
4 <item android:drawable="@drawable/emo_im_angel" android:duration="500" />
5 <item android:drawable="@drawable/emo_im_cool" android:duration="500" />
6 <item android:drawable="@drawable/emo_im_crying" android:duration="500" />
7 <item android:drawable="@drawable/emo_im_happy" android:duration="500" />
8 <item android:drawable="@drawable/emo_im_kissing" android:duration="500" />
9 <item android:drawable="@drawable/emo_im_laughing" android:duration="500" />
10 <item android:drawable="@drawable/emo_im_sad" android:duration="500" />
11 <item android:drawable="@drawable/emo_im_surprised" android:duration="500" />
12 <item android:drawable="@drawable/emo_im_winking" android:duration="500" />
13 </animation-list>

上面的XML就定义了一个Frame Animation,其包含9帧动画,9帧动画中分别应用了drawable中的9张图片,每帧动画持续500毫秒。

然后我们将以上XML保存在res/anim/文件夹下,命名为anim_nv.xml,显示动画的代码:

 1 package cn.yj3g.AnimationsAPI;
2
3 import android.app.Activity;
4 import android.graphics.drawable.AnimationDrawable;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.view.View.OnClickListener;
8 import android.widget.Button;
9 import android.widget.ImageView;
10
11 public class AnimationsAPIActivity3 extends Activity implements OnClickListener {
12 private ImageView imageView;
13 private Button moveButton;
14
15 @Override
16 public void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.framebyframe);
19 // 找到布局文件中的控件
20 imageView = (ImageView) findViewById(R.id.iv_imagemove);
21 moveButton = (Button) findViewById(R.id.move);
22 moveButton.setOnClickListener(this);
23 }
24
25 @Override
26 public void onClick(View v) {
27 //得到配置文件中的背景文件
28 imageView.setBackgroundResource(R.drawable.anim_nv);
29 AnimationDrawable animationDrawable = (AnimationDrawable) imageView
30 .getBackground();
31 animationDrawable.start();
32 }
33 }

代码运行的结果:9张图片按照顺序循环播放.

有一点需要强调的是:启动Frame Animation动画的代码animationDrawable.start();不能在OnCreate()中,因为在OnCreate()中AnimationDrawable还没有完全的与ImageView绑定,在OnCreate()中启动动画,就只能看到第一张图片。这里是在拖曳事件中实现的。

layoutAnimationController

layoutAnimationController用于为一个layout里面的控件或者是ViewGroup里面的控件设置动画效果。这里,如果为布局文件设置了动画效果,那么包含在该控件里的控件都具有相同的动画效果。这些控件的动画效果可以在不同时间里显示出来。layoutAnimationController可以在代码中实现,也可以在xml文件中实现。

layoutAnimationController在xml文件中实现:

首先我们要在res/anim目录下创建一个新文件,这里我创建的是list_anim_layout.xml文件

1 <?xml version="1.0" encoding="utf-8"?>
2 <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
3 android:delay="1"//动画效果延迟时间
4 android:animationOrder="normal" //控件的动画效果显示顺序
5 android:animation="@anim/list_anim" />//动画效果实现方式
animationOrder的顺序有三种:ORDER_NORMAL, ORDER_REVERSE or ORDER_RANDOM分别代表一般形式,相反形式,随机形式。
上面xml文件中的list_anim是定义动画实现的效果,根据需求做出相应的效果,下面是我的一个实现效果。
list_anim.xml:
 1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android"
3 android:interpolator="@android:anim/accelerate_interpolator"
4 android:shareInterpolator="true">
5 <alpha
6 android:fromAlpha="0.0"
7 android:toAlpha="1.0"
8 android:duration="2000" />
9 <scale
10 android:fromXScale="1.0"
11 android:toXScale="0.0"
12 android:fromYScale="1.0"
13 android:toYScale="0.0"
14 android:pivotX="50%"
15 android:pivotY="50%"
16 android:duration="2000"/>
17
18 </set>
然后在布局文件main.xml文件当中为ListView做出如下设置:
    android:layoutAnimation="@anim/list_anim_layout"
这样利用xml文件就实现了layoutAnimationController效果。

layoutAnimationController在java代码中的实现:

1.创建一个Animation对象(可以通过加载xml文件或者在代码中直接创建Animation对象)。

2.创建LayoutAnimationController。

3.设置控件显示顺序。

4.为ListView控件设置LayoutAnimationController属性。

posted @ 2011-10-12 01:47  jacktu  阅读(9242)  评论(0编辑  收藏  举报