AnimationDemoActivity.java
package com.tianjf;
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 AnimationDemoActivity extends Activity implements OnClickListener {
private ImageView mImageView;
private Button mAlphaButton;
private Button mScaleButton;
private Button mRotateButton;
private Button mTranslateButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImageView = (ImageView) findViewById(R.id.imageView);
mAlphaButton = (Button) findViewById(R.id.alphaButton);
mScaleButton = (Button) findViewById(R.id.scaleButton);
mRotateButton = (Button) findViewById(R.id.rotateButton);
mTranslateButton = (Button) findViewById(R.id.translateButton);
mAlphaButton.setOnClickListener(this);
mScaleButton.setOnClickListener(this);
mRotateButton.setOnClickListener(this);
mTranslateButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.alphaButton:
// 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
AnimationSet animationSet = new AnimationSet(true);
// 创建一个AlphaAnimation对象(参数表示从完全不透明到完全透明)
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
// 设置动画执行的时间(单位:毫秒)
alphaAnimation.setDuration(1000);
// 将AlphaAnimation对象添加到AnimationSet当中
animationSet.addAnimation(alphaAnimation);
// 使用ImageView的startAnimation方法开始执行动画
mImageView.startAnimation(animationSet);
break;
case R.id.scaleButton:
// 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
animationSet = new AnimationSet(true);
// 创建一个ScaleAnimation对象(以某个点为中心缩放)
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// 设置动画执行之前等待的时间(单位:毫秒)
scaleAnimation.setStartOffset(1000);
// 设置动画执行的时间(单位:毫秒)
scaleAnimation.setDuration(2000);
// 如果fillAfter设为true,则动画执行后,控件将停留在动画结束的状态
// 运行了一下发现以下奇怪的现象
// scaleAnimation.setFillAfter(true);不会停留在动画结束的状态
// animationSet.setFillAfter(true);则会停留在动画结束的状态
animationSet.setFillAfter(true);
// 将ScaleAnimation对象添加到AnimationSet当中
animationSet.addAnimation(scaleAnimation);
// 使用ImageView的startAnimation方法开始执行动画
mImageView.startAnimation(animationSet);
break;
case R.id.rotateButton:
// 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
animationSet = new AnimationSet(true);
// 创建一个RotateAnimation对象(以某个点为圆心旋转360度)
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.25f);
// 设置动画执行的时间(单位:毫秒)
rotateAnimation.setDuration(5000);
// 将RotateAnimation对象添加到AnimationSet当中
animationSet.addAnimation(rotateAnimation);
// 使用ImageView的startAnimation方法开始执行动画
mImageView.startAnimation(animationSet);
break;
case R.id.translateButton:
// 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
animationSet = new AnimationSet(true);
// 创建一个RotateAnimation对象(从某个点移动到另一个点)
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1.0f);
// 设置动画执行的时间(单位:毫秒)
translateAnimation.setDuration(1000);
// 将TranslateAnimation对象添加到AnimationSet当中
animationSet.addAnimation(translateAnimation);
// 使用ImageView的startAnimation方法开始执行动画
mImageView.startAnimation(animationSet);
break;
default:
break;
}
}
}
