Android_Animation

Animation从总体来说可以分为两类:

  1. Tweened Animations:该类提供了旋转,移动,伸展,淡入淡出等效果
  2. Frame-By-Frame Animations:该类可以创建一个Drawable序列,这些Drawable可以按照指定的事件间隔一个一个显示,和动画片差不多


Tweened Animations

Tweened Animations也有四种类型:

  1. Alpha:淡入淡出效果
  2. Scale:缩放效果
  3. Rotate:旋转效果
  4. Translate:移动效果

设置动画效果可以在XML文件中设置,也可以在Java代码中设置。我们先来讲解在Java代码中怎么设置这四种动画效果

Java代码中设置动画效果的步骤:

  1. 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
  2. 根据需要创建相应的Animation对象
  3. 根据需要对Animation对象的各个属性进行设值
  4. 将Animation对象添加到AnimationSet对象中
  5. 使用控件的startAnimation()方法执行AnimationSet

Java代码中的通用属性:

  • setDuration(long durationMillis):设置动画持续事件(单位:毫秒)
  • setFillAfter(boolean fillAfter):如果fillAfter设为true,则动画执行后,控件将停留在动画结束的状态
  • setFillBefore(boolean fillBefore):如果fillBefore设为true,则动画执行后,控件将回到动画开始的状态
  • setStartOffset(long startOffset):设置动画执行之前等待的时间(单位:毫秒)
  • setRepeatCount(int repeatCount):设置动画重复的次数
  • setInterpolator(Interpolator i):设置动画的变化速度

setInterpolator(new AccelerateDecelerateInterpolator()):先加速,后减速

setInterpolator(new AccelerateInterpolator()):加速

setInterpolator(new DecelerateInterpolator()):减速

setInterpolator(new CycleInterpolator()):动画循环播放特定次数,速率改变沿着正弦曲线

setInterpolator(new LinearInterpolator()):匀速

以及其他一些特定的动画效果

Java代码中设置动画效果的Demo(AnimationDemo01):

main.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/imageView"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginTop="100dip"  
  12.         android:src="@drawable/ic_launcher" />  
  13.   
  14.     <LinearLayout  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="fill_parent"  
  17.         android:gravity="bottom"  
  18.         android:orientation="vertical" >  
  19.   
  20.         <Button  
  21.             android:id="@+id/alphaButton"  
  22.             android:layout_width="fill_parent"  
  23.             android:layout_height="wrap_content"  
  24.             android:text="测试alpha动画效果" />  
  25.   
  26.         <Button  
  27.             android:id="@+id/scaleButton"  
  28.             android:layout_width="fill_parent"  
  29.             android:layout_height="wrap_content"  
  30.             android:text="测试scale动画效果" />  
  31.   
  32.         <Button  
  33.             android:id="@+id/rotateButton"  
  34.             android:layout_width="fill_parent"  
  35.             android:layout_height="wrap_content"  
  36.             android:text="测试rotate动画效果" />  
  37.   
  38.         <Button  
  39.             android:id="@+id/translateButton"  
  40.             android:layout_width="fill_parent"  
  41.             android:layout_height="wrap_content"  
  42.             android:text="测试translate动画效果" />  
  43.     </LinearLayout>  
  44.   
  45. </LinearLayout>  
AnimationDemoActivity.java
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.tianjf;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.view.animation.AlphaAnimation;  
  8. import android.view.animation.Animation;  
  9. import android.view.animation.AnimationSet;  
  10. import android.view.animation.RotateAnimation;  
  11. import android.view.animation.ScaleAnimation;  
  12. import android.view.animation.TranslateAnimation;  
  13. import android.widget.Button;  
  14. import android.widget.ImageView;  
  15.   
  16. public class AnimationDemoActivity extends Activity implements OnClickListener {  
  17.   
  18.     private ImageView mImageView;  
  19.     private Button mAlphaButton;  
  20.     private Button mScaleButton;  
  21.     private Button mRotateButton;  
  22.     private Button mTranslateButton;  
  23.   
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.   
  29.         mImageView = (ImageView) findViewById(R.id.imageView);  
  30.         mAlphaButton = (Button) findViewById(R.id.alphaButton);  
  31.         mScaleButton = (Button) findViewById(R.id.scaleButton);  
  32.         mRotateButton = (Button) findViewById(R.id.rotateButton);  
  33.         mTranslateButton = (Button) findViewById(R.id.translateButton);  
  34.   
  35.         mAlphaButton.setOnClickListener(this);  
  36.         mScaleButton.setOnClickListener(this);  
  37.         mRotateButton.setOnClickListener(this);  
  38.         mTranslateButton.setOnClickListener(this);  
  39.     }  
  40.   
  41.     @Override  
  42.     public void onClick(View v) {  
  43.         switch (v.getId()) {  
  44.         case R.id.alphaButton:  
  45.             // 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)  
  46.             AnimationSet animationSet = new AnimationSet(true);  
  47.             // 创建一个AlphaAnimation对象(参数表示从完全不透明到完全透明)  
  48.             AlphaAnimation alphaAnimation = new AlphaAnimation(10);  
  49.             // 设置动画执行的时间(单位:毫秒)  
  50.             alphaAnimation.setDuration(1000);  
  51.             // 将AlphaAnimation对象添加到AnimationSet当中  
  52.             animationSet.addAnimation(alphaAnimation);  
  53.             // 使用ImageView的startAnimation方法开始执行动画  
  54.             mImageView.startAnimation(animationSet);  
  55.             break;  
  56.   
  57.         case R.id.scaleButton:  
  58.             // 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)  
  59.             animationSet = new AnimationSet(true);  
  60.             // 创建一个ScaleAnimation对象(以某个点为中心缩放)  
  61.             ScaleAnimation scaleAnimation = new ScaleAnimation(10.1f, 10.1f,  
  62.                     Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  
  63.             // 设置动画执行之前等待的时间(单位:毫秒)  
  64.             scaleAnimation.setStartOffset(1000);  
  65.             // 设置动画执行的时间(单位:毫秒)  
  66.             scaleAnimation.setDuration(2000);  
  67.             // 如果fillAfter设为true,则动画执行后,控件将停留在动画结束的状态  
  68.             // 运行了一下发现以下奇怪的现象  
  69.             // scaleAnimation.setFillAfter(true);不会停留在动画结束的状态  
  70.             // animationSet.setFillAfter(true);则会停留在动画结束的状态  
  71.             animationSet.setFillAfter(true);  
  72.                         // 将ScaleAnimation对象添加到AnimationSet当中  
  73.                         animationSet.addAnimation(scaleAnimation);  
  74.                         // 使用ImageView的startAnimation方法开始执行动画  
  75.                         mImageView.startAnimation(animationSet);  
  76.             break;  
  77.   
  78.         case R.id.rotateButton:  
  79.             // 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)  
  80.             animationSet = new AnimationSet(true);  
  81.             // 创建一个RotateAnimation对象(以某个点为圆心旋转360度)  
  82.             RotateAnimation rotateAnimation = new RotateAnimation(0360,  
  83.                     Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.25f);  
  84.             // 设置动画执行的时间(单位:毫秒)  
  85.             rotateAnimation.setDuration(5000);  
  86.             // 将RotateAnimation对象添加到AnimationSet当中  
  87.             animationSet.addAnimation(rotateAnimation);  
  88.             // 使用ImageView的startAnimation方法开始执行动画  
  89.             mImageView.startAnimation(animationSet);  
  90.             break;  
  91.   
  92.         case R.id.translateButton:  
  93.             // 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)  
  94.             animationSet = new AnimationSet(true);  
  95.             // 创建一个RotateAnimation对象(从某个点移动到另一个点)  
  96.             TranslateAnimation translateAnimation = new TranslateAnimation(  
  97.                     Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f,  
  98.                     Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1.0f);  
  99.             // 设置动画执行的时间(单位:毫秒)  
  100.             translateAnimation.setDuration(1000);  
  101.             // 将TranslateAnimation对象添加到AnimationSet当中  
  102.             animationSet.addAnimation(translateAnimation);  
  103.             // 使用ImageView的startAnimation方法开始执行动画  
  104.             mImageView.startAnimation(animationSet);  
  105.             break;  
  106.   
  107.         default:  
  108.             break;  
  109.         }  
  110.     }  
  111. }  
AlphaAnimation很简单,不多说,直接看代码注释

剩下的3中动画效果都涉及到了参考点的问题,下面我们逐一解释

RotateAnimation的创建中用到了Animation.RELATIVE_TO_PARENT(表示以父控件为参考),另外还有2种参考:Animation.RELATIVE_TO_SELF(以自身为参考),Animation.ABSOLUTE(绝对坐标,没有参考)

我们就拿Animation.RELATIVE_TO_PARENT来画图讲解到底RotateAnimation旋转的圆心在哪儿?

例子中,X轴设置了相对于父控件,值是0.5f;Y轴设置了相对于父控件,值是0.25f。也就是说,圆心是0.5个父控件的width和0.25个父控件的height,如下图

那么,控件就围绕着上图的圆心,从0°旋转到360°(如果是0,-360,那么就是0°旋转到-360°)

以父控件为参照理解了的话,以自身为参考就可以举一反三了。

ScaleAnimation的创建的参数的解释:缩放的中心点可以按照上文中的计算方法(如下图),缩小后的图片大小是0.1个自身width,0.1个自身height,相当于缩小90%

TranslateAnimation的创建的参数的解释:参数主要是决定移动的起始点和终了点,计算方法参考上文(如下图)

上面的Java代码设置动画效果有一个缺点:代码的可复用性很低,不利于维护。为了维护,可以使用XML来设置动画效果

XML中设置动画效果的步骤:

  1. 在res文件夹下新建一个名为anim的文件夹
  2. 创建xml文件,并首先加入set标签(set标签就相当于Java代码中的AnimationSet)
  3. 在Set标签中加入alpha,scale,rotate,translate标签(相当于Java代码中的AlphaAnimation,ScaleAnimation,RotateAnimation,TranslateAnimation)
  4. 在Java代码中使用AnimationUtils的loadAnimation方法来加载XML文件,并得到一个Animation对象
  5. 使用控件的startAnimation()方法执行这个Animation对象

XML中的通用属性:

  • android:duration:设置动画持续事件(单位:毫秒)
  • android:fillAfter:如果fillAfter设为true,则动画执行后,控件将停留在动画结束的状态
  • android:fillBefore:如果fillBefore设为true,则动画执行后,控件将回到动画开始的状态
  • android:startOffset(long startOffset):设置动画执行之前等待的时间(单位:毫秒)
  • android:repeatCount(int repeatCount):设置动画重复的次数
  • android:interpolator:设置动画的变化速度

android:interpolator="@android:anim/accelerate_decelerate_interpolator":先加速,后减速

android:interpolator="@android:anim/accelerate_interpolator":加速

android:interpolator="@android:anim/decelerate_interpolator":减速

android:interpolator="@android:anim/cycle_Interpolator":动画循环播放特定次数,速率改变沿着正弦曲线

android:interpolator="@android:anim/linear_Interpolator":匀速

以及其他一些特定的动画效果

XML中设置动画效果的Demo(AnimationDemo02):

main.xml

同上

alpha.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.   
  5.     <alpha  
  6.         android:duration="500"  
  7.         android:fromAlpha="1.0"  
  8.         android:startOffset="500"  
  9.         android:toAlpha="0.0" />  
  10.   
  11. </set>  
rotate.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.   
  5.     <rotate  
  6.         android:duration="5000"  
  7.         android:fromDegrees="0"  
  8.         android:pivotX="50%"  
  9.         android:pivotY="50%"  
  10.         android:toDegrees="360" />  
  11.   
  12. </set>  
scale.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.   
  5.     <scale  
  6.         android:duration="2000"  
  7.         android:fromXScale="1.0"  
  8.         android:fromYScale="1.0"  
  9.         android:pivotX="50%"  
  10.         android:pivotY="50%"  
  11.         android:toXScale="0.0"  
  12.         android:toYScale="0.0" />  
  13.   
  14. </set>  
translate.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.   
  5.     <translate  
  6.         android:duration="2000"  
  7.         android:fromXDelta="50%"  
  8.         android:fromYDelta="0%"  
  9.         android:toXDelta="100%"  
  10.         android:toYDelta="100%" />  
  11.   
  12. </set>  
AnimationDemoActivity.java
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.tianjf;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.view.animation.Animation;  
  8. import android.view.animation.AnimationUtils;  
  9. import android.widget.Button;  
  10. import android.widget.ImageView;  
  11.   
  12. public class AnimationDemoActivity extends Activity implements OnClickListener {  
  13.   
  14.     private ImageView mImageView;  
  15.     private Button mAlphaButton;  
  16.     private Button mScaleButton;  
  17.     private Button mRotateButton;  
  18.     private Button mTranslateButton;  
  19.   
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.   
  25.         mImageView = (ImageView) findViewById(R.id.imageView);  
  26.         mAlphaButton = (Button) findViewById(R.id.alphaButton);  
  27.         mScaleButton = (Button) findViewById(R.id.scaleButton);  
  28.         mRotateButton = (Button) findViewById(R.id.rotateButton);  
  29.         mTranslateButton = (Button) findViewById(R.id.translateButton);  
  30.   
  31.         mAlphaButton.setOnClickListener(this);  
  32.         mScaleButton.setOnClickListener(this);  
  33.         mRotateButton.setOnClickListener(this);  
  34.         mTranslateButton.setOnClickListener(this);  
  35.     }  
  36.   
  37.     @Override  
  38.     public void onClick(View v) {  
  39.         switch (v.getId()) {  
  40.         case R.id.alphaButton:  
  41.             Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);  
  42.             mImageView.startAnimation(alphaAnimation);  
  43.             break;  
  44.   
  45.         case R.id.scaleButton:  
  46.             Animation scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scale);  
  47.             mImageView.startAnimation(scaleAnimation);  
  48.             break;  
  49.   
  50.         case R.id.rotateButton:  
  51.             Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate);  
  52.             mImageView.startAnimation(rotateAnimation);  
  53.             break;  
  54.   
  55.         case R.id.translateButton:  
  56.             Animation translateAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);  
  57.             mImageView.startAnimation(translateAnimation);  
  58.             break;  
  59.   
  60.         default:  
  61.             break;  
  62.         }  
  63.     }  
  64. }  
关于属性的讲解,已经在AnimationDemo01中讲解过了,在此就不再重复了。

值得一提的是,AnimationDemo01中出现的绝对定位(Animation.ABSOLUTE),相对于自身定位(Animation.RELATIVE_TO_SELF),相对于父控件定位(Animation.RELATIVE_TO_PAREN),在XML文件中用XX(绝对),XX%(相对于自身),XX%p(相对于父控件)

上面两个例子都有一个类叫:AnimationSet(XML文件中是set标签),这个AnimationSet就相当于一个集合,可以存放一个或多个Animation对象,如果我们对AnimationSet设置属性值,那么,这些属性值将应用到AnimationSet下的所有Animation对象中去。


Frame-By-Frame Animations

直接上代码(AnimationDemo03)

main.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/imageView"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginTop="100dip" />  
  12.   
  13.     <LinearLayout  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="fill_parent"  
  16.         android:gravity="bottom"  
  17.         android:orientation="vertical" >  
  18.   
  19.         <Button  
  20.             android:id="@+id/frameByFrameButton"  
  21.             android:layout_width="fill_parent"  
  22.             android:layout_height="wrap_content"  
  23.             android:text="测试Frame-By-Frame动画效果" />  
  24.     </LinearLayout>  
  25.   
  26. </LinearLayout>  
anim_nv.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:oneshot="false" >  
  4.   
  5.     <item  
  6.         android:drawable="@drawable/nv1"  
  7.         android:duration="500"/>  
  8.     <item  
  9.         android:drawable="@drawable/nv2"  
  10.         android:duration="500"/>  
  11.     <item  
  12.         android:drawable="@drawable/nv3"  
  13.         android:duration="500"/>  
  14.     <item  
  15.         android:drawable="@drawable/nv4"  
  16.         android:duration="500"/>  
  17.   
  18. </animation-list>  
AnimationDemoActivity.java
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.tianjf;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.drawable.AnimationDrawable;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.ImageView;  
  10.   
  11. public class AnimationDemoActivity extends Activity implements OnClickListener {  
  12.   
  13.     private ImageView mImageView;  
  14.     private Button mFrameByFrameButton;  
  15.   
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.   
  21.         mImageView = (ImageView) findViewById(R.id.imageView);  
  22.         mFrameByFrameButton = (Button) findViewById(R.id.frameByFrameButton);  
  23.   
  24.         mFrameByFrameButton.setOnClickListener(this);  
  25.     }  
  26.   
  27.     @Override  
  28.     public void onClick(View v) {  
  29.         switch (v.getId()) {  
  30.         case R.id.frameByFrameButton:  
  31.             // 为ImageView设置背景资源  
  32.             mImageView.setBackgroundResource(R.drawable.anim_nv);  
  33.             // 通过ImageView得到AnimationDrawable  
  34.             AnimationDrawable animationDrawable = (AnimationDrawable) mImageView.getBackground();  
  35.             // 开始执行动画  
  36.             animationDrawable.start();  
  37.             break;  
  38.   
  39.         default:  
  40.             break;  
  41.         }  
  42.     }  
  43. }  


LayoutAnimationController

什么是LayoutAnimationController呢?LayoutAnimationController用于为一个Layout里面的一组控件设置相同的动画效果,并可以控制这一组动画在不同的时间出现

LayoutAnimationController可以在XML中设置,也可在Java代码中设置

先看一个在XML中设置的Demo(AnimationDemo04)

list_anim.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.   
  6.     <alpha  
  7.         android:duration="2000"  
  8.         android:fromAlpha="0.0"  
  9.         android:toAlpha="1.0" />  
  10.   
  11. </set>  
list_anim_layout.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:delay="1"  
  4.     android:animationOrder="normal"   
  5.     android:animation="@anim/list_anim" />  
main.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ListView  
  8.         android:id="@id/android:list"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layoutAnimation="@anim/list_anim_layout"  
  12.         android:scrollbars="vertical"  
  13.         android:visibility="gone" />  
  14.   
  15.     <LinearLayout  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="fill_parent"  
  18.         android:gravity="bottom"  
  19.         android:orientation="vertical" >  
  20.   
  21.         <Button  
  22.             android:id="@+id/button"  
  23.             android:layout_width="fill_parent"  
  24.             android:layout_height="wrap_content"  
  25.             android:text="测试LayoutAnimation动画效果" />  
  26.     </LinearLayout>  
  27.   
  28. </LinearLayout>  
item.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal"  
  6.     android:paddingBottom="1dip"  
  7.     android:paddingLeft="10dip"  
  8.     android:paddingRight="10dip"  
  9.     android:paddingTop="1dip" >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/user_name"  
  13.         android:layout_width="180dip"  
  14.         android:layout_height="30dip"  
  15.         android:singleLine="true"  
  16.         android:textSize="20dip" />  
  17.   
  18.     <TextView  
  19.         android:id="@+id/user_gender"  
  20.         android:layout_width="fill_parent"  
  21.         android:layout_height="fill_parent"  
  22.         android:singleLine="true"  
  23.         android:textSize="20dip" />  
  24.   
  25. </LinearLayout>  
AnimationDemoActivity.java
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.tianjf;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6.   
  7. import android.app.ListActivity;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12. import android.widget.ListAdapter;  
  13. import android.widget.ListView;  
  14. import android.widget.SimpleAdapter;  
  15.   
  16. public class AnimationDemoActivity extends ListActivity implements OnClickListener {  
  17.   
  18.     private ListView mListView;  
  19.     private Button mButton;  
  20.   
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.   
  26.         mListView = getListView();  
  27.         mButton = (Button) findViewById(R.id.button);  
  28.   
  29.         mListView.setAdapter(buildListAdapter());  
  30.         mButton.setOnClickListener(this);  
  31.     }  
  32.   
  33.     @Override  
  34.     public void onClick(View v) {  
  35.         switch (v.getId()) {  
  36.         case R.id.button:  
  37.             mListView.setVisibility(View.VISIBLE);  
  38.             break;  
  39.   
  40.         default:  
  41.             break;  
  42.         }  
  43.     }  
  44.   
  45.     private ListAdapter buildListAdapter() {  
  46.         List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();  
  47.         HashMap<String, String> m1 = new HashMap<String, String>();  
  48.         m1.put("user_name""张三");  
  49.         m1.put("user_gender""女");  
  50.   
  51.         HashMap<String, String> m2 = new HashMap<String, String>();  
  52.         m2.put("user_name""李四");  
  53.         m2.put("user_gender""女");  
  54.   
  55.         HashMap<String, String> m3 = new HashMap<String, String>();  
  56.         m3.put("user_name""王五");  
  57.         m3.put("user_gender""男");  
  58.   
  59.         list.add(m1);  
  60.         list.add(m2);  
  61.         list.add(m3);  
  62.   
  63.         SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.item, new String[] {  
  64.                 "user_name""user_gender" }, new int[] { R.id.user_name, R.id.user_gender });  
  65.         return simpleAdapter;  
  66.     }  
  67. }  
layoutAnimation标签中的android:animationOrder属性就是设置动画的显示顺序的,normal就是依次显示出来

如果要在Java代码中设置LayoutAnimation,可以按照如下设置

删掉list_anim_layout.xml文件,删掉main.xml下的ListView的android:layoutAnimation属性,最后,在OnClick方法里面加上如下代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Animation animation = (Animation) AnimationUtils.loadAnimation(this, R.anim.list_anim);  
  2. LayoutAnimationController lac = new LayoutAnimationController(animation);  
  3. lac.setOrder(LayoutAnimationController.ORDER_NORMAL);  
  4. lac.setDelay(0.5f);  
  5. mListView.setLayoutAnimation(lac);  


AnimationListener

还可以为Animation对象添加AnimationListener监听器。监听动画执行的各阶段。

AnimationListener主要包含三个方法:

  1. onAnimationEnd:动画结束
  2. onAnimationStart:动画开始
  3. onAnimationRepet:动画重复
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值