Animation
一、Animation
在Activity中设置动画效果:
package org.demo.animations1;
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 Animations1Activity extends Activity {
private ImageView img=null;
private Button alphaBtn=null;
private Button scaleBtn=null;
private Button rotateBtn=null;
private Button translateBtn=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img=(ImageView)findViewById(R.id.img);
alphaBtn=(Button)findViewById(R.id.alpha);
alphaBtn.setOnClickListener(new AlphaClick());
scaleBtn=(Button)findViewById(R.id.scale);
scaleBtn.setOnClickListener(new scaleClick());
rotateBtn=(Button)findViewById(R.id.rotate);
rotateBtn.setOnClickListener(new rotateClick());
translateBtn=(Button)findViewById(R.id.translate);
translateBtn.setOnClickListener(new translateClick());
}
class AlphaClick implements OnClickListener{
public void onClick(View v) {
//缩放效果
//建立一个AnimationSet,用来存放动画效果,true表示所有Animation共享AnimationSet设置的Interpolator
//如果为false,则需要为每个动画设置Interpolator
AnimationSet set=new AnimationSet(true);
//建立一个alphaanimation
AlphaAnimation alpha=new AlphaAnimation(1f, 0.1f);
//设置动画持续时间
set.setDuration(5000);
//设置动画是否停留在执行动画后
set.setFillAfter(true);
//设置动画是否恢复到执行动画前
set.setFillBefore(false);
//将alpha添加到set集合中
set.addAnimation(alpha);
img.startAnimation(set);
}
}
class scaleClick implements OnClickListener{
public void onClick(View v) {
//缩放效果
//建立一个AnimationSet,用来存放动画效果
AnimationSet set=new AnimationSet(true);
//建立一个scaleanimation
ScaleAnimation scale=new ScaleAnimation(0f, 0.5f, 0, 0.5f);
//设置动画持续时间
set.setDuration(5000);
//设置动画是否停留在执行动画后
set.setFillAfter(true);
//设置动画是否恢复到执行动画前
set.setFillBefore(false);
//将alpha添加到set集合中
set.addAnimation(scale);
img.startAnimation(set);
}
}
class rotateClick implements OnClickListener{
public void onClick(View v) {
//旋转
//建立一个AnimationSet,用来存放动画效果
AnimationSet set=new AnimationSet(true);
//建立一个rotateanimation
RotateAnimation rotate=new RotateAnimation(10f,50f);
//设置动画持续时间
set.setDuration(5000);
set.addAnimation(rotate);
img.startAnimation(set);
}
}
class translateClick implements OnClickListener{
public void onClick(View v) {
//旋转
//建立一个AnimationSet,用来存放动画效果
AnimationSet set=new AnimationSet(true);
//建立一个translateanimation
TranslateAnimation translate=new TranslateAnimation(0f,0f,1f,20f);
//设置动画持续时间
set.setDuration(5000);
set.addAnimation(translate);
img.startAnimation(set);
}
}
}