Animation是个抽象类,他有五个子类。分别为:AlphaAnimation, AnimationSet, RotateAnimation, ScaleAnimation, TranslateAnimation 。
1. AlphaAnimation 淡入淡出效果
2. RotateAnimation 旋转效果
3. ScaleAnimation 缩放效果
4. TranslateAnimation 移动效果
5. AnimationSet animation集合,里面可以放多个animation。
下面直接实例,相关参数说明都在代码中:
package com.kevin.animations;
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 AnimationsDemo extends Activity {
private ImageView img;
private Button btn_alpha,btn_scale,btn_traslate,btn_rotate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView) findViewById(R.id.imageView1);
btn_alpha = (Button) findViewById(R.id.btn_alpha);
btn_rotate = (Button) findViewById(R.id.btn_rotate);
btn_scale = (Button) findViewById(R.id.btn_scale);
btn_traslate = (Button) findViewById(R.id.btn_translate);
btn_alpha.setOnClickListener(new AlphaButtonListener());
btn_rotate.setOnClickListener(new RotateButtonListener());
btn_scale.setOnClickListener(new ScaleButtonListener());
btn_traslate.setOnClickListener(new TranslateButtonListener());
}
// 淡入淡出效果
class AlphaButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// 创建AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
// 创建AlphaAnimation对象
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
// 设置动画持续时间
alphaAnimation.setDuration(2000);
// 将AlphaAnimation对象添加到AnimationSet当中
animationSet.addAnimation(alphaAnimation);
// 使用ImageView的startAnimation方法开始执行动画
img.startAnimation(animationSet);
}
}
// 旋转效果
class RotateButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
/*
* fromDegrees 为0,起始旋转的角度
* toDegrees 为360,最终旋转到的角度
* pivotXType 为动画在X轴相对于物件位置类型(三种1.absolute 绝对坐标, 2. RELATIVE_TO_PARENT相对父控件 3.RELATIVE_TO_SELF相对于自身)
* pivotXValue 1f(百分比)代表整个x轴的值,0f代表X轴的原点
* pivotYType,pivotYValue与上面类似
* pivotXType,pivotXValue,pivotYType,pivotYValue四个参数确定旋转的圆心
*/
RotateAnimation rotateAnimation = new RotateAnimation(0, 320,
Animation.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_PARENT,0f);
rotateAnimation.setDuration(5000);
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
animationSet.addAnimation(rotateAnimation);
img.startAnimation(animationSet);
}
}
// 缩放效果
class ScaleButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
/*
* fromX为X大小
* toX为缩放后的X大小(>1放大,<1缩小,=1维持不变)
* fromY为Y轴大小
* toY为缩放后的Y大小
* pivotXType为动画在X轴相对于物件位置类型(三种1.absolute 绝对坐标, 2. RELATIVE_TO_PARENT相对父控件 3.RELATIVE_TO_SELF相对于自身)
* pivotXValue 1f代表整个x轴的值,0f代表X轴的原点
* pivotYType,pivotYValue与上面类似
* pivotXType,pivotXValue,pivotYType,pivotYValue四个参数确定轴心
*/
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2.0f, 1, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);
animationSet.addAnimation(scaleAnimation);
// 让img停留在最终的位置,而不恢复到初始位置
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
img.startAnimation(animationSet);
}
}
// 移动效果
class TranslateButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
/*
* 第一个参数为动画在X轴相对于物件位置类型
* fromXValue为动画起始时 X坐标上的移动位置
* toXValue为动画结束时 X坐标上的移动位置 (最终的X的位置)
* 下面的Y类似
*/
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, -1f);
translateAnimation.setDuration(5000);
animationSet.addAnimation(translateAnimation);
img.startAnimation(animationSet);
System.out.println(img.getWidth() + " " + img.getHeight());
}
}
}