Android Animation 动画解析

来源:互联网 发布:java linktag.class 编辑:程序博客网 时间:2024/05/22 23:56

Android Animation 动画解析

http://blog.csdn.net/wr132/article/details/48830745?ref=myread
Animations

一、Animations介绍

Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转、缩放、淡入淡出等,这些效果可以应用在绝大多数的控件中。 

二、Animations的分类

Animations从总体上可以分为两大类:

1.TweenedAnimations:该类Animations提供了旋转、移动、伸展和淡出等效果。Alpha——淡入淡出,Scale——缩放效果,Rotate——旋转,Translate——移动效果。

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

 

三、Animations的使用方法(代码中使用)

Animations extends Object implementsCloneable 

 使用TweenedAnimations的步骤:

1.创建一个AnimationSet对象(Animation子类);

2.增加需要创建相应的Animation对象;

3.更加项目的需求,为Animation对象设置相应的数据;

4.将Animatin对象添加到AnimationSet对象当中;

5.使用控件对象开始执行AnimationSet。


  Tweened Animations的分类
  1、Alpha:淡入淡出效果
  2、Scale:缩放效果
  3、Rotate:旋转效果
  4、Translate:移动效果

 

Animation的四个子类:
  AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation

四、具体实现

1、main.xml

[cpp] view plaincopy
  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.     <LinearLayout  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="horizontal" >  
  11.          <Button  
  12.             android:id="@+id/rotateButton"  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:text="旋转" />  
  16.          <Button  
  17.             android:id="@+id/scaleButton"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:text="缩放" />  
  21.          <Button  
  22.             android:id="@+id/alphaButton"  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:text="淡入淡出" />  
  26.          <Button  
  27.             android:id="@+id/translateButton"  
  28.             android:layout_width="wrap_content"  
  29.             android:layout_height="wrap_content"  
  30.             android:text="移动" />  
  31.     </LinearLayout>  
  32.      <LinearLayout  
  33.         android:layout_width="fill_parent"  
  34.         android:layout_height="fill_parent"  
  35.         android:orientation="vertical" >  
  36.          <ImageView  
  37.             android:id="@+id/image"  
  38.             android:layout_width="wrap_content"  
  39.             android:layout_height="wrap_content"  
  40.             android:layout_centerInParent="true"  
  41.             android:src="@drawable/an" />  
  42.     </LinearLayout>  
  43.  </LinearLayout>  

2、.java文件

[cpp] view plaincopy
  1. importandroid.app.Activity;  
  2. importandroid.os.Bundle;  
  3. importandroid.view.View;  
  4. importandroid.view.View.OnClickListener;  
  5. import android.view.animation.AlphaAnimation;  
  6. import android.view.animation.Animation;  
  7. importandroid.view.animation.AnimationSet;  
  8. importandroid.view.animation.RotateAnimation;  
  9. importandroid.view.animation.ScaleAnimation;  
  10. import android.view.animation.TranslateAnimation;  
  11. importandroid.widget.Button;  
  12. importandroid.widget.ImageView;  
  13. public class Animation1Activity extends Activity {  
  14.     private Button rotateButton = null;  
  15.     private Button scaleButton = null;  
  16.     private Button alphaButton = null;  
  17.     private Button translateButton = null;  
  18.     private ImageView image = null;  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.          
  24.         rotateButton = (Button)findViewById(R.id.rotateButton);  
  25.         scaleButton = (Button)findViewById(R.id.scaleButton);  
  26.         alphaButton = (Button)findViewById(R.id.alphaButton);  
  27.         translateButton = (Button)findViewById(R.id.translateButton);  
  28.         image = (ImageView)findViewById(R.id.image);  
  29.        
  30.         rotateButton.setOnClickListener(newRotateButtonListener());  
  31.         scaleButton.setOnClickListener(newScaleButtonListener());  
  32.         alphaButton.setOnClickListener(newAlphaButtonListener());  
  33.         translateButton.setOnClickListener(  
  34.            new TranslateButtonListener());  
  35.     }  
  36.     class AlphaButtonListener implementsOnClickListener{  
  37.        public void onClick(View v) {  
  38.            //创建一个AnimationSet对象,参数为Boolean型,  
  39.            //true表示使用Animation的interpolator,false则是使用自己的  
  40.            AnimationSet animationSet = new AnimationSet(true);  
  41.            //创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明  
  42.            AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);  
  43.            //设置动画执行的时间  
  44.            alphaAnimation.setDuration(500);  
  45.            //将alphaAnimation对象添加到AnimationSet当中  
  46.            animationSet.addAnimation(alphaAnimation);  
  47.            //使用ImageView的startAnimation方法执行动画  
  48.            image.startAnimation(animationSet);  
  49.        }  
  50.     }  
  51.     class RotateButtonListener implementsOnClickListener{  
  52.        public void onClick(View v) {  
  53.            AnimationSet animationSet = new AnimationSet(true);  
  54.            //参数1:从哪个旋转角度开始  
  55.            //参数2:转到什么角度  
  56.            //后4个参数用于设置围绕着旋转的圆的圆心在哪里  
  57.            //参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标  
  58.            //参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴  
  59.            //参数5:确定y轴坐标的类型  
  60.            //参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴  
  61.            RotateAnimation rotateAnimation = new RotateAnimation(0, 360,  
  62.                   Animation.RELATIVE_TO_SELF,0.5f,  
  63.                   Animation.RELATIVE_TO_SELF,0.5f);  
  64.            rotateAnimation.setDuration(1000);  
  65.            animationSet.addAnimation(rotateAnimation);  
  66.            image.startAnimation(animationSet);  
  67.        }  
  68.     }  
  69.     class ScaleButtonListener implementsOnClickListener{  
  70.        public void onClick(View v) {  
  71.            AnimationSet animationSet = new AnimationSet(true);  
  72.            //参数1:x轴的初始值  
  73.            //参数2:x轴收缩后的值  
  74.            //参数3:y轴的初始值  
  75.            //参数4:y轴收缩后的值  
  76.            //参数5:确定x轴坐标的类型  
  77.            //参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴  
  78.            //参数7:确定y轴坐标的类型  
  79.            //参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴  
  80.            ScaleAnimation scaleAnimation = new ScaleAnimation(  
  81.                   0, 0.1f,0,0.1f,  
  82.                   Animation.RELATIVE_TO_SELF,0.5f,  
  83.                   Animation.RELATIVE_TO_SELF,0.5f);  
  84.            scaleAnimation.setDuration(1000);  
  85.            animationSet.addAnimation(scaleAnimation);  
  86.            image.startAnimation(animationSet);  
  87.        }  
  88.     }  
  89.     class TranslateButtonListener implementsOnClickListener{  
  90.        public void onClick(View v) {  
  91.            AnimationSet animationSet = new AnimationSet(true);  
  92.            //参数1~2:x轴的开始位置  
  93.            //参数3~4:y轴的开始位置  
  94.            //参数5~6:x轴的结束位置  
  95.            //参数7~8:x轴的结束位置  
  96.            TranslateAnimation translateAnimation =  
  97.               new TranslateAnimation(  
  98.                   Animation.RELATIVE_TO_SELF,0f,  
  99.                   Animation.RELATIVE_TO_SELF,0.5f,  
  100.                   Animation.RELATIVE_TO_SELF,0f,  
  101.                   Animation.RELATIVE_TO_SELF,0.5f);  
  102.            translateAnimation.setDuration(1000);  
  103.            animationSet.addAnimation(translateAnimation);  
  104.            image.startAnimation(animationSet);  
  105.        }  
  106.     }  
  107. }  

Tween Animations的通用方法

  1、setDuration(longdurationMills)
  设置动画持续时间(单位:毫秒)
  2、setFillAfter(Boolean fillAfter)
  如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
  3、setFillBefore(Boolean fillBefore)
  如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态
  4、setStartOffSet(long startOffSet)
  设置动画执行之前的等待时间
  5、setRepeatCount(int repeatCount)
  设置动画重复执行的次数

 

在代码中使用Animations可以很方便的调试、运行,但是代码的可重用性差,重复代码多。同样可以在xml文件中配置Animations,这样做可维护性变高了,只不过不容易进行调试。

一、在xml中使用Animations步骤

       1.在res文件夹下建立一个anim文件夹;

       2.创建xml文件,并首先加入set标签,更改标签如下:

[cpp] view plaincopy
  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. </set>  

3.在该标签当中加入rotate,alpha,scale或者translate标签;

[cpp] view plaincopy
  1. <alpha  
  2.         android:fromAlpha="1.0"  
  3.         android:toAlpha="0.0"  
  4.         android:startOffset="500"  
  5.         android:duration="500"/>  

4.在代码当中使用AnimationUtils当中装载xml文件,并生成Animation对象。因为Animation是AnimationSet的子类,所以向上转型,用Animation对象接收。

[cpp] view plaincopy
  1. Animation animation = AnimationUtils.loadAnimation(  
  2.                   Animation1Activity.this, R.anim.alpha);  
  3.            // 启动动画  
  4.            image.startAnimation(animation);  

二、具体实现

 1、  alpha.xml

[cpp] view plaincopy
  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.     <!-- fromAlpha和toAlpha是起始透明度和结束时透明度 -->  
  5.     <alpha  
  6.         android:fromAlpha="1.0"  
  7.         android:toAlpha="0.0"  
  8.         android:startOffset="500"  
  9.         android:duration="500"/>  
  10. </set>  

2、  rotate.xml

[cpp] view plaincopy
  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.         fromDegrees:开始的角度  
  6.         toDegrees:结束的角度,+表示是正的  
  7.         pivotX:用于设置旋转时的x轴坐标  
  8.         例  
  9.            1)当值为"50",表示使用绝对位置定位  
  10.            2)当值为"50%",表示使用相对于控件本身定位  
  11.            3)当值为"50%p",表示使用相对于控件的父控件定位  
  12.         pivotY:用于设置旋转时的y轴坐标  
  13.       -->  
  14.     <rotate  
  15.         android:fromDegrees="0"  
  16.         android:toDegrees="+360"  
  17.         android:pivotX="50%"  
  18.         android:pivotY="50%"  
  19.         android:duration="1000"/>  
  20. </set>  

3、  scale.xml

[cpp] view plaincopy
  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.        起始x轴坐标  
  6.            止x轴坐标  
  7.            始y轴坐标  
  8.            止y轴坐标  
  9.            轴的坐标  
  10.            轴的坐标  
  11.      -->  
  12.    <scale  
  13.        android:fromXScale="1.0"  
  14.        android:toXScale="0.0"  
  15.        android:fromYScale="1.0"  
  16.        android:toYScale="0.0"  
  17.        android:pivotX="50%"  
  18.        android:pivotY="50%"  
  19.        android:duration="1000"/>  
  20. </set>  

4、  translate.xml

[cpp] view plaincopy
  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.            始x轴坐标  
  6.            止x轴坐标  
  7.            始y轴坐标  
  8.            止y轴坐标  
  9.       -->  
  10.     <translate  
  11.         android:fromXDelta="0%"  
  12.         android:toXDelta="100%"  
  13.         android:fromYDelta="0%"  
  14.         android:toYDelta="100%"  
  15.         android:duration="2000"/>  
  16. </set>  

5、  .java文件

[cpp] view plaincopy
  1. importandroid.app.Activity;  
  2. importandroid.os.Bundle;  
  3. importandroid.view.View;  
  4. importandroid.view.View.OnClickListener;  
  5. import android.view.animation.Animation;  
  6. importandroid.view.animation.AnimationUtils;  
  7. import android.widget.Button;  
  8. import android.widget.ImageView;  
  9.    
  10. public class Animation1Activity extends Activity {  
  11.     private Button rotateButton = null;  
  12.     private Button scaleButton = null;  
  13.     private Button alphaButton = null;  
  14.     private Button translateButton = null;  
  15.     private ImageView image = null;  
  16.    
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.        super.onCreate(savedInstanceState);  
  20.        setContentView(R.layout.main);  
  21.    
  22.        rotateButton = (Button) findViewById(R.id.rotateButton);  
  23.        scaleButton = (Button) findViewById(R.id.scaleButton);  
  24.        alphaButton = (Button) findViewById(R.id.alphaButton);  
  25.        translateButton = (Button) findViewById(R.id.translateButton);  
  26.        image = (ImageView) findViewById(R.id.image);  
  27.    
  28.        rotateButton.setOnClickListener(newRotateButtonListener());  
  29.        scaleButton.setOnClickListener(newScaleButtonListener());  
  30.        alphaButton.setOnClickListener(newAlphaButtonListener());  
  31.        translateButton.setOnClickListener(newTranslateButtonListener());  
  32.     }  
  33.    
  34.     class AlphaButtonListener implementsOnClickListener {  
  35.        public void onClick(View v) {  
  36.            // 使用AnimationUtils装载动画配置文件  
  37.            Animation animation = AnimationUtils.loadAnimation(  
  38.                   Animation1Activity.this, R.anim.alpha);  
  39.            // 启动动画  
  40.            image.startAnimation(animation);  
  41.        }  
  42.     }  
  43.    
  44.     class RotateButtonListener implementsOnClickListener {  
  45.        public void onClick(View v) {  
  46.            Animation animation = AnimationUtils.loadAnimation(  
  47.                   Animation1Activity.this, R.anim.rotate);  
  48.            image.startAnimation(animation);  
  49.        }  
  50.     }  
  51.    
  52.     class ScaleButtonListener implementsOnClickListener {  
  53.        public void onClick(View v) {  
  54.            Animation animation = AnimationUtils.loadAnimation(  
  55.                   Animation1Activity.this, R.anim.scale);  
  56.            image.startAnimation(animation);  
  57.        }  
  58.     }  
  59.    
  60.     class TranslateButtonListener implementsOnClickListener {  
  61.        public void onClick(View v) {  
  62.            Animation animation = AnimationUtils.loadAnimation(Animation1Activity.this, R.anim.translate);  
  63.            image.startAnimation(animation);  
  64.        }  
  65.     }  
  66. }  

AnimationSet的具体使用方法

 

       1.AnimationSet是Animation的子类;

       2.一个AnimationSet包含了一系列的Animation;

       3.针对AnimationSet设置一些Animation的常见属性(如startOffset,duration等),可以被包含在AnimationSet当中的Animation集成;

例:一个AnimationSet中有两个Animation,效果叠加

第一种方法:

doubleani.xml

 

[cpp] view plaincopy
  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.     <!-- fromAlpha和toAlpha是起始透明度和结束时透明度 -->  
  6.     <alpha  
  7.         android:fromAlpha="1.0"  
  8.         android:toAlpha="0.0"  
  9.         android:startOffset="500"  
  10.         android:duration="500"/>  
  11.     <translate  
  12.         android:fromXDelta="0%"  
  13.         android:toXDelta="100%"  
  14.         android:fromYDelta="0%"  
  15.         android:toYDelta="100%"  
  16.         android:duration="2000"/>  
  17. </set>  

.java文件中

[cpp] view plaincopy
  1. classDoubleButtonListener implements OnClickListener {  
  2.        public void onClick(View v) {  
  3.            // 使用AnimationUtils装载动画配置文件  
  4.            Animation animation = AnimationUtils.loadAnimation(  
  5.                   Animation2Activity.this, R.anim. doubleani);  
  6.            // 启动动画  
  7.            image.startAnimation(animation);  
  8.        }  
  9.     }  

第二种方法:

.java文件中


[cpp] view plaincopy
  1. classDoubleButtonListener implements OnClickListener {  
  2.        public void onClick(View v) {  
  3.            AnimationSet animationSet = new AnimationSet(true);  
  4.            AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);  
  5.            RotateAnimation rotateAnimation = new RotateAnimation(0, 360,  
  6.                   Animation.RELATIVE_TO_SELF,0.5f,  
  7.                   Animation.RELATIVE_TO_SELF,0.5f);  
  8.            rotateAnimation.setDuration(1000);  
  9.            animationSet.addAnimation(rotateAnimation);  
  10.            animationSet.addAnimation(alphaAnimation);  
  11.            image.startAnimation(animationSet);  
  12.    
  13.        }  
  14.     }  

Interpolator的具体使用方法

 

       Interpolator定义了动画变化的速率,在Animations框架当中定义了一下几种Interpolator

        AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候速率快。

        AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速

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

        DecelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速

        LinearInterpolator:动画以均匀的速率改变

分为以下几种情况:

1、在set标签中

[cpp] view plaincopy
  1. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:interpolator="@android:anim/accelerate_interpolator"/>  

2、如果在一个set标签中包含多个动画效果,如果想让这些动画效果共享一个Interpolator。

[cpp] view plaincopy
  1. android:shareInterpolator="true"  

3、如果不想共享一个interpolator,则设置android:shareInterpolator="true"并且需要在每一个动画效果处添加interpolator。

[cpp] view plaincopy
  1. <alpha  
  2.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  3.         android:fromAlpha="1.0"  
  4.         android:toAlpha="0.0"  
  5.         android:startOffset="500"  
  6.         android:duration="500"/>  

4、如果是在代码上设置共享一个interpolator,则可以在AnimationSet设置interpolator。

[cpp] view plaincopy
  1. AnimationSet animationSet = newAnimationSet(true);  
  2. animationSet.setInterpolator(new AccelerateInterpolator());  

5、如果不设置共享一个interpolator则可以在每一个Animation对象上面设置interpolator。

[cpp] view plaincopy
  1. AnimationSet animationSet = newAnimationSet(false);  
  2. alphaAnimation.setInterpolator(new AccelerateInterpolator());  
  3. rotateAnimation.setInterpolator(new DecelerateInterpolator());  

Frame-By-FrameAnimations的使用方法

 

       Frame-By-FrameAnimations是一帧一帧的格式显示动画效果。类似于电影胶片拍摄的手法。

 main.xml

[cpp] view plaincopy
  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.     <LinearLayout  
  7.         android:orientation="horizontal"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_width="wrap_content">  
  10.        <Button  
  11.            android:id="@+id/button"  
  12.                android:layout_width="wrap_content"  
  13.                android:layout_height="wrap_content"  
  14.                android:text="运动"/>  
  15.     </LinearLayout>  
  16.     <LinearLayout  
  17.         android:orientation="vertical"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="fill_parent">  
  20.        <ImageView  
  21.            android:id="@+id/image"  
  22.             android:layout_width="wrap_content"  
  23.             android:layout_height="wrap_content"  
  24.             android:layout_centerInParent="true"/>  
  25.     </LinearLayout>  
  26. </LinearLayout>  

3、anim.xml


[cpp] view plaincopy
  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.     <item android:drawable="@drawable/a_01" android:duration="50"/>  
  5.     <item android:drawable="@drawable/a_02" android:duration="50"/>  
  6.     <item android:drawable="@drawable/a_03" android:duration="50"/>  
  7.     <item android:drawable="@drawable/a_04" android:duration="50"/>  
  8.     <item android:drawable="@drawable/a_05" android:duration="50"/>  
  9.     <item android:drawable="@drawable/a_06" android:duration="50"/>  
  10. </animation-list>  

4、.java文件

[cpp] view plaincopy
  1. importandroid.app.Activity;  
  2. importandroid.graphics.drawable.AnimationDrawable;  
  3. importandroid.os.Bundle;  
  4. importandroid.view.View;  
  5. importandroid.view.View.OnClickListener;  
  6. importandroid.widget.Button;  
  7. importandroid.widget.ImageView;  
  8. public class AnimationsActivity extends Activity {  
  9.     private Button button = null;  
  10.     private ImageView imageView = null;  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);  
  15.         button = (Button)findViewById(R.id.button);  
  16.         imageView = (ImageView)findViewById(R.id.image);  
  17.         button.setOnClickListener(newButtonListener());  
  18.     }  
  19.     class ButtonListener implementsOnClickListener{  
  20.        public void onClick(View v) {  
  21.            imageView.setBackgroundResource(R.anim.anim);  
  22.            AnimationDrawable animationDrawable = (AnimationDrawable)  
  23.               imageView.getBackground();  
  24.            animationDrawable.start();  
  25.        }  
  26.     }  
  27. }  

  LayoutAnimationsController

1、什么是LayoutAnimationsController

LayoutAnimationsController可以用于实现使多个控件按顺序一个一个的显示。

1)LayoutAnimationsController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置统一的动画效果。

2)每一个控件都有相同的动画效果

3)控件的动画效果可以在不同的时间显示出来

4)LayoutAnimationsController可以在xml文件当中设置,以可以在代码当中进行设置。

2、在xml当中使用LayoutAnimationController

1)在res/anim文件夹下创建一个名为list_anim_layout.xml文件:

android:delay- 动画间隔时间;子类动画时间间隔 (延迟)  70% 也可以是一个浮点数 如“1.2”等

android:animationOrder- 动画执行的循序(normal:顺序,random:随机,reverse:反向显示)

android:animation– 引用动画效果文件

[cpp] view plaincopy
  1. <layoutAnimation  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:delay="0.5"  
  4.     android:animationOrder="normal"  
  5.     android:animation="@anim/list_anim"/>  

2)创建list_anim.xml文件,设置动画效果

[cpp] view plaincopy
  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="0.0"  
  7.        android:toAlpha="1.0"  
  8.        android:duration="1000"/>  
  9. </set>  

3)在布局文件main.xml当中为ListVIew添加如下配置

[cpp] view plaincopy
  1. <ListView  
  2.        android:id="@id/android:list"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="wrap_content"  
  5.         android:scrollbars="vertical"  
  6.         android:layoutAnimation="@anim/list_anim_layout"/>  

4)程序结构




5)list_anim_layout.xml

[cpp] view plaincopy
  1. <layoutAnimation  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:delay="0.5"  
  4.     android:animationOrder="normal"  
  5.     android:animation="@anim/list_anim"/>  

6)list_anim.xml

[cpp] view plaincopy
  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="0.0"  
  7.        android:toAlpha="1.0"  
  8.        android:duration="1000"/>  
  9. </set>  

7)main.xml

[cpp] view plaincopy
  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.     <ListView  
  8.        android:id="@id/android:list"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:scrollbars="vertical"  
  12.         android:layoutAnimation="@anim/list_anim_layout"/>  
  13.     <Button  
  14.         android:id="@+id/button"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="测试"/>  
  18. </LinearLayout>  

8)item.xml

[cpp] view plaincopy
  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:paddingLeft="10dip"  
  7.     android:paddingRight="10dip"  
  8.     android:paddingTop="1dip"  
  9.     android:paddingBottom="1dip">  
  10.     <TextView android:id="@+id/name"  
  11.        android:layout_width="180dip"  
  12.        android:layout_height="30dip"  
  13.        android:textSize="5pt"  
  14.        android:singleLine="true" />  
  15.     <TextView android:id="@+id/sex"  
  16.        android:layout_width="fill_parent"  
  17.        android:layout_height="fill_parent"  
  18.        android:textSize="5pt"  
  19.        android:singleLine="true"/>  
  20. </LinearLayout>  

9)java文件

[cpp] view plaincopy
  1. public class Animation2Activity extendsListActivity {  
  2.     private Button button = null;  
  3.     private ListView listView = null;  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.         listView = getListView();  
  9.         button = (Button)findViewById(R.id.button);  
  10.         button.setOnClickListener(newButtonListener());  
  11.     }  
  12.     private ListAdapter createListAdapter() {  
  13.        List<HashMap<String,String>> list =  
  14.            new ArrayList<HashMap<String,String>>();  
  15.        HashMap<String,String> m1 = new HashMap<String,String>();  
  16.        m1.put("name""bauble");  
  17.        m1.put("sex""male");  
  18.        HashMap<String,String> m2 = new HashMap<String,String>();  
  19.        m2.put("name""Allorry");  
  20.        m2.put("sex""male");  
  21.        HashMap<String,String> m3 = new HashMap<String,String>();  
  22.        m3.put("name""Allotory");  
  23.        m3.put("sex""male");  
  24.        HashMap<String,String> m4 = new HashMap<String,String>();  
  25.        m4.put("name""boolbe");  
  26.        m4.put("sex""male");  
  27.        list.add(m1);  
  28.        list.add(m2);  
  29.        list.add(m3);  
  30.        list.add(m4);  
  31.        SimpleAdapter simpleAdapter = new SimpleAdapter(  
  32.               this,list,R.layout.item,new String[]{"name","sex"},  
  33.               new int[]{R.id.name,R.id.sex});  
  34.        return simpleAdapter;  
  35.     }  
  36.     private class ButtonListener implementsOnClickListener{  
  37.        public void onClick(View v) {  
  38.            listView.setAdapter(createListAdapter());  
  39.        }  
  40.     }  
  41. }  

备注:要将整个动画效果设置到LinerLayout中,可以这样设置:

[cpp] view plaincopy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:layoutAnimation="@anim/list_anim_layout"  
  6. >   

3、在代码当中使用LayoutAnimationController

1)去掉main.xml中的android:layoutAnimation="@anim/list_anim_layout"/>

2)创建一个Animation对象:可以通过装载xml文件,或者是直接使用Animation的构造方法创建Animation对象;

[cpp] view plaincopy
  1. Animation animation = (Animation) AnimationUtils.loadAnimation(  
  2.                   Animation2Activity.this, R.anim.list_anim);  

3)创建LayoutAnimationController对象:  

[cpp] view plaincopy
  1. LayoutAnimationController controller = new LayoutAnimationController(animation);   

4)设置控件的显示顺序以及延迟时间

[cpp] view plaincopy
  1. controller.setOrder(LayoutAnimationController.ORDER_NORMAL);   
  2. controller.setDelay(0.5f);      

5)为ListView设置LayoutAnimationController属性:

[cpp] view plaincopy
  1. listView.setLayoutAnimation(controller);  

完整代码:

[cpp] view plaincopy
  1. private class ButtonListener implementsOnClickListener {  
  2.        public void onClick(View v) {  
  3.            listView.setAdapter(createListAdapter());  
  4.            Animation animation = (Animation) AnimationUtils.loadAnimation(  
  5.                   Animation2Activity.this, R.anim.list_anim);  
  6.             
  7.            LayoutAnimationController controller = new LayoutAnimationController(animation);   
  8.            controller.setOrder(LayoutAnimationController.ORDER_NORMAL);   
  9.            controller.setDelay(0.5f);  
  10.            listView.setLayoutAnimation(controller);   
  11.        }  
  12.     }  


 AnimationListener

1、什么是AnimationListener

1).AnimationListener是一个监听器,该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法;

2).AnimationListener主要包括如下三个方法:

n         ·onAnimationEnd(Animationanimation) - 当动画结束时调用

n         ·onAnimationRepeat(Animationanimation) - 当动画重复时调用

n         ·onAniamtionStart(Animationanimation) - 当动画启动时调用

2、具体实现

1)main.xml

[cpp] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/layout"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent">  
  7.     <Button android:id="@+id/addButton"  
  8.        android:layout_width="fill_parent"  
  9.        android:layout_height="wrap_content"  
  10.        android:layout_alignParentBottom="true"  
  11.        android:text="添加图片" />  
  12.     <Button android:id="@+id/deleteButton"  
  13.        android:layout_width="fill_parent"  
  14.        android:layout_height="wrap_content"  
  15.        android:layout_above="@id/addButton"  
  16.        android:text="删除图片" />  
  17.     <ImageView android:id="@+id/image"  
  18.        android:layout_width="wrap_content"  
  19.        android:layout_height="wrap_content"  
  20.        android:layout_centerInParent="true"  
  21.        android:layout_marginTop="100dip"  
  22.        android:src="@drawable/an" />  
  23. </RelativeLayout>  

2).java文件

[cpp] view plaincopy
  1. public class Animation2Activity extends Activity {  
  2.     private Button addButton = null;  
  3.     private Button deleteButton = null;  
  4.     private ImageView imageView = null;  
  5.     private ViewGroup viewGroup = null;  
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.main);  
  10.         addButton = (Button)findViewById(R.id.addButton);  
  11.         deleteButton = (Button)findViewById(R.id.deleteButton);  
  12.         imageView = (ImageView)findViewById(R.id.image);  
  13.         //LinearLayout下的一组控件  
  14.         viewGroup = (ViewGroup)findViewById(R.id.layout);  
  15.         addButton.setOnClickListener(newAddButtonListener());  
  16.         deleteButton.setOnClickListener(newDeleteButtonListener());  
  17.     }  
  18.     private class AddButtonListener implements OnClickListener{  
  19.        public void onClick(View v) {  
  20.            //淡入  
  21.            AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);  
  22.            animation.setDuration(1000);  
  23.            animation.setStartOffset(500);  
  24.            //创建一个新的ImageView  
  25.            ImageView newImageView = new ImageView(  
  26.               Animation2Activity.this);  
  27.            newImageView.setImageResource(R.drawable.an);  
  28.            viewGroup.addView(newImageView,  
  29.               new LayoutParams(  
  30.                   LayoutParams.FILL_PARENT,  
  31.                   LayoutParams.WRAP_CONTENT));  
  32.            newImageView.startAnimation(animation);  
  33.        }  
  34.     }  
  35.     private class DeleteButtonListener implements OnClickListener{  
  36.        public void onClick(View v) {  
  37.            //淡出  
  38.            AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);  
  39.            animation.setDuration(1000);  
  40.            animation.setStartOffset(500);  
  41.            //为Aniamtion对象设置监听器  
  42.            animation.setAnimationListener(  
  43.               new RemoveAnimationListener());  
  44.            imageView.startAnimation(animation);  
  45.        }  
  46.     }  
  47.     private class RemoveAnimationListener implements AnimationListener{  
  48.        //动画效果执行完时remove  
  49.        public void onAnimationEnd(Animation animation) {  
  50.            System.out.println("onAnimationEnd");  
  51.            viewGroup.removeView(imageView);  
  52.        }  
  53.        public void onAnimationRepeat(Animation animation) {  
  54.            System.out.println("onAnimationRepeat");  
  55.        }  
  56.        public void onAnimationStart(Animation animation) {  
  57.            System.out.println("onAnimationStart");  
  58.        }  
  59.     }  
  60. }  

3、总结一下

可以在Activity中动态添加和删除控件,方法是:

1)取到那个Layout

[cpp] view plaincopy
  1. viewGroup = (ViewGroup)findViewById(R.id.layout);  

2)添加时,先创建对象,然后添加

[cpp] view plaincopy
  1. ImageView newImageView = new ImageView(  
  2.               Animation2Activity.this);  
  3. newImageView.setImageResource(R.drawable.an);  
  4. viewGroup.addView(newImageView,  
  5.               new LayoutParams(  
  6.                   LayoutParams.FILL_PARENT,  
  7.                   LayoutParams.WRAP_CONTENT));  

3)删除时,直接删除。

[cpp] view plaincopy
  1. viewGroup.removeView(imageView);  



0 0
原创粉丝点击