Android中Animation详解

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


一、Tweened Animations


Tweened Animations也有四种类型:


Alpha:淡入淡出效果
Scale:缩放效果
Rotate:旋转效果
Translate:移动效果


设置动画效果可以在XML文件中设置,也可以在Java代码中设置。我们先来讲解在Java代码中怎么设置这四种动画效果
Java代码中设置动画效果的步骤:
  创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
  根据需要创建相应的Animation对象
  根据需要对Animation对象的各个属性进行设值
  将Animation对象添加到AnimationSet对象中
  使用控件的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):

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

 

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中设置动画效果的步骤:

在res文件夹下新建一个名为anim的文件夹

创建xml文件,并首先加入set标签(set标签就相当于Java代码中的AnimationSet)

在Set标签中加入alpha,scale,rotate,translate标签(相当于Java代码中的AlphaAnimation,ScaleAnimation,RotateAnimation,TranslateAnimation)

在Java代码中使用AnimationUtils的loadAnimation方法来加载XML文件,并得到一个Animation对象

使用控件的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):

 1 main.xml
 2 
 3  <?xml version="1.0" encoding="utf-8"?> 
 4  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 5      android:layout_width="fill_parent" 
 6      android:layout_height="fill_parent" 
 7      android:orientation="vertical" > 
 8   
 9      <ImageView 
10          android:id="@+id/imageView" 
11          android:layout_width="wrap_content" 
12          android:layout_height="wrap_content" 
13          android:layout_marginTop="100dip" 
14          android:src="@drawable/ic_launcher" /> 
15   
16      <LinearLayout 
17          android:layout_width="fill_parent" 
18          android:layout_height="fill_parent" 
19          android:gravity="bottom" 
20          android:orientation="vertical" > 
21   
22          <Button 
23              android:id="@+id/alphaButton" 
24              android:layout_width="fill_parent" 
25              android:layout_height="wrap_content" 
26              android:text="测试alpha动画效果" /> 
27   
28          <Button 
29              android:id="@+id/scaleButton" 
30              android:layout_width="fill_parent" 
31              android:layout_height="wrap_content" 
32              android:text="测试scale动画效果" /> 
33   
34          <Button 
35              android:id="@+id/rotateButton" 
36              android:layout_width="fill_parent" 
37              android:layout_height="wrap_content" 
38              android:text="测试rotate动画效果" /> 
39   
40          <Button 
41              android:id="@+id/translateButton" 
42              android:layout_width="fill_parent" 
43              android:layout_height="wrap_content" 
44              android:text="测试translate动画效果" /> 
45      </LinearLayout> 
46   
47  </LinearLayout> 
main.xml(同上)
 1 alpha.xml
 2 
 3  <?xml version="1.0" encoding="utf-8"?> 
 4  <set xmlns:android="http://schemas.android.com/apk/res/android" 
 5      android:interpolator="@android:anim/accelerate_interpolator" > 
 6   
 7      <alpha 
 8          android:duration="500" 
 9          android:fromAlpha="1.0" 
10          android:startOffset="500" 
11          android:toAlpha="0.0" /> 
12   
13  </set> 
alpha.xml
 1 rotate.xml
 2 
 3  <?xml version="1.0" encoding="utf-8"?> 
 4  <set xmlns:android="http://schemas.android.com/apk/res/android" 
 5      android:interpolator="@android:anim/accelerate_interpolator" > 
 6   
 7      <rotate 
 8          android:duration="5000" 
 9          android:fromDegrees="0" 
10          android:pivotX="50%" 
11          android:pivotY="50%" 
12          android:toDegrees="360" /> 
13   
14  </set> 
rotate.xml
 1 scale.xml
 2 
 3  <?xml version="1.0" encoding="utf-8"?> 
 4  <set xmlns:android="http://schemas.android.com/apk/res/android" 
 5      android:interpolator="@android:anim/accelerate_interpolator" > 
 6   
 7      <scale 
 8          android:duration="2000" 
 9          android:fromXScale="1.0" 
10          android:fromYScale="1.0" 
11          android:pivotX="50%" 
12          android:pivotY="50%" 
13          android:toXScale="0.0" 
14          android:toYScale="0.0" /> 
15   
16  </set> 
scale.xml
 1 translate.xml
 2 
 3  <?xml version="1.0" encoding="utf-8"?> 
 4  <set xmlns:android="http://schemas.android.com/apk/res/android" 
 5      android:interpolator="@android:anim/accelerate_interpolator" > 
 6   
 7      <translate 
 8          android:duration="2000" 
 9          android:fromXDelta="50%" 
10          android:fromYDelta="0%" 
11          android:toXDelta="100%" 
12          android:toYDelta="100%" /> 
13   
14  </set> 
translate.xml

 

 1 AnimationDemoActivity.java
 2 
 3  package com.tianjf; 
 4   
 5  import android.app.Activity; 
 6  import android.os.Bundle; 
 7  import android.view.View; 
 8  import android.view.View.OnClickListener; 
 9  import android.view.animation.Animation; 
10  import android.view.animation.AnimationUtils; 
11  import android.widget.Button; 
12  import android.widget.ImageView; 
13   
14  public class AnimationDemoActivity extends Activity implements OnClickListener { 
15   
16      private ImageView mImageView; 
17      private Button mAlphaButton; 
18      private Button mScaleButton; 
19      private Button mRotateButton; 
20      private Button mTranslateButton; 
21   
22      @Override 
23      public void onCreate(Bundle savedInstanceState) { 
24          super.onCreate(savedInstanceState); 
25          setContentView(R.layout.main); 
26   
27          mImageView = (ImageView) findViewById(R.id.imageView); 
28          mAlphaButton = (Button) findViewById(R.id.alphaButton); 
29          mScaleButton = (Button) findViewById(R.id.scaleButton); 
30          mRotateButton = (Button) findViewById(R.id.rotateButton); 
31          mTranslateButton = (Button) findViewById(R.id.translateButton); 
32   
33          mAlphaButton.setOnClickListener(this); 
34          mScaleButton.setOnClickListener(this); 
35          mRotateButton.setOnClickListener(this); 
36          mTranslateButton.setOnClickListener(this); 
37      } 
38   
39      @Override 
40      public void onClick(View v) { 
41          switch (v.getId()) { 
42          case R.id.alphaButton: 
43              Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha); 
44              mImageView.startAnimation(alphaAnimation); 
45              break; 
46   
47          case R.id.scaleButton: 
48              Animation scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scale); 
49              mImageView.startAnimation(scaleAnimation); 
50              break; 
51   
52          case R.id.rotateButton: 
53              Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate); 
54              mImageView.startAnimation(rotateAnimation); 
55              break; 
56   
57          case R.id.translateButton: 
58              Animation translateAnimation = AnimationUtils.loadAnimation(this, R.anim.translate); 
59              mImageView.startAnimation(translateAnimation); 
60              break; 
61   
62          default: 
63              break; 
64          } 
65      } 
66  } 
AnimationDemoActivity.java

 

关于属性的讲解,已经在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)

 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> 

main.xml

 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> 
 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  } 

anim_nv.xml

AnimationDemoActivity.java

三、LayoutAnimationController


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

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


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

 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.xml

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" /> 
 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> 

 

list_anim_layout.xml

 

main.xml

 1 item.xml
 2 
 3  <?xml version="1.0" encoding="utf-8"?> 
 4  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 5      android:layout_width="fill_parent" 
 6      android:layout_height="fill_parent" 
 7      android:orientation="horizontal" 
 8      android:paddingBottom="1dip" 
 9      android:paddingLeft="10dip" 
10      android:paddingRight="10dip" 
11      android:paddingTop="1dip" > 
12   
13      <TextView 
14          android:id="@+id/user_name" 
15          android:layout_width="180dip" 
16          android:layout_height="30dip" 
17          android:singleLine="true" 
18          android:textSize="20dip" /> 
19   
20      <TextView 
21          android:id="@+id/user_gender" 
22          android:layout_width="fill_parent" 
23          android:layout_height="fill_parent" 
24          android:singleLine="true" 
25          android:textSize="20dip" /> 
26   
27  </LinearLayout> 

item.xml

 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  } 

AnimationDemoActivity.java

layoutAnimation标签中的android:animationOrder属性就是设置动画的显示顺序的,normal就是依次显示出来
如果要在Java代码中设置LayoutAnimation,可以按照如下设置
删掉list_anim_layout.xml文件,删掉main.xml下的ListView的android:layoutAnimation属性,最后,在OnClick方法里面加上如下代码:

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主要包含三个方法:

  onAnimationEnd:动画结束

  onAnimationStart:动画开始

  onAnimationRepet:动画重复

 

原文地址:https://www.cnblogs.com/laxlerbo/p/3835503.html