Android使用Animation的实现方法总共有两种,一种为在xml中定义动画的实现方法,另一种则是直接在代码中定义动画。

Android 使用Animation的具体操作方法我们将会在这篇文章中做一个详细的介绍。大家可以通过这里举出的代码进行解读,并从中了解到相关操作技巧,方便我们将来开发应用,并且加深对这一操作系统的理解程度。
o Android使用SDK方法详解
o Android电源管理相关应用技巧分享
o Android NDK环境创建方法简介
o Android界面布局基本知识简述
o Android logcat应用指南

在Android中,分别可以在xml中定义Animation,也可以在程序代码中定义,下面的小例子是利用RotateAnimation简单展示一下两种Android使用Animation的方法,对于其他动画,如ScaleAnimation,AlphaAnimation,原理是一样的。

Android使用Animation方法一:在xml中定义动画:

Xml代码

1. < ?xml version="1.0" encoding="utf-8"?>
2. < set xmlns:android=
"http://schemas.android.com/apk/res/android">
3. < rotate
4. android:interpolator="@android:anim/accelerate_
decelerate_interpolator"
5. android:fromDegrees="0"
6. android:toDegrees="+360"
7. android:duration="3000" />
8. < !-- rotate 旋转动画效果
9. 属性:interpolator 指定一个动画的插入器,用来控制动画的速度变化
10. fromDegrees 属性为动画起始时物件的角度
11. toDegrees 属性为动画结束时物件旋转的角度,+代表顺时针
12. duration 属性为动画持续时间,以毫秒为单位
13. -->
14. < /set>
15. < ?xml version="1.0" encoding="utf-8"?>
16. < set xmlns:android=
"http://schemas.android.com/apk/res/android">
17. < rotate
18. android:interpolator=
"@android:anim/accelerate_decelerate_interpolator"
19. android:fromDegrees="0"
20. android:toDegrees="+360"
21. android:duration="3000" />
22. < !-- rotate 旋转动画效果
23. 属性:interpolator 指定一个动画的插入器,用来控制动画的速度变化
24. fromDegrees 属性为动画起始时物件的角度
25. toDegrees 属性为动画结束时物件旋转的角度,+代表顺时针
26. duration 属性为动画持续时间,以毫秒为单位
27. -->
28. < /set>

使用动画的Java代码,程序的效果是点击按钮,TextView旋转一周:

Java代码

1. package com.ray.animation;
2. import android.app.Activity;
3. import android.os.Bundle;
4. import android.view.View;
5. import android.view.View.OnClickListener;
6. import android.view.animation.Animation;
7. import android.view.animation.AnimationUtils;
8. import android.widget.Button;
9. import android.widget.TextView;
10. public class TestAnimation extends Activity
implements OnClickListener{
11. public void onCreate(Bundle savedInstanceState) {
12. super.onCreate(savedInstanceState);
13. setContentView(R.layout.main);
14. Button btn = (Button)findViewById(R.id.Button01);
15. btn.setOnClickListener(this);
16. }
17. @Override
18. public void onClick(View v) {
19. Animation anim = AnimationUtils.loadAnimation(this,
R.anim.my_rotate_action);
20. findViewById(R.id.TextView01).startAnimation(anim);
21. }
22. }
23. package com.ray.animation;
24. import android.app.Activity;
25. import android.os.Bundle;
26. import android.view.View;
27. import android.view.View.OnClickListener;
28. import android.view.animation.Animation;
29. import android.view.animation.AnimationUtils;
30. import android.widget.Button;
31. import android.widget.TextView;
32. public class TestAnimation extends Activity
implements OnClickListener{
33. public void onCreate(Bundle savedInstanceState) {
34. super.onCreate(savedInstanceState);
35. setContentView(R.layout.main);
36. Button btn = (Button)findViewById(R.id.Button01);
37. btn.setOnClickListener(this);
38. }
39. @Override
40. public void onClick(View v) {
41. Animation anim = AnimationUtils.loadAnimation(this,
R.anim.my_rotate_action);
42. findViewById(R.id.TextView01).startAnimation(anim);
43. }
44. }

Android使用Animation方法二:直接在代码中定义动画(效果跟方法一类似):

Java代码

1. package com.ray.animation;
2. import android.app.Activity;
3. import android.os.Bundle;
4. import android.view.View;
5. import android.view.View.OnClickListener;
6. import android.view.animation.AccelerateDecelerateInterpolator;
7. import android.view.animation.Animation;
8. import android.view.animation.RotateAnimation;
9. import android.widget.Button;
10. public class TestAnimation extends Activity
implements OnClickListener{
11. public void onCreate(Bundle savedInstanceState) {
12. super.onCreate(savedInstanceState);
13. setContentView(R.layout.main);
14. Button btn = (Button)findViewById(R.id.Button);
15. btn.setOnClickListener(this);
16. }
17. public void onClick(View v) {
18. Animation anim = null;
19. anim = new RotateAnimation(0.0f,+360.0f);
20. anim.setInterpolator(new AccelerateDecelerateInterpolator());
21. anim.setDuration(3000);
22. findViewById(R.id.TextView01).startAnimation(anim);
23. }
24. }

Android使用Animation相关实现方法就为大家介绍到这里

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. android上一些方法的区别和用法的注意事项
  5. Android(安卓)Activity界面切换添加动画特效
  6. android实现字体闪烁动画的方法
  7. Android中dispatchDraw分析
  8. Android四大基本组件介绍与生命周期
  9. Android(安卓)MediaPlayer 常用方法介绍

随机推荐

  1. JSP学习笔记
  2. 怎样画好人物头发?头发画法技巧教程
  3. PHP安全配置优化详解
  4. html加css样式实现js美食项目首页示例代
  5. JBuilder2005实战JSP之切换控制 图文步骤
  6. jsp读取数据库实现分页技术简析
  7. php中Swoole的热更新实现代码实例
  8. jsp 标准标签库简析
  9. php中Swoole的热更新实现代码实例
  10. HTML基础详解(下)