Android 动画之TranslateAnimation应用详解

本节讲解TranslateAnimation动画,TranslateAnimation比较常用,比如QQ,网易新闻菜单条的动画,就可以用TranslateAnimation实现,本文将详细介绍通过TranslateAnimation 来定义动画,需要的朋友可以参考下

android中提供了4中动画: 

AlphaAnimation 透明度动画效果 
ScaleAnimation 缩放动画效果 
TranslateAnimation 位移动画效果 
RotateAnimation 旋转动画效果 

参数说明: 

float fromXDelta 动画开始的点离当前View X坐标上的差值 
float toXDelta 动画结束的点离当前View X坐标上的差值 
float fromYDelta 动画开始的点离当前View Y坐标上的差值 
float toYDelta 动画开始的点离当前View Y坐标上的差值 

常用方法: 

animation.setDuration(long durationMillis);//设置动画持续时间 
animation.setRepeatCount(int i);//设置重复次数 
animation.setRepeatMode(Animation.REVERSE);//设置反方向执行 

Xml属性: 

android:duration:运行动画的时间 
android:repeatCount:定义动画重复的时间 

代码: 

public class MainActivity extends Activity { 
ImageView image; 
Button start; 
Button cancel; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
image = (ImageView) findViewById(R.id.main_img); 
start = (Button) findViewById(R.id.main_start); 
cancel = (Button) findViewById(R.id.main_cancel); 
/** 设置位移动画 向右位移150 */ 
final TranslateAnimation animation = new TranslateAnimation(0, 150,0, 0); 
animation.setDuration(2000);//设置动画持续时间 
animation.setRepeatCount(2);//设置重复次数 
animation.setRepeatMode(Animation.REVERSE);//设置反方向执行 
start.setOnClickListener(new OnClickListener() { 
public void onClick(View arg0) { 
image.setAnimation(animation); 
/** 开始动画 */ 
animation.startNow(); 
} 
}); 
cancel.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
/** 结束动画 */ 
animation.cancel(); 
} 
}); 
} 
} 

 

连接如下:

AlphaAnimation 透明度动画效果 
ScaleAnimation 缩放动画效果 
TranslateAnimation 位移动画效果 
RotateAnimation 旋转动画效果 

posted @ 2015-04-30 16:01  Stuart001  阅读(715)  评论(0编辑  收藏  举报