作者:李灏
一、什么是Animations
Animations提供了一系列的动画效果,这些效果可以应用在绝大多数控件上。
二、Animations的分类
总体上可以分为两大类。
1.tweened Animations
该类Animations提供了旋转,移动,伸展和淡入淡出等等效果。
2.Frame-by-Frame Animations
这一类的Animations可以创建一个Drawable序列,这个Drawable可以按照时间间歇一个一个的显示。
三、tweened Animations
分类:
1.alpha:淡入淡出效果
2.scale:缩放效果
3.Rotate:旋转效果
4.Translate:移动效果
步骤:
1.创建AnimationSet对象;
可以把多个Animation放到一个AnimationSet当中。
2.根据需要创建相应的Animation对象;
3.根据软件动画的需求,为Animation对象设置相应的数据;
4.将Animation对象添加到AnimationSet对象当中;
5.使用控件对象开始执行AnimationSet。
四、源代码
package com.animation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class AnimationsActivity extends Activity {
/** Called when the activity is first created. */
private ImageView imageView = null;
private Button rotateButton = null;
private Button transButton = null;
private Button alphaButton = null;
private Button scaleButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) this.findViewById(R.id.imageView1);
rotateButton = (Button) this.findViewById(R.id.rotateButton);
transButton = (Button) this.findViewById(R.id.transButton);
scaleButton = (Button) this.findViewById(R.id.scaleButton);
alphaButton = (Button) this.findViewById(R.id.alphaButton);
rotateButton.setOnClickListener(new RotateListener());
transButton.setOnClickListener(new TransListener());
scaleButton.setOnClickListener(new ScaleListener());
alphaButton.setOnClickListener(new AlphaListener());
}
class RotateListener implements OnClickListener {
public void onClick(View arg0) {
AnimationSet animationSet = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(0, 720,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(rotateAnimation);
}
}
class TransListener implements OnClickListener {
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation transAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f
);
transAnimation.setDuration(1000);
animationSet.addAnimation(transAnimation);
imageView.startAnimation(animationSet);
}
}
class AlphaListener implements OnClickListener {
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);
imageView.startAnimation(animationSet);
}
}
class ScaleListener implements OnClickListener {
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_PARENT, 0.5f,
Animation.RELATIVE_TO_PARENT, 0.5f);
scaleAnimation.setDuration(2000);
animationSet.addAnimation(scaleAnimation);
imageView.startAnimation(animationSet);
}
}
}
下面对 AlphaListener中的实现过程来做一个介绍:
步骤一、创建AnimationSet对象
AnimationSetanimationSet = new AnimationSet(true);
步骤二、根据需要创建相应的Animation对象
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
步骤三、根据软件动画的需求,为Animation对象设置相应的数据
alphaAnimation.setDuration(1000);
步骤四、将Animation对象添加到AnimationSet对象当中
animationSet.addAnimation(alphaAnimation);
步骤五、使用控件对象开始执行AnimationSet。
imageView.startAnimation(animationSet);
五、Animations的通用属性
setDuration(long);
设置动画的持续时间
setFillAfter(bool);
如果为true,则动画运行完之后停留在执行结束时的位置。
setFillBefore(bool);
如果为true,则动画运行完之后会停留在执行之前的位置。
setStartOffSet(long);
设置动画执行之前的等待时间。
setRepeatCount(int);
设置动画重复执行的次数。
如果在AnimationSet里面设置这些属性后,所有的Animation都会有上述属性。
六、总结
以上只是用代码来控制android下的控件来实现动画效果,也可以通过实现xml文件来实现动态效果。.Frame-by-Frame Animations会在以后的博客中介绍。