tween和frame 动画

来源:互联网 发布:淘宝旺旺官网 编辑:程序博客网 时间:2024/05/16 19:55

Android中的动画类型有两种,一种是Tween动画、还有一种是Frame动画。Tween动画,这种实现方式可以使视图组件移动、放大、缩小以及产生透明度的变化;另一种Frame动画,传统的动画方法,通过顺序的播放排列好的图片来实现,类似电影。下面将一一详细介绍:

       本博客参考自网络,结合自己一点理解,实为学习之用,不为其他目的。

一,Tween动画

        又称“补间动画”、“中间动画”,最早接触Tween类是在学习Flash时候,使用ActionScript做动画的时候,使用过类Tween。

        Tween动画主要的功能是在绘制动画前设置动画绘制的轨迹,包括时间, 位置 ,等等。但是Tween动画的缺点是它只能设置起始点与结束点的两帧,中间过程全部由系统帮我们完成。所以在帧数比较多的游戏开发中是不太会用到它的。
       Tween一共提供了4中动画的效果

       Scale:缩放动画
       Rotate:旋转动画
       Translate:移动动画
       Alpha::透明渐变动画

      Tween与Frame动画类似都需要在res\anim路径下创建动画的 布局文件

   1)Scale动画

         

[html] view plaincopyprint?
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.View;  
  4. import android.view.View.OnClickListener;  
  5. import android.view.animation.Animation;  
  6. import android.view.animation.AnimationUtils;  
  7. import android.widget.Button;  
  8. import android.widget.ImageView;  
  9.   
  10. public class ScaleActivity extends Activity {  
  11.   
  12.   
  13.     Button mButton0 = null;//缩小动画    
  14.      
  15.     Button mButton1 = null;//放大动画  
  16.     
  17.     ImageView mImageView = null; //显示动画  
  18.     
  19.     Animation mLitteAnimation = null; //缩小动画  
  20.       
  21.     Animation mBigAnimation = null; //放大动画  
  22.       
  23.       
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.     super.onCreate(savedInstanceState);  
  27.     setContentView(R.layout.scale);  
  28.   
  29.     mImageView = (ImageView)findViewById(R.id.imageView);  
  30.   
  31.     /**加载缩小与放大动画**/  
  32.     mLitteAnimation = AnimationUtils.loadAnimation(this, R.anim.scalelitte);  
  33.     mBigAnimation = AnimationUtils.loadAnimation(this, R.anim.scalebig);  
  34.       
  35.     mButton0 = (Button)findViewById(R.id.button0);  
  36.     mButton0.setOnClickListener(new OnClickListener() {  
  37.           
  38.         @Override  
  39.         public void onClick(View arg0) {  
  40.           
  41.         /**播放缩小动画**/  
  42.         mImageView.startAnimation(mLitteAnimation);  
  43.           
  44.         }  
  45.     });  
  46.       
  47.     mButton1 = (Button)findViewById(R.id.button1);  
  48.     mButton1.setOnClickListener(new OnClickListener() {  
  49.           
  50.         @Override  
  51.         public void onClick(View arg0) {  
  52.         /**播放放大动画**/  
  53.         mImageView.startAnimation(mBigAnimation);  
  54.         }  
  55.     });  
  56.     }  
  57. }  

[html] view plaincopyprint?
  1. <scale>标签为缩放节点  
  2. android:fromXscale="1.0" 表示开始时X轴缩放比例为 1.0 (原图大小 * 1.0 为原图大小)  
  3. android:toXscale="0.0"   表示结束时X轴缩放比例为0.0(原图大小 *0.0 为缩小到看不见)  
  4. android:fromYscale="1.0" 表示开始时Y轴缩放比例为 1.0 (原图大小 * 1.0 为原图大小)  
  5. android:toYscale="0.0"   表示结束时Y轴缩放比例为0.0(原图大小 *0.0 为缩小的看不到了)  
  6. android:pivotX="50%"     X轴缩放的位置为中心点  
  7. android:pivotY="50%"     Y轴缩放的位置为中心点  
  8. android:duration="2000"  动画播放时间 这里是2000毫秒也就是2秒  

/anim/scalelitte.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <scale   
  3.       xmlns:android="http://schemas.android.com/apk/res/android"  
  4.       android:fromXScale="0.0"   
  5.       android:toXScale="1.0"  
  6.       android:fromYScale="0.0"   
  7.       android:toYScale="1.0"   
  8.       android:pivotX="50%"  
  9.       android:pivotY="50%"   
  10.       android:duration="2000">  
  11. </scale>  
/anim/scalebig.xml
[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <scale xmlns:android="http://schemas.android.com/apk/res/android"  
  3.             android:fromXScale="1.0"   
  4.             android:toXScale="0.0"  
  5.             android:fromYScale="1.0"   
  6.             android:toYScale="0.0"   
  7.             android:pivotX="50%"  
  8.             android:pivotY="50%"   
  9.             android:duration="2000">  
  10. </scale>  

如果在代码中,加载动画,而不用xml配置动画

[html] view plaincopyprint?
  1. mLitteAnimation =  new ScaleAnimation(0.0f, 1.0f, 0.0f,  1.0f,  
  2.                   Animation.RELATIVE_TO_SELF, 0.5f,    
  3.                   Animation.RELATIVE_TO_SELF, 0.5f);    
  4.               mLitteAnimation.setDuration(2000);  


2)Rotate旋转动画


[html] view plaincopyprint?
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.View;  
  4. import android.view.View.OnClickListener;  
  5. import android.view.animation.Animation;  
  6. import android.view.animation.AnimationUtils;  
  7. import android.widget.Button;  
  8. import android.widget.ImageView;  
  9.   
  10. public class RotateActivity extends Activity {  
  11.   
  12.     /**向左旋转动画按钮**/  
  13.     Button mButton0 = null;  
  14.     
  15.     /**向右旋转动画按钮**/  
  16.     Button mButton1 = null;  
  17.     
  18.     /**显示动画的ImageView**/  
  19.     ImageView mImageView = null;  
  20.     
  21.     /**向左旋转动画**/  
  22.     Animation mLeftAnimation = null;  
  23.       
  24.     /**向右旋转动画**/  
  25.     Animation mRightAnimation = null;   
  26.       
  27.       
  28.     @Override  
  29.     public void onCreate(Bundle savedInstanceState) {  
  30.     super.onCreate(savedInstanceState);  
  31.     setContentView(R.layout.retate);  
  32.   
  33.     /**拿到ImageView对象**/  
  34.     mImageView = (ImageView)findViewById(R.id.imageView);  
  35.   
  36.     /**加载向左与向右旋转动画**/  
  37.     mLeftAnimation = AnimationUtils.loadAnimation(this, R.anim.retateleft);  
  38.     mRightAnimation = AnimationUtils.loadAnimation(this, R.anim.retateright);  
  39.       
  40.     mButton0 = (Button)findViewById(R.id.button0);  
  41.     mButton0.setOnClickListener(new OnClickListener() {  
  42.           
  43.         @Override  
  44.         public void onClick(View arg0) {  
  45.           
  46.         /**播放向左旋转动画**/  
  47.         mImageView.startAnimation(mLeftAnimation);  
  48.           
  49.         }  
  50.     });  
  51.       
  52.     mButton1 = (Button)findViewById(R.id.button1);  
  53.     mButton1.setOnClickListener(new OnClickListener() {  
  54.           
  55.         @Override  
  56.         public void onClick(View arg0) {  
  57.         /**播放向右旋转动画**/  
  58.         mImageView.startAnimation(mRightAnimation);  
  59.         }  
  60.     });  
  61.     }  
  62. }  

[html] view plaincopyprint?
  1. <rotate>标签为旋转节点  
  2. Tween一共为我们提供了3种动画渲染模式。  
  3. android:interpolator="@android:anim/accelerate_interpolator" 设置动画渲染器为加速动画(动画播放中越来越快)  
  4. android:interpolator="@android:anim/decelerate_interpolator" 设置动画渲染器为减速动画(动画播放中越来越慢)  
  5. android:interpolator="@android:anim/accelerate_decelerate_interpolator" 设置动画渲染器为先加速在减速(开始速度最快 逐渐减慢)  
  6. 如果不写的话 默认为匀速运动  
  7.   
  8. android:fromDegrees="+360"设置动画开始的角度  
  9. android:toDegrees="0"设置动画结束的角度  
  10.   
  11. 这个动画布局设置动画将向左做360度旋转加速运动。  

/anim/retateleft.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <rotate xmlns:android="http://schemas.android.com/apk/res/android"   
  3.         android:interpolator="@android:anim/accelerate_interpolator"    
  4.         android:fromDegrees="+360"    
  5.         android:toDegrees="0"    
  6.         android:pivotX="50%"    
  7.         android:pivotY="50%"    
  8.         android:duration="2000"   
  9. />    
/anim/retateright.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <rotate  xmlns:android="http://schemas.android.com/apk/res/android"  
  3.         android:interpolator="@android:anim/decelerate_interpolator"    
  4.         android:fromDegrees="0"    
  5.         android:toDegrees="+360"    
  6.         android:pivotX="50%"    
  7.         android:pivotY="50%"    
  8.         android:duration="2000"   
  9. />    


 如果在代码中加载动画,而不用xml配置,代码如下

[html] view plaincopyprint?
  1. mLeftAnimation = new RotateAnimation(360.0f, 0.0f,  
  2.         Animation.RELATIVE_TO_SELF, 0.5f,    
  3.         Animation.RELATIVE_TO_SELF, 0.5f);              
  4.              mLeftAnimation.setDuration(2000);    

3)Translate移动动画
[html] view plaincopyprint?
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.animation.Animation;  
  4. import android.view.animation.AnimationUtils;  
  5. import android.widget.ImageView;  
  6.   
  7. public class TranslateActivity extends Activity {  
  8.     /**显示动画的ImageView**/  
  9.     ImageView mImageView = null;  
  10.     
  11.     /**移动动画**/  
  12.     Animation mAnimation = null;  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.     super.onCreate(savedInstanceState);  
  16.     setContentView(R.layout.translate);  
  17.   
  18.     /**拿到ImageView对象**/  
  19.     mImageView = (ImageView)findViewById(R.id.imageView);  
  20.   
  21.     /**加载移动动画**/  
  22.     mAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);  
  23.       
  24.     /**播放移动动画**/  
  25.     mImageView.startAnimation(mAnimation);  
  26.     }  
  27. }  
/layout/translate.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.   <ImageView  
  8.    android:id="@+id/imageView"  
  9.    android:src="@drawable/images"  
  10.    android:layout_width="wrap_content"  
  11.    android:layout_height="wrap_content"  
  12.  />  
  13. </LinearLayout>  
/anim/translate.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <translate  xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromXDelta="0"    
  4.     android:toXDelta="320"    
  5.     android:fromYDelta="0"    
  6.     android:toYDelta="480"    
  7.     android:duration="2000"    
  8.     android:repeatCount="infinite"   
  9. />    

说明:
[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <translate  xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromXDelta="0"    
  4.     android:toXDelta="320"    
  5.     android:fromYDelta="0"    
  6.     android:toYDelta="480"    
  7.     android:duration="2000"    
  8.     android:repeatCount="infinite"   
  9. />    

代码中加载动画:
[html] view plaincopyprint?
  1. mAnimation = new TranslateAnimation(0, 320, 0, 480);  
  2. mAnimation.setDuration(2000);    

4 )Alpha:透明渐变动画

[html] view plaincopyprint?
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.animation.Animation;  
  4. import android.view.animation.AnimationUtils;  
  5. import android.widget.ImageView;  
  6.   
  7. public class AlphaActivity extends Activity {  
  8.     /**显示动画的ImageView**/  
  9.     ImageView mImageView = null;  
  10.     
  11.     /**透明动画**/  
  12.     Animation mAnimation = null;  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.     super.onCreate(savedInstanceState);  
  16.     setContentView(R.layout.translate);  
  17.   
  18.     /**拿到ImageView对象**/  
  19.     mImageView = (ImageView)findViewById(R.id.imageView);  
  20.   
  21.     /**加载透明动画**/  
  22.     mAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);  
  23.       
  24.     /**播放透明动画**/  
  25.     mImageView.startAnimation(mAnimation);  
  26.     }  
  27. }  
/anim/alpha.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <alpha  xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromAlpha="1.0"    
  4.     android:toAlpha="0.0"   
  5.     android:repeatCount="infinite"    
  6.     android:duration="2000">    
  7. </alpha>    

说明:
[html] view plaincopyprint?
  1. <alpha>标签为alpha透明度节点  
  2. android:fromAlpha="1.0" 设置动画起始透明度为1.0 表示完全不透明  
  3. android:toAlpha="0.0"设置动画结束透明度为0.0 表示完全透明  
  4. 也就是说alpha的取值范围为0.0 - 1.0 之间  

手动加载动画:
[html] view plaincopyprint?
  1. mAnimation = new AlphaAnimation(1.0f, 0.0f);   
  2. mAnimation.setDuration(2000);    

5)综合动画

可以将上面介绍的4种动画设置在一起同时进行播放,那么就须要使用<set>标签将所有须要播放的动画放在一起。

这个动画布局设置动画同时播放
移动、渐变、旋转


[html] view plaincopyprint?
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.animation.Animation;  
  4. import android.view.animation.AnimationUtils;  
  5. import android.widget.ImageView;  
  6.   
  7. public class AllActivity extends Activity {  
  8.   
  9.     ImageView mImageView = null;  
  10.     
  11.     Animation mAnimation = null;  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.     super.onCreate(savedInstanceState);  
  15.     setContentView(R.layout.translate);  
  16.   
  17.     mImageView = (ImageView)findViewById(R.id.imageView);  
  18.   
  19.     mAnimation = AnimationUtils.loadAnimation(this, R.anim.all);  
  20.       
  21.     mImageView.startAnimation(mAnimation);  
  22.     }  
  23. }  
/anim/all.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">    
  3.     <rotate    
  4.         android:interpolator="@android:anim/accelerate_interpolator"    
  5.         android:fromDegrees="+360"    
  6.         android:toDegrees="0"    
  7.         android:pivotX="50%"    
  8.         android:pivotY="50%"    
  9.         android:duration="2000"   
  10.         android:repeatCount="infinite"  
  11.     />   
  12.     <alpha  android:fromAlpha="1.0"    
  13.     android:toAlpha="0.0"   
  14.     android:repeatCount="infinite"  
  15.     android:duration="2000">    
  16.     </alpha>   
  17.  <translate    
  18.     android:fromXDelta="0"    
  19.     android:toXDelta="320"    
  20.     android:fromYDelta="0"    
  21.     android:toYDelta="480"    
  22.     android:duration="2000"    
  23.     android:repeatCount="infinite"   
  24. />    
  25. </set>    


二,AnimationDrable实现Frame动画(设计游戏专用,嘎嘎嘎)感谢宣教主分享


[html] view plaincopyprint?
  1. import android.app.Activity;  
  2. import android.graphics.drawable.AnimationDrawable;  
  3. import android.os.Bundle;  
  4. import android.util.Log;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.ImageView;  
  9. import android.widget.RadioButton;  
  10. import android.widget.RadioGroup;  
  11. import android.widget.SeekBar;  
  12. import android.widget.SeekBar.OnSeekBarChangeListener;  
  13.   
  14. public class SimpleActivity extends Activity {  
  15.   
  16.     /**播放动画按钮**/  
  17.     Button button0 = null;  
  18.     
  19.     /**停止动画按钮**/  
  20.     Button button1 = null;  
  21.       
  22.     /**设置动画循环选择框**/  
  23.     RadioButton radioButton0null;  
  24.     RadioButton radioButton1null;  
  25.     RadioGroup  radioGroup = null;  
  26.     
  27.     /**拖动图片修改Alpha值**/  
  28.     SeekBar seekbar = null;  
  29.     
  30.     /**绘制动画View**/  
  31.     ImageView imageView = null;  
  32.      
  33.     /**绘制动画对象**/  
  34.     AnimationDrawable animationDrawable = null;  
  35.     @Override  
  36.     public void onCreate(Bundle savedInstanceState) {  
  37.     super.onCreate(savedInstanceState);  
  38.     setContentView(R.layout.simple);  
  39.   
  40.     /**拿到ImageView对象**/  
  41.     imageView = (ImageView)findViewById(R.id.imageView);  
  42.     /**通过ImageView对象拿到背景显示的AnimationDrawable**/  
  43.     animationDrawable = (AnimationDrawable) imageView.getBackground();  
  44.       
  45.       
  46.     /**开始播放动画**/  
  47.     button0 = (Button)findViewById(R.id.button0);  
  48.     button0.setOnClickListener(new OnClickListener() {  
  49.           
  50.         @Override  
  51.         public void onClick(View arg0) {  
  52.         /**播放动画**/  
  53.         if(!animationDrawable.isRunning()) {  
  54.             animationDrawable.start();  
  55.         }  
  56.         }  
  57.     });  
  58.       
  59.     /**停止播放动画**/  
  60.     button1 = (Button)findViewById(R.id.button1);  
  61.     button1.setOnClickListener(new OnClickListener() {  
  62.           
  63.         @Override  
  64.         public void onClick(View arg0) {  
  65.         /**停止动画**/  
  66.         if(animationDrawable.isRunning()) {  
  67.             animationDrawable.stop();  
  68.         }  
  69.         }  
  70.     });  
  71.     /**单次播放**/  
  72.     radioButton0 = (RadioButton)findViewById(R.id.checkbox0);  
  73.     /**循环播放**/  
  74.     radioButton1 = (RadioButton)findViewById(R.id.checkbox1);  
  75.     /**单选列表组**/  
  76.     radioGroup = (RadioGroup)findViewById(R.id.radiogroup);  
  77.     radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  78.           
  79.         @Override  
  80.         public void onCheckedChanged(RadioGroup radioGroup, int checkID) {  
  81.         if(checkID == radioButton0.getId()) {  
  82.             //设置单次播放  
  83.             animationDrawable.setOneShot(true);  
  84.         }else if (checkID == radioButton1.getId()) {  
  85.             //设置循环播放  
  86.             animationDrawable.setOneShot(false);  
  87.         }  
  88.           
  89.         //发生改变后让动画重新播放  
  90.         animationDrawable.stop();  
  91.         animationDrawable.start();  
  92.         }  
  93.     });  
  94.       
  95.     /**监听的进度条修改透明度**/  
  96.     seekbar = (SeekBar)findViewById(R.id.seekBar);  
  97.     seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  98.         @Override  
  99.         public void onStopTrackingTouch(SeekBar seekBar) {  
  100.           
  101.         }  
  102.         @Override  
  103.         public void onStartTrackingTouch(SeekBar seekBar) {  
  104.           
  105.         }  
  106.         @Override  
  107.         public void onProgressChanged(SeekBar seekBar, int progress, boolean frameTouch) {  
  108.         /**设置动画Alpha值**/  
  109.         animationDrawable.setAlpha(progress);  
  110.         /**通知imageView 刷新屏幕**/  
  111.         imageView.postInvalidate();  
  112.         }  
  113.     });  
  114.       
  115.     }  
  116. }  

/layout/simple.xml

[html] view plaincopyprint?
  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. <LinearLayout   
  8.     android:orientation="horizontal"  
  9.     android:layout_width="wrap_content"  
  10.     android:layout_height="wrap_content"  
  11.     >  
  12.  <Button  
  13.    android:id="@+id/button0"  
  14.    android:layout_width="wrap_content"  
  15.    android:layout_height="wrap_content"  
  16.    android:text="播放动画"  
  17.  />  
  18.    
  19.   <Button  
  20.    android:id="@+id/button1"  
  21.    android:layout_width="wrap_content"  
  22.    android:layout_height="wrap_content"  
  23.    android:text="停止动画"  
  24.  />  
  25.  </LinearLayout>  
  26.      
  27.  <RadioGroup android:id="@+id/radiogroup"  
  28.      android:layout_width="wrap_content"  
  29.      android:layout_height="wrap_content"  
  30.      android:orientation="horizontal">  
  31.    <RadioButton  
  32.      android:id="@+id/checkbox0"  
  33.      android:layout_width="wrap_content"  
  34.      android:layout_height="wrap_content"  
  35.      android:checked="true"  
  36.      android:text="单次播放"  
  37.    />  
  38.   <RadioButton  
  39.     android:id="@+id/checkbox1"  
  40.     android:layout_width="wrap_content"  
  41.     android:layout_height="wrap_content"  
  42.     android:text="循环播放"  
  43.    />  
  44.    </RadioGroup>  
  45.      
  46.     <TextView  
  47.     android:layout_width="wrap_content"  
  48.     android:layout_height="wrap_content"  
  49.     android:text="拖动进度条修改透明度(0 - 255)之间"  
  50.     />   
  51.   <SeekBar  
  52.     android:id="@+id/seekBar"  
  53.     android:layout_width="fill_parent"  
  54.     android:layout_height="wrap_content"  
  55.     android:max="256"  
  56.     android:progress="256"/>  
  57.   <ImageView  
  58.    android:id="@+id/imageView"  
  59.    android:background="@anim/animation"  
  60.    android:layout_width="wrap_content"  
  61.    android:layout_height="wrap_content"  
  62.  />  
  63. </LinearLayout>  


控制帧播放的/anim/animation.xml

[html] view plaincopyprint?
  1. <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">  
  2.  <item android:drawable="@drawable/a" android:duration="100" />   
  3.  <item android:drawable="@drawable/b" android:duration="100" />   
  4.  <item android:drawable="@drawable/c" android:duration="100" />   
  5.  <item android:drawable="@drawable/d" android:duration="100" />   
  6.  <item android:drawable="@drawable/e" android:duration="100" />   
  7.  <item android:drawable="@drawable/f" android:duration="100" />   
  8.  <item android:drawable="@drawable/g" android:duration="100" />   
  9.  <item android:drawable="@drawable/h" android:duration="100" />   
  10.  <item android:drawable="@drawable/i" android:duration="100" />   
  11.  <item android:drawable="@drawable/j" android:duration="100" />   
  12.  </animation-list>  
      看看内容应该是很好理解的,<animation-list>为动画的总标签,这里面放着帧动画 <item>标签,也就是说若干<item>标签的帧 组合在一起就是帧动画了。<animation-list > 标签中android:oneshot="false" 这是一个非常重要的属性,默认为false 表示 动画循环播放, 如果这里写true 则表示动画只播发一次。 <item>标签中记录着每一帧的信息android:drawable="@drawable/a"表示这一帧用的图片为"a",下面以此类推。  android:duration="100" 表示这一帧持续100毫秒,可以根据这个值来调节动画播放的速度。

这是一个比较简单的布局文件,应该都能看懂吧。  我主要说一下 最后的这个 ImageView, 它就是用来显示我们的动画。 这里使用android:background="@anim/animation"设置这个ImageView现实的背景为一个动画,动画资源的路径为res/anim/animation.xml   ,当然 设置background同样也可以在代码中设置。


[java] view plaincopy
  1. imageView.setBackgroundResource(R.anim.animation);  




通过getBackground方法就可以拿到这个animationDrawable对象。


[java] view plaincopy
  1. /**拿到ImageView对象**/  
  2. imageView = (ImageView)findViewById(R.id.imageView);  
  3. /**通过ImageView对象拿到背景显示的AnimationDrawable**/  
  4. animationDrawable = (AnimationDrawable) imageView.getBackground();  


AnimationDrawable 就是用来控制这个帧动画,这个类中提供了很多方法。

animationDrawable.start(); 开始这个动画
animationDrawable.stop(); 结束这个动画
animationDrawable.setAlpha(100);设置动画的透明度, 取值范围(0 - 255)
animationDrawable.setOneShot(true); 设置单次播放
animationDrawable.setOneShot(false); 设置循环播放
animationDrawable.isRunning(); 判断动画是否正在播放
animationDrawable.getNumberOfFrames(); 得到动画的帧数。


宣教主警戒:拖动进度条设置Alpha值的时候 一定要使用     imageView.postInvalidate(); 方法来通知UI线程重绘屏幕中的imageView  否则会看不到透明的效果 。这里切记切记~~


谢谢CSDN博主宣雨松,牛人一枚。以后还会多多向他学习。


原创粉丝点击