Android 动画之TranslateAnimation应用详解

时间:2023-06-27 06:17:59 

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:定义动画重复的时间


代码:


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
0
投稿

猜你喜欢

  • 浅析Java多线程同步synchronized

    2023-05-20 15:52:29
  • 如何在Unity中检测死循环和卡死

    2023-12-18 00:55:17
  • C#中事务处理和非事务处理方法实例分析

    2023-12-23 08:09:13
  • C#中的多线程多参数传递详解

    2023-08-02 22:23:46
  • C#表达式和运算符详细解析

    2021-09-30 23:39:09
  • C#实现收发邮件功能

    2021-09-20 19:40:46
  • Spring Utils工具类常用方法实例

    2023-05-01 05:37:37
  • Android图文居中显示控件使用方法详解

    2023-04-04 14:07:24
  • Zookeeper和Eureka哪个更好?

    2023-11-10 02:57:35
  • Android中实现根据资源名获取资源ID

    2023-06-20 04:18:30
  • Springboot 扫描mapper接口的2种操作

    2022-08-04 03:28:54
  • 源码阅读之storm操作zookeeper-cluster.clj

    2022-06-01 13:21:48
  • Android权限询问的实例详解

    2022-10-03 21:28:51
  • Java中Steam流的用法详解

    2021-12-16 14:18:50
  • Android中双击返回键退出应用实例代码

    2022-02-20 00:28:44
  • Java解码H264格式视频流中的图片

    2023-11-24 23:58:24
  • C# 使用反射来实现对象的深度复制方法

    2021-10-27 19:50:01
  • Java程序测试上传Maven工程代码示例解析

    2022-01-21 18:43:25
  • JAVA如何按字节截取字符串

    2023-11-25 13:31:41
  • springboot 使用poi进行数据的导出过程详解

    2022-12-01 07:23:31
  • asp之家 软件编程 m.aspxhome.com