我是靠谱客的博主 兴奋可乐,这篇文章主要介绍Android动画之TranslateAnimation用法案例详解,现在分享给大家,希望可以做个参考。

我们在实际的开发过程中,有很多地方需要使用TranslateAnimation,本文是爱站技术频道小编为大家做的简单介绍,下面是详解Android 动画之TranslateAnimation应用的参数说明,希望对你学习这方面知识有帮助!

android中提供了4中动画:

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

本节讲解TranslateAnimation动画,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:定义动画重复的时间

代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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(); } }); } }

效果:

到此这篇关于Android动画之TranslateAnimation用法案例详解的文章就介绍到这了,更多相关Android动画之TranslateAnimation用法内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是兴奋可乐最近收集整理的关于Android动画之TranslateAnimation用法案例详解的全部内容,更多相关Android动画之TranslateAnimation用法案例详解内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(124)

评论列表共有 0 条评论

立即
投稿
返回
顶部