原文网址:http://blog.csdn.net/huangbiao86/article/details/6683665

Animation动画效果。提供了一系列的动画效果,可以应用大多数 的控件。

一、Animations从总体上来说可以分为两大类:

1、TweenedAnimations:该类提供了旋转,移动,伸展,和淡出竺效果;

2、Frame-by-FrameAmimations:这一类可以创建一个Drawable序列:这些Drawable可以按照指定的时间间歇一个一个的显示。

二、TwenedAnimations的分类

a)        Alpha:淡入淡出效果

b)        Scale:缩放效果

c)        Rotate:旋转效果

d)        Translate:移动效果

这四种动画效果对应四个不同的类,都有不同的参数,但是这四个不同的动画效果有共同的参数:下面简单介绍下面几种:

setDuration(long durationMills)设置动画持续时间,单位毫秒 ;

setFillAfter(boolean fillAfter)如果为true的话,动画执行后,控件停留在执行结束的状态;

setFillBefore(boolean fillBefore)

setStartOffSet(long startOffSet)设置动画执行之前等待时间;

setRepeatCount(int repeatCount)设置动画重复的次数

下面简单介绍一下,TewnedAnimation的使用步骤:

1、创建一个AnimationSet,存放动画集合,也可以只有一个动画。

2、根据需要创建对应的Animation

3、根据动画的需求,为Animation创建相对应的数据。

4、将Animation对象添加到AnimationSet

5、使用控件开始执行Animation:textView.startAnimation(AnimateionSet)当然里面的参数也可以是Animate,由于Animation是AnimationSet的父类,所以不会出错

我们来做一个简单的例子,了解Animation的用法 :

1、 在main.xml文件里面添加四个按钮控件,和一个图片控件,用来进行测试,图片用于显示动画效果;

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <ImageView android:id="@+id/image"
  8. android:src="@drawable/icon"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_margin="100dip"/>
  12. <Button android:id="@+id/alpha"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:text="@string/alpha"/>
  16. <Button android:id="@+id/scale"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:text="@string/scale"/>
  20. <Button android:id="@+id/rotate"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. android:text="@string/rotate"/>
  24. <Button android:id="@+id/translate"
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. android:text="@string/translate"/>
  28. </LinearLayout>

2、编写Activity类,添加监听:并且在对应的监听事件上 加上动画效果(按上面介绍了的步骤进行)

  1. public class HelloAnimationActivity extends Activity {
  2. /** Called when the activity is first created. */
  3. Button b_alpha;
  4. Button b_scale;
  5. Button b_rotate;
  6. Button b_translate;
  7. ImageView iv;
  8. AnimationSet animationSet;
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. iv = (ImageView)findViewById(R.id.image);
  14. b_alpha = (Button)findViewById(R.id.alpha);
  15. b_scale = (Button)findViewById(R.id.scale);
  16. b_rotate = (Button)findViewById(R.id.rotate);
  17. b_translate = (Button)findViewById(R.id.translate);
  18. b_alpha.setOnClickListener(new AlpahListener());
  19. b_scale.setOnClickListener(new ScaleListener());
  20. b_rotate.setOnClickListener(new RotateListener());
  21. b_translate.setOnClickListener(new TranslateListener());
  22. }
  23. class AlpahListener implements OnClickListener{
  24. @Override
  25. public void onClick(View v) {
  26. //创建一个AnimationSet对象
  27. AnimationSet animationSet = new AnimationSet(true);
  28. //创建一个AlphaAnimation对象
  29. AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
  30. //设置动画执行的时间(单位:毫秒)
  31. alphaAnimation.setDuration(1000);
  32. //将AlphaAnimation对象添加到AnimationSet当中
  33. animationSet.addAnimation(alphaAnimation);
  34. //使用ImageView的startAnimation方法开始执行动画
  35. iv.startAnimation(animationSet);
  36. }
  37. }
  38. class ScaleListener implements OnClickListener{
  39. /**
  40. * 从大到小的动画,x轴1到0.1,y轴1到0.1。中心为图片的中心(0.5f,0.5f)
  41. */
  42. @Override
  43. public void onClick(View arg0) {
  44. AnimationSet animationSet = new AnimationSet(true);
  45. ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
  46. Animation.RELATIVE_TO_SELF, 0.5f,
  47. Animation.RELATIVE_TO_SELF, 0.5f);
  48. animationSet.addAnimation(scaleAnimation);
  49. animationSet.setStartOffset(1000);
  50. animationSet.setFillAfter(true);
  51. animationSet.setFillBefore(false);
  52. animationSet.setDuration(2000);
  53. iv.startAnimation(animationSet);
  54. }
  55. }
  56. class RotateListener implements OnClickListener{
  57. /**
  58. * 以图片中心旋转360度;(0.5f,0.5f)表示图片的中心
  59. */
  60. @Override
  61. public void onClick(View arg0) {
  62. // TODO Auto-generated method stub
  63. animationSet = new AnimationSet(true);
  64. RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
  65. Animation.RELATIVE_TO_SELF, 0.5f,
  66. Animation.RELATIVE_TO_SELF, 0.5f);
  67. rotateAnimation.setDuration(5000);
  68. animationSet.addAnimation(rotateAnimation);
  69. iv.setAnimation(rotateAnimation);
  70. }
  71. }
  72. class TranslateListener implements OnClickListener{
  73. @Override
  74. public void onClick(View arg0) {
  75. // TODO Auto-generated method stub
  76. animationSet = new AnimationSet(true);
  77. TranslateAnimation translateAnimation = new TranslateAnimation(
  78. Animation.RELATIVE_TO_SELF, 0f,
  79. Animation.RELATIVE_TO_SELF, 0.5f,
  80. Animation.RELATIVE_TO_SELF, 0f,
  81. Animation.RELATIVE_TO_SELF, 1.0f);
  82. animationSet.addAnimation(translateAnimation);
  83. iv.setAnimation(animationSet);
  84. }
  85. }
  86. }

下面来看看Animation的第二种用法 :

这种方法跟上一种差不多,只不过是将动画的布局,在xml文件里配置,配置动画;这样做的好处是一个动画配置可以重复应用,这种方法在面向对象的语言中是很注重的;

在上一例子的基础上,在res目录下新建目录anim;在下面新建xml文件,就做为Animation的布局文件。

xml文件编写方法:

1、首先加入set标签。<set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">

2、再在该标签当中加入rotate,alpha,scale,translate标签;

在代码中用AnimationUtils静态函数,加载动画xml文件。

如:

  1. <set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">
  2. <scale
  3. android:interpolator="@android:anim/accelerate_decelerate_interpolator"
  4. android:fromXScale="1.0"
  5. android:toXScale="1.4"
  6. android:fromYScale="1.0"
  7. android:toYScale="0.6"
  8. android:pivotX="50%"
  9. android:pivotY="50%"
  10. android:fillAfter="false"
  11. android:duration="1000" />
  12. <set android:interpolator="@android:anim/decelerate_interpolator">
  13. <scale
  14. android:fromXScale="1.4"
  15. android:toXScale="0.0"
  16. android:fromYScale="0.6"
  17. android:toYScale="0.0"
  18. android:pivotX="50%"
  19. android:pivotY="50%"
  20. android:startOffset="700"
  21. android:duration="400"
  22. android:fillBefore="false" />
  23. <rotate
  24. android:fromDegrees="0"
  25. android:toDegrees="50"
  26. android:toYScale="1.0"
  27. android:pivotX="50%"
  28. android:pivotY="50%"
  29. android:startOffset="700"
  30. android:duration="1400" />
  31. </set>
  32. </set>

上面在一个xml文件中定义了 一系列的动画效果,注意:

android:pivotX="50"这种方法使用绝对位置定位,相当于代码中的  Animation.ABSOLUTE

android:pivotX="50%"这种方法相对于控件本身定位,相当于代码中的 Animation.RELATIVE_TO_SELF

android:pivotX="50%p" 这种方法相对于控件 的父控件定位,相当于代码中上 Animation.RELATIVE_TO_PARENT

如果需要调用 这个动画效果到某个控件 ,可以用下面的方法,给图片添加上面的一系列的动画效果,如果只希望有一个动画效果 ,那么也是一个xml文件:

  1. class AlpahListener implements OnClickListener{
  2. @Override
  3. public void onClick(View v) {
  4. Animation animation = AnimationUtils.loadAnimation(HelloAnimationActivity.this, R.anim.animation_test);
  5. iv.startAnimation(animation);
  6. }
  7. }

这只是一段监听器代码。

另外,如果我们直接在xml文件里面,直接加两个或多个效果:如下面:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:interpolator="@android:anim/accelerate_interpolator"
  4. android:shareInterpolator="true">
  5. <alpha
  6. android:fromAlpha="1.0"
  7. android:toAlpha="0.0"
  8. android:startOffset="500"
  9. android:duration="2000" />
  10. <rotate android:fromDegrees="0"
  11. android:toDegrees="360"
  12. android:pivotX="50%"
  13. android:pivotY="50%"
  14. android:duration="2000" />
  15. </set>

这时候,如果给控件添加这个动画效果,那么这个xml文件里面的alpha和rotate会叠加,而且这两个动画效果共享@android:anim/accelerate_interpolator.都是逐渐加速;如果需要单个设置速率就需要在每个效果里添加属性

  1. android:interpolator="@android:anim/accelerate_interpolator"

Android 提供了几个 Interpolator 子类,实现了不同的速度曲线,如下:

AccelerateDecelerateInterpolator 在动画开始与介绍的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始减速
LinearInterpolator 在动画的以均匀的速率改变

Frame-by-FrameAmimations这一类可以创建一个Drawable序列:这些Drawable可以按照指定的时间间歇一个一个的显示,就像win7桌面背景图片切换一样,循环切换;

使用方法:定义资源文件xml:

  1. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:oneshot="true">
  3. <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
  4. <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
  5. <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
  6. </animation-list>

其中item是轮流切换的图片;

在代码中使用资源文件:

  1. AnimationDrawable rocketAnimation;
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.main);
  5. ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
  6. rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
  7. rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
  8. }
  9. public boolean onTouchEvent(MotionEvent event) {
  10. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  11. rocketAnimation.start();
  12. return true;
  13. }
  14. return super.onTouchEvent(event);
  15. }

如果我们要给每一个动画添加监听器:如下面代码 :

  1. public class MainActivity extends Activity {
  2. /** Called when the activity is first created. */
  3. private Button removeButton = null;
  4. private Button addButton = null;
  5. private ImageView imageView = null;
  6. private ViewGroup viewGroup = null;
  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.main);
  11. removeButton = (Button)findViewById(R.id.removeButtonId);
  12. imageView = (ImageView)findViewById(R.id.imageViewId);
  13. removeButton.setOnClickListener(new RemoveButtonListener());
  14. viewGroup = (ViewGroup)findViewById(R.id.layoutId);//这里viewGroup是LinearLayout的id,这个viewGroup包括了下面所有的控件。
  15. addButton = (Button)findViewById(R.id.addButtonId);
  16. addButton.setOnClickListener(new AddButtonListener());
  17. }
  18. private class AddButtonListener implements OnClickListener{
  19. @Override
  20. public void onClick(View v) {
  21. //创建了一个淡入效果的Animation对象
  22. AlphaAnimation animation = new AlphaAnimation(0.0f,1.0f);
  23. animation.setDuration(1000);
  24. animation.setStartOffset(500);
  25. //创建一个新的ImageView
  26. ImageView imageViewAdd = new ImageView(MainActivity.this);
  27. imageViewAdd.setImageResource(R.drawable.icon);
  28. //将新的ImageView添加到viewGroup当中
  29. viewGroup.addView(imageViewAdd, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  30. //启动动画
  31. imageViewAdd.startAnimation(animation);
  32. }
  33. }
  34. private class RemoveButtonListener implements OnClickListener{
  35. @Override
  36. public void onClick(View v) {
  37. //创建一个淡出效果的Animation对象
  38. AlphaAnimation animation = new AlphaAnimation(1.0f,0.0f);
  39. //为Animation对象设置属性
  40. animation.setDuration(1000);
  41. animation.setStartOffset(500);
  42. //为Animation对象设置监听器
  43. animation.setAnimationListener(new RemoveAnimationListener());
  44. imageView.startAnimation(animation);
  45. }
  46. }
  47. private class RemoveAnimationListener implements AnimationListener{
  48. //该方法在淡出效果执行结束之后被调用
  49. @Override
  50. public void onAnimationEnd(Animation animation) {
  51. System.out.println("end");
  52. //从viewGroup当中删除掉imageView控件
  53. viewGroup.removeView(imageView);
  54. }
  55. //该方法在动画重复执行的时候调用
  56. @Override
  57. public void onAnimationRepeat(Animation animation) {
  58. System.out.println("repeat");
  59. }
  60. //该方法在动画开始执行的时候,调用 。
  61. @Override
  62. public void onAnimationStart(Animation animation) {
  63. System.out.println("start");
  64. }
  65. }
  66. }

在实际应用中,后者可能会多一些,但是由于写xml的时候,编译器很多地方不会报错,所以维护也是一个问题;

【转】Android:Animation的简单学习--不错的更多相关文章

  1. Android greenDAO 数据库 简单学习之基本使用

    看网上对greenDAO介绍的不错,今天就动手来试一把,看看好不好使. greenDAO 官方网站:http://greendao-orm.com/ 代码托管地址:https://github.com ...

  2. Android Studio IDE 简单学习和介绍

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  3. 38.Android之ListView简单学习(一)

    android中ListView用的很普遍,今天来学习下,本篇主要以本地数据加载到listview,后面会学习从网络获取数据添加到listview. 首先改下布局文件: <?xml versio ...

  4. Android Animation学习(六) View Animation介绍

    Android Animation学习(六) View Animation介绍 View Animation View animation系统可以用来执行View上的Tween animation和F ...

  5. Android Animation学习(五) ApiDemos解析:容器布局动画 LayoutTransition

    Android Animation学习(五) ApiDemos解析:容器布局动画 LayoutTransition Property animation系统还提供了对ViewGroup中的View改变 ...

  6. Android动画学习笔记-Android Animation

    Android动画学习笔记-Android Animation   3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中 ...

  7. Android Animation学习 实现 IOS 滤镜退出动画

    IOS的用户体验做的很好,其中一点很重要的地方就是动画效果. 最近在学习Android的Animation,简单实现了一个IOS相机滤镜退出的动画: 布局文件:activity_animation_d ...

  8. Android Animation学习(四) ApiDemos解析:多属性动画

    Android Animation学习(四) ApiDemos解析:多属性动画 如果想同时改变多个属性,根据前面所学的,比较显而易见的一种思路是构造多个对象Animator , ( Animator可 ...

  9. Android Animation学习(三) ApiDemos解析:XML动画文件的使用

    Android Animation学习(三) ApiDemos解析:XML动画文件的使用 可以用XML文件来定义Animation. 文件必须有一个唯一的根节点: <set>, <o ...

随机推荐

  1. 重新设置MySQL的root密码

    1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以任意地登录 ...

  2. 【技术·水】浅谈Dism++清理插件开发

    前言 昨天我发布了NCleaner,一款Dism++清理插件(地址:http://bbs.pcbeta.com/viewthread-1692182-1-1.html) 有些人想要我开源NCleane ...

  3. Name control

    static: (Page 406) In both C and C++ the keyword static has two basic meanings, which unfortunately ...

  4. linux管理文件系统指令

    就一个基本的linux系统而言,其计算机硬盘只能有三个分区:一个交换分区(用于处理物理内存存不下的信息),一个包含引导转载程序的内核的启动分区,一个根文件系统分区,后两个常采用 ext3文件系统 与e ...

  5. x264_param_t结构

    typedef struct x264_param_t { unsigned int cpu; // CPU 标志位 int i_threads; // 并行编码多帧; 线程数,为0则自动多线程编码 ...

  6. tq --uboot使用

    - Nand Flash命令- nand info nand erase nand read[.jffs2] addr off size              .jffs代表ECC方式不同 nan ...

  7. 【转】oracle PLSQL常用方法汇总

    原文:http://www.cnblogs.com/luluping/archive/2010/03/10/1682885.html 在SQLPLUS下,实现中-英字符集转换alter session ...

  8. session阻塞机制,解决方法

    session从生成到读取,或从生成到写入都出现锁定的情况. 1.session_start();session_commit(); 2.session_start();session_write_c ...

  9. java 各种排序算法

    各种排序算法的分析及java实现   排序一直以来都是让我很头疼的事,以前上<数据结构>打酱油去了,整个学期下来才勉强能写出个冒泡排序.由于下半年要准备工作了,也知道排序算法的重要性(据说 ...

  10. SQLSERVER与C#中数据类型的对应关系

    SQLSERVER与C#中数据类型的对应关系 ///<summary> ///数据库中与C#中的数据类型对照 ///</summary> ///<paramname=&q ...