您的位置:首页 > 其它

TranslateAnimation 位移动画效果

2015-08-05 20:16 351 查看
TranslateAnimation比较常用,比如QQ,网易新闻菜单条的动画,就可以用TranslateAnimation实现,

通过TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 来定义动画

参数说明:

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();
}
});
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: