一。什么是Animations
Animations提供了一系列的动画效果。这些效果可以运用到绝大多数的控件中。
二。Animations的分类
1.Twenn动画
提供了旋转(Rotate),移动(Translate),缩放(Scale),透明度(Alpha)等效果。
TweenAnimations的几个通用属性:
setDuration(long DurationMills)设置动画持续时间
setFillAfter(boolean fillAfter)如果为true,动画执行完毕后,将停留在动画执行结束的地方。
setFillBefore(boolean fillBefore)如果为true,动画执行完毕后,将停留在动画开始的地方。
setStartOffSet(long StartOffSet):设置动画执行前等待的时间。
setRepeatCount(in repeatCount):设置动画重复播放的次数。
2.Frame动画
Drawable序列,一张张显示,犹如电影效果。
三。Animations的两种使用方法
1.在java代码中实现:
实现Tween动画的步骤是:
1)创建一个AnimationSet对象
2)根据需要创建Animation对象
3)为Animation设置相应的数据
4)将Animation对象添加到AnimationSet对象中去
5)使用空间对象操作Animation
接下来看一个简单的Demo来在代java代码中实现Tween动画。
package com.android.douf;
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 MainActivity extends Activity {
private Button rotateButton,translateButton,alphaButton,scaleButton;
private ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.image);
rotateButton = (Button) findViewById(R.id.RotateButton);
rotateButton.setOnClickListener(new RotateButtonListener());
translateButton = (Button) findViewById(R.id.TranslateButton);
translateButton.setOnClickListener(new TranslateButtonListener());
alphaButton = (Button) findViewById(R.id.AlphaButton);
alphaButton.setOnClickListener(new AlphaButtonListener());
scaleButton = (Button) findViewById(R.id.ScaleButton);
scaleButton.setOnClickListener(new ScaleButtonListener());
}
//旋转动画
public class RotateButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
//从角度0到360度,相对于父控件的(0.5,0)的点旋转
Animation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 0.5f,
Animation.RELATIVE_TO_PARENT, 0f);
rotateAnimation.setDuration(2000);
animationSet.addAnimation(rotateAnimation);
image.startAnimation(animationSet);
}
}
//移动动画
public class TranslateButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
//相对于自身,X轴从0到1,Y轴0到0
Animation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f);
translateAnimation.setDuration(2000);
animationSet.addAnimation(translateAnimation);
image.startAnimation(animationSet);
}
}
//透明度动画
public class AlphaButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
//透明度从1到0.1
Animation alphaAnimation = new AlphaAnimation(1f, 0.1f);
alphaAnimation.setDuration(2000);
animationSet.addAnimation(alphaAnimation);
image.startAnimation(animationSet);
}
}
//缩放动画
public class ScaleButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
AnimationSet animationSet = n