adorid动画

来源:互联网 发布:开淘宝店铺挣钱吗 编辑:程序博客网 时间:2024/04/27 16:40

动画类型

Android的animation由四种类型组成。


XML
alpha    渐变透明度动画效果scale渐变尺寸伸缩动画效果translate画面转换位置移动动画效果rotate画面转移旋转动画效果


JavaCode
AlphaAnimation渐变透明度动画效果ScaleAnimation渐变尺寸伸缩动画效果TranslateAnimation画面转换位置移动动画效果RotateAnimation画面转移旋转动画效果


Android动画模式
Animation主要有两种动画模式:
一种是tweened animation(渐变动画) ,Tween动画主要包括图片的放大缩小、旋转、透明度变化、移动等等操作 。

XMLJavaCodealphaAlphaAnimationscaleScaleAnimationtranslateTranslateAnimationrotateRotateAnimation

一种是frame by frame(画面转换动画) ,Frame动画则简单得多了,就是把一张张的图片连续播放产生动画效果。Frame动画主要是通过AnimationDrawable类来实现的,它有start()和stop()两个重要的方法来启动和停止动画。


如何在XML文件中定义动画
① 打开Eclipse,新建Android工程
② res目录中新建anim文件夹
③ anim目录中新建一个myanim.xml(注意文件名小写)
④ 加入XML的动画代码

[html] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2.  
  3. <setxmlns:android="http://schemas.android.com/apk/res/android"> 
  4.  
  5.   <alpha/> 
  6.  
  7.   <scale/> 
  8.  
  9.   <translate/> 
  10.  
  11.   <rotate/> 
  12.  
  13. </set> 
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">  <alpha/>  <scale/>  <translate/>  <rotate/></set>

Android动画解析--XML

<alpha>

[html] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2.  
  3. <setxmlns:android="http://schemas.android.com/apk/res/android"> 
  4.  
  5. <alpha 
  6.  
  7. android:fromAlpha="0.1" 
  8.  
  9. android:toAlpha="1.0" 
  10.  
  11. android:duration="3000" 
  12.  
  13. />  
  14.  
  15. <!-- 透明度控制动画效果 alpha 
  16.  
  17.         浮点型值: 
  18.  
  19.             fromAlpha 属性为动画起始时透明度 
  20.  
  21.             toAlpha   属性为动画结束时透明度 
  22.  
  23.             说明:  
  24.  
  25.                 0.0表示完全透明 
  26.  
  27.                 1.0表示完全不透明 
  28.  
  29.             以上值取0.0-1.0之间的float数据类型的数字 
  30.  
  31.  
  32.  
  33.         长整型值: 
  34.  
  35.             duration  属性为动画持续时间 
  36.  
  37.             说明:      
  38.  
  39.                 时间以毫秒为单位 
  40.  
  41. --!> 
  42.  
  43. </set> 
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" ><alphaandroid:fromAlpha="0.1"android:toAlpha="1.0"android:duration="3000"/> <!-- 透明度控制动画效果 alpha        浮点型值:            fromAlpha 属性为动画起始时透明度            toAlpha   属性为动画结束时透明度            说明:                 0.0表示完全透明                1.0表示完全不透明            以上值取0.0-1.0之间的float数据类型的数字        长整型值:            duration  属性为动画持续时间            说明:                     时间以毫秒为单位--!></set>

<scale>
[html] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2.  
  3. <setxmlns:android="http://schemas.android.com/apk/res/android"> 
  4.  
  5.    <scale   
  6.  
  7.           android:interpolator
  8.  
  9.                      "@android:anim/accelerate_decelerate_interpolator" 
  10.  
  11.           android:fromXScale="0.0" 
  12.  
  13.           android:toXScale="1.4" 
  14.  
  15.           android:fromYScale="0.0" 
  16.  
  17.           android:toYScale="1.4" 
  18.  
  19.           android:pivotX="50%" 
  20.  
  21.           android:pivotY="50%" 
  22.  
  23.           android:fillAfter="false" 
  24.  
  25.           android:duration="700"/> 
  26.  
  27. </set> 
  28.  
  29. <!-- 尺寸伸缩动画效果 scale 
  30.  
  31.        属性:interpolator 指定一个动画的插入器 
  32.  
  33.         在我试验过程中,使用android.res.anim中的资源时候发现 
  34.  
  35.         有三种动画插入器: 
  36.  
  37.             accelerate_decelerate_interpolator  加速-减速 动画插入器 
  38.  
  39.             accelerate_interpolator        加速-动画插入器 
  40.  
  41.             decelerate_interpolator        减速- 动画插入器 
  42.  
  43.         其他的属于特定的动画效果 
  44.  
  45.       浮点型值: 
  46.  
  47.           
  48.  
  49.             fromXScale 属性为动画起始时 X坐标上的伸缩尺寸     
  50.  
  51.             toXScale   属性为动画结束时 X坐标上的伸缩尺寸      
  52.  
  53.          
  54.  
  55.             fromYScale 属性为动画起始时Y坐标上的伸缩尺寸     
  56.  
  57.             toYScale   属性为动画结束时Y坐标上的伸缩尺寸     
  58.  
  59.          
  60.  
  61.             说明: 
  62.  
  63.                  以上四种属性值     
  64.  
  65.      
  66.  
  67.                     0.0表示收缩到没有  
  68.  
  69.                     1.0表示正常无伸缩      
  70.  
  71.                     值小于1.0表示收缩   
  72.  
  73.                     值大于1.0表示放大 
  74.  
  75.          
  76.  
  77.             pivotX     属性为动画相对于物件的X坐标的开始位置 
  78.  
  79.             pivotY     属性为动画相对于物件的Y坐标的开始位置 
  80.  
  81.          
  82.  
  83.             说明: 
  84.  
  85.                     以上两个属性值 从0%-100%中取值 
  86.  
  87.                     50%为物件的X或Y方向坐标上的中点位置 
  88.  
  89.          
  90.  
  91.         长整型值: 
  92.  
  93.             duration  属性为动画持续时间 
  94.  
  95.             说明:   时间以毫秒为单位 
  96.  
  97.  
  98.  
  99.         布尔型值: 
  100.  
  101.             fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用 
  102.  
  103. --!> 
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">   <scale            android:interpolator=                     "@android:anim/accelerate_decelerate_interpolator"          android:fromXScale="0.0"          android:toXScale="1.4"          android:fromYScale="0.0"          android:toYScale="1.4"          android:pivotX="50%"          android:pivotY="50%"          android:fillAfter="false"          android:duration="700" /></set><!-- 尺寸伸缩动画效果 scale       属性:interpolator 指定一个动画的插入器        在我试验过程中,使用android.res.anim中的资源时候发现        有三种动画插入器:            accelerate_decelerate_interpolator  加速-减速 动画插入器            accelerate_interpolator        加速-动画插入器            decelerate_interpolator        减速- 动画插入器        其他的属于特定的动画效果      浮点型值:                     fromXScale 属性为动画起始时 X坐标上的伸缩尺寸                toXScale   属性为动画结束时 X坐标上的伸缩尺寸                         fromYScale 属性为动画起始时Y坐标上的伸缩尺寸                toYScale   属性为动画结束时Y坐标上的伸缩尺寸                        说明:                 以上四种属性值                            0.0表示收缩到没有                     1.0表示正常无伸缩                         值小于1.0表示收缩                      值大于1.0表示放大                    pivotX     属性为动画相对于物件的X坐标的开始位置            pivotY     属性为动画相对于物件的Y坐标的开始位置                    说明:                    以上两个属性值 从0%-100%中取值                    50%为物件的X或Y方向坐标上的中点位置                长整型值:            duration  属性为动画持续时间            说明:   时间以毫秒为单位        布尔型值:            fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用--!>

<translate>
[html] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2.  
  3. <setxmlns:android="http://schemas.android.com/apk/res/android"> 
  4.  
  5. <translate 
  6.  
  7. android:fromXDelta="30" 
  8.  
  9. android:toXDelta="-80" 
  10.  
  11. android:fromYDelta="30" 
  12.  
  13. android:toYDelta="300" 
  14.  
  15. android:duration="2000" 
  16.  
  17. /> 
  18.  
  19. <!-- translate 位置转移动画效果 
  20.  
  21.         整型值: 
  22.  
  23.             fromXDelta 属性为动画起始时 X坐标上的位置     
  24.  
  25.             toXDelta   属性为动画结束时 X坐标上的位置 
  26.  
  27.             fromYDelta 属性为动画起始时 Y坐标上的位置 
  28.  
  29.             toYDelta   属性为动画结束时 Y坐标上的位置 
  30.  
  31.             注意: 
  32.  
  33.                      没有指定fromXType toXType fromYType toYType 时候, 
  34.  
  35.                      默认是以自己为相对参照物              
  36.  
  37.         长整型值: 
  38.  
  39.             duration  属性为动画持续时间 
  40.  
  41.             说明:   时间以毫秒为单位 
  42.  
  43. --!> 
  44.  
  45. </set> 
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:fromXDelta="30"android:toXDelta="-80"android:fromYDelta="30"android:toYDelta="300"android:duration="2000"/><!-- translate 位置转移动画效果        整型值:            fromXDelta 属性为动画起始时 X坐标上的位置                toXDelta   属性为动画结束时 X坐标上的位置            fromYDelta 属性为动画起始时 Y坐标上的位置            toYDelta   属性为动画结束时 Y坐标上的位置            注意:                     没有指定fromXType toXType fromYType toYType 时候,                     默认是以自己为相对参照物                     长整型值:            duration  属性为动画持续时间            说明:   时间以毫秒为单位--!></set>

<rotate>

[html] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2.  
  3. <setxmlns:android="http://schemas.android.com/apk/res/android"> 
  4.  
  5. <rotate  
  6.  
  7.         android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
  8.  
  9.         android:fromDegrees="0"  
  10.  
  11.         android:toDegrees="+350"          
  12.  
  13.         android:pivotX="50%"  
  14.  
  15.         android:pivotY="50%"      
  16.  
  17.         android:duration="3000"/>   
  18.  
  19. <!-- rotate 旋转动画效果 
  20.  
  21.        属性:interpolator 指定一个动画的插入器 
  22.  
  23.              在我试验过程中,使用android.res.anim中的资源时候发现 
  24.  
  25.              有三种动画插入器: 
  26.  
  27.                 accelerate_decelerate_interpolator   加速-减速 动画插入器 
  28.  
  29.                 accelerate_interpolator               加速-动画插入器 
  30.  
  31.                 decelerate_interpolator               减速- 动画插入器 
  32.  
  33.              其他的属于特定的动画效果 
  34.  
  35.  
  36.  
  37.        浮点数型值: 
  38.  
  39.             fromDegrees 属性为动画起始时物件的角度     
  40.  
  41.             toDegrees   属性为动画结束时物件旋转的角度 可以大于360度    
  42.  
  43.  
  44.  
  45.  
  46.  
  47.             说明: 
  48.  
  49.                      当角度为负数——表示逆时针旋转 
  50.  
  51.                      当角度为正数——表示顺时针旋转               
  52.  
  53.                      (负数from——to正数:顺时针旋转)    
  54.  
  55.                      (负数from——to负数:逆时针旋转)  
  56.  
  57.                      (正数from——to正数:顺时针旋转)  
  58.  
  59.                      (正数from——to负数:逆时针旋转)        
  60.  
  61.  
  62.  
  63.             pivotX     属性为动画相对于物件的X坐标的开始位置 
  64.  
  65.             pivotY     属性为动画相对于物件的Y坐标的开始位置 
  66.  
  67.  
  68.  
  69.             说明:        以上两个属性值 从0%-100%中取值 
  70.  
  71.                          50%为物件的X或Y方向坐标上的中点位置 
  72.  
  73.  
  74.  
  75.         长整型值: 
  76.  
  77.             duration  属性为动画持续时间 
  78.  
  79.             说明:       时间以毫秒为单位 
  80.  
  81. --!> 
  82.  
  83. </set> 
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><rotate         android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:fromDegrees="0"         android:toDegrees="+350"                 android:pivotX="50%"         android:pivotY="50%"             android:duration="3000" />  <!-- rotate 旋转动画效果       属性:interpolator 指定一个动画的插入器             在我试验过程中,使用android.res.anim中的资源时候发现             有三种动画插入器:                accelerate_decelerate_interpolator   加速-减速 动画插入器                accelerate_interpolator               加速-动画插入器                decelerate_interpolator               减速- 动画插入器             其他的属于特定的动画效果       浮点数型值:            fromDegrees 属性为动画起始时物件的角度                toDegrees   属性为动画结束时物件旋转的角度 可以大于360度               说明:                     当角度为负数——表示逆时针旋转                     当角度为正数——表示顺时针旋转                                   (负数from——to正数:顺时针旋转)                        (负数from——to负数:逆时针旋转)                      (正数from——to正数:顺时针旋转)                      (正数from——to负数:逆时针旋转)                   pivotX     属性为动画相对于物件的X坐标的开始位置            pivotY     属性为动画相对于物件的Y坐标的开始位置            说明:        以上两个属性值 从0%-100%中取值                         50%为物件的X或Y方向坐标上的中点位置        长整型值:            duration  属性为动画持续时间            说明:       时间以毫秒为单位--!></set>

如何使用XML中的动画效果

[java] view plaincopyprint?
  1. <span style="font-size: 16px;">publicstatic Animation loadAnimation (Context context,int id)  
  2.  
  3. //第一个参数Context为程序的上下文     
  4.  
  5. //第二个参数id为动画XML文件的引用 
  6.  
  7. //例子: 
  8.  
  9. myAnimation= AnimationUtils.loadAnimation(this,R.anim.my_action); 
  10.  
  11. //使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件</span> 
public static Animation loadAnimation (Context context, int id) //第一个参数Context为程序的上下文    //第二个参数id为动画XML文件的引用//例子:myAnimation= AnimationUtils.loadAnimation(this,R.anim.my_action);//使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件

如何在Java代码中定义动画
[java] view plaincopyprint?
  1. <span style="font-size: 16px;">//在代码中定义 动画实例对象 
  2.  
  3. private Animation myAnimation_Alpha; 
  4.  
  5. private Animation myAnimation_Scale; 
  6.  
  7. private Animation myAnimation_Translate; 
  8.  
  9. private Animation myAnimation_Rotate; 
  10.  
  11.  
  12.  
  13.     //根据各自的构造方法来初始化一个实例对象 
  14.  
  15. myAnimation_Alpha=new AlphaAnimation(0.1f,1.0f); 
  16.  
  17.  
  18.  
  19. myAnimation_Scale =new ScaleAnimation(0.0f,1.4f, 0.0f,1.4f, 
  20.  
  21.              Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
  22.  
  23.  
  24.  
  25. myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f,30.0f, 300.0f); 
  26.  
  27.  
  28.  
  29. myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f, 
  30.  
  31.                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); 
  32. </span> 
//在代码中定义 动画实例对象private Animation myAnimation_Alpha;private Animation myAnimation_Scale;private Animation myAnimation_Translate;private Animation myAnimation_Rotate;    //根据各自的构造方法来初始化一个实例对象myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,             Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,               Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);


Frame动画的实现

Frame动画主要是通过AnimationDrawable类来实现的,它有start()和stop()两个重要的方法来启动和停止动画。Frame动画一般通过XML文件配置,在工程的res/anim目录下创建一个XML配置文件,该配置文件有一个<animation-list>根元素和若干个<item>子元素。

实现一个人跳舞的Frame动画,6张图片如下所示:

第一种方式:

1、把这6张图片放到res/drawable目录下,分别取名为:p01.png,p02.png,p03.png,p04.png,p05.png,p06.png。

2、在res/anim目录下创建一个XML配置文件,文件名为:dance.xml,文件内容:

[html] view plaincopyprint?
  1. <spanstyle="font-family: Arial, Verdana, sans-serif;"><spanstyle="white-space: normal;"><spanstyle="font-family: monospace;"><spanstyle="white-space: pre;"><?xmlversion="1.0"encoding="utf-8"?> 
  2. <animation-listxmlns:apk="http://schemas.android.com/apk/res/android"apk:oneshot="false"> 
  3. <itemapk:drawable="@drawable/p01"apk:duration="500"/> 
  4. <item apk:drawable="@drawable/p02"apk:duration="500"/> 
  5. <itemapk:drawable="@drawable/p03"apk:duration="500"/> 
  6. <item apk:drawable="@drawable/p04"apk:duration="500"/> 
  7. <itemapk:drawable="@drawable/p05"apk:duration="500"/> 
  8. <item apk:drawable="@drawable/p06"apk:duration="500"/> 
  9. </animation-list> 
  10. </span></span></span></span> 
<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:apk="http://schemas.android.com/apk/res/android" apk:oneshot="false"><item apk:drawable="@drawable/p01" apk:duration="500"/><item apk:drawable="@drawable/p02" apk:duration="500"/><item apk:drawable="@drawable/p03" apk:duration="500"/><item apk:drawable="@drawable/p04" apk:duration="500"/><item apk:drawable="@drawable/p05" apk:duration="500"/><item apk:drawable="@drawable/p06" apk:duration="500"/></animation-list>
apk:oneshot指示是否只运行一次,设置为false则意味着循环播放。

3、在res/layout目录下创建layout配置文件dance.xml,文件内容:

[html] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <LinearLayoutxmlns:apk="http://schemas.android.com/apk/res/android"apk:orientation="vertical"apk:layout_width="fill_parent"apk:layout_height="fill_parent"> 
  3. <!-- Frame动画图片 --> 
  4. <ImageViewapk:id="@+id/ImgDance"apk:layout_width="wrap_content"apk:layout_height="wrap_content"apk:background="@anim/dance"/> 
  5.  
  6. <!-- 动画控制按钮 --> 
  7. <LinearLayoutapk:layout_width="fill_parent"apk:layout_height="wrap_content"apk:orientation="horizontal"> 
  8. <Button apk:text="开始"apk:layout_width="wrap_content"apk:layout_height="wrap_content"apk:onClick="onStartDance"/> 
  9. <Buttonapk:text="结束"apk:layout_width="wrap_content"apk:layout_height="wrap_content"apk:onClick="onStopDance"/> 
  10. </LinearLayout> 
  11. </LinearLayout> 
  12.   
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:apk="http://schemas.android.com/apk/res/android" apk:orientation="vertical" apk:layout_width="fill_parent" apk:layout_height="fill_parent"><!-- Frame动画图片 --><ImageView apk:id="@+id/ImgDance" apk:layout_width="wrap_content" apk:layout_height="wrap_content" apk:background="@anim/dance"/><!-- 动画控制按钮 --><LinearLayout apk:layout_width="fill_parent" apk:layout_height="wrap_content" apk:orientation="horizontal"><Button apk:text="开始" apk:layout_width="wrap_content" apk:layout_height="wrap_content" apk:onClick="onStartDance"/><Button apk:text="结束" apk:layout_width="wrap_content" apk:layout_height="wrap_content" apk:onClick="onStopDance"/></LinearLayout></LinearLayout> 
apk:background使用上面的动画作为背景,意味着要取得动画,只要取得该View的背景即可,当然可以在代码中通过设置背景的方式指定;

    apk:onClick指示按钮的动作,当然可以在代码中通过实现OnClickListener的方式实现。

4、Activity代码:

[java] view plaincopyprint?
  1. import android.app.Activity;  
  2.  
  3. import android.graphics.drawable.AnimationDrawable;  
  4.  
  5. import android.os.Bundle;  
  6.  
  7. import android.view.View;  
  8.  
  9. import android.widget.ImageView;  
  10.  
  11.    
  12.  
  13. import com.aboy.android.study.R;  
  14.  
  15.    
  16.  
  17. /**
  18. * Frame动画
  19. *
  20. * @author obullxl@gmail.com
  21. * @version $Id: FrameActivity.java, v 0.1 2011-6-10 下午12:49:46 oldbulla Exp $
  22. */  
  23.  
  24. public class FrameActivityextends Activity {  
  25.  
  26.     public staticfinal String TAG = "FrameActivity";  
  27.  
  28.        
  29.  
  30.     // 显示动画的组件  
  31.  
  32.     private ImageView imgDance;  
  33.  
  34.     // Frame动画  
  35.  
  36.     private AnimationDrawable animDance;  
  37.  
  38.        
  39.  
  40.     /**
  41.      * @see android.app.Activity#onCreate(android.os.Bundle)
  42.      */  
  43.  
  44.     public void onCreate(Bundle cycle) {  
  45.  
  46.         super.onCreate(cycle);  
  47.  
  48.         super.setContentView(R.layout.dance);  
  49.  
  50.            
  51.  
  52.         // 实例化组件  
  53.  
  54.         this.imgDance = (ImageView)super.findViewById(R.id.ImgDance);  
  55.  
  56.            
  57.  
  58.         // 获得背景(6个图片形成的动画)  
  59.  
  60.         this.animDance = (AnimationDrawable)this.imgDance.getBackground();  
  61.  
  62.     }  
  63.  
  64.        
  65.  
  66.     /**
  67.      * 按钮:开始‘跳舞’动画
  68.      */  
  69.  
  70.     public void onStartDance(View view) {  
  71.  
  72.         this.animDance.start();  
  73.  
  74.     }  
  75.  
  76.        
  77.  
  78.     /**
  79.      * 按钮:停止‘跳舞’动画
  80.      */  
  81.  
  82.     public void onStopDance(View view) {  
  83.  
  84.         this.animDance.stop();  
  85.  
  86.     }  
  87.  
  88.        
  89.  
  90. }  
import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.View; import android.widget.ImageView;   import com.aboy.android.study.R;   /**  * Frame动画  *  * @author obullxl@gmail.com  * @version $Id: FrameActivity.java, v 0.1 2011-6-10 下午12:49:46 oldbulla Exp $  */ public class FrameActivity extends Activity {     public static final String TAG = "FrameActivity";           // 显示动画的组件     private ImageView imgDance;     // Frame动画     private AnimationDrawable animDance;           /**      * @see android.app.Activity#onCreate(android.os.Bundle)      */     public void onCreate(Bundle cycle) {         super.onCreate(cycle);         super.setContentView(R.layout.dance);                   // 实例化组件         this.imgDance = (ImageView) super.findViewById(R.id.ImgDance);                   // 获得背景(6个图片形成的动画)         this.animDance = (AnimationDrawable) this.imgDance.getBackground();     }           /**      * 按钮:开始‘跳舞’动画      */     public void onStartDance(View view) {         this.animDance.start();     }           /**      * 按钮:停止‘跳舞’动画      */     public void onStopDance(View view) {         this.animDance.stop();     }       } 

第二种方式:

新建了一个ImgData.java类,存储了序列位图的数据如:
[java] view plaincopyprint?
  1. public class ImgData { 
  2.   
  3.     public staticint[] imgData() 
  4.     { 
  5.         int[] imgData_= 
  6.         { 
  7.             R.drawable.img0,R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4, 
  8.             R.drawable.img5,R.drawable.img6 
  9.         }; 
  10.         return imgData_; 
  11.     } 
  12. }<span style="font-family: Arial, Verdana, sans-serif;"><span style="white-space: normal;"
  13. </span></span> 
public class ImgData { public static int[] imgData(){int[] imgData_={R.drawable.img0,R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6};return imgData_;}}

Activity中的主要代码:

主要变量:

[java] view plaincopyprint?
  1. private AnimationDrawable frameAnimation; 
  2.   private Drawable bitmapDraw; 
  3.   private int[] imgList=ImgData.imgData(); 
  4.   private ImageView imgView; 
  private AnimationDrawable frameAnimation;    private Drawable bitmapDraw;    private int[] imgList=ImgData.imgData();    private ImageView imgView;
子函数:
[java] view plaincopyprint?
  1. private void setFrameAnimation() 
  2.   { 
  3.     for(int i=0;i<imgList.length;i++) 
  4.     { 
  5.         bitmapDraw = getResources().getDrawable(imgList[i]); 
  6.         frameAnimation.addFrame(bitmapDraw, 80); 
  7.     } 
  8.  
  9.     imgView.setBackgroundDrawable(frameAnimation); 
  10.     frameAnimation.setOneShot(isLoop); 
  11. frameAnimation.start(); 
  12. if(frameAnimation.isRunning()) frameAnimation.stop(); 
  13.   } 
  private void setFrameAnimation()    {    for(int i=0;i<imgList.length;i++)    {    bitmapDraw = getResources().getDrawable(imgList[i]);    frameAnimation.addFrame(bitmapDraw, 80);    }     imgView.setBackgroundDrawable(frameAnimation);    frameAnimation.setOneShot(isLoop);frameAnimation.start();if(frameAnimation.isRunning()) frameAnimation.stop();    }



原创粉丝点击