Android Frame动画——AnimationDrawable

有两种方式

1、通过在xml定义一个animation-list:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animation-list   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:oneshot="false"  
  5.     >  
  6.     <item android:drawable="@drawable/img01" android:duration="50" />  
  7.     <item android:drawable="@drawable/img02" android:duration="50" />  
  8.     <item android:drawable="@drawable/img03" android:duration="50" />  
  9.     <item android:drawable="@drawable/img04" android:duration="50" />  
  10. </animation-list>  

然后在Java中的代码处理如下:

[html]  view plain copy
  1. // Load the ImageView that will host the animation and  
  2. // set its background to our AnimationDrawable XML resource.  
  3. ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);  
  4. img.setBackgroundResource(R.drawable.spin_animation);  
  5.   
  6. // Get the background, which has been compiled to an AnimationDrawable object.  
  7. AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();  
  8.   
  9. // Start the animation (looped playback by default).  
  10. frameAnimation.start();  

 

2、直接在代码中动态添加frame:

[java]  view plain copy
  1. animationDrawable = new AnimationDrawable();  
  2. for(int i=1; i<5; i++) {  
  3.     int id = getResources().getIdentifier("img0" + i, "drawable", getPackageName());  
  4.     Log.d("DEBUG""ID: " + id);  
  5.     Drawable drawable = getResources().getDrawable(id);  
  6.     animationDrawable.addFrame(drawable, 100);  
  7. }  
  8. imageView.setBackgroundDrawable(animationDrawable);  
  9. animationDrawable.setOneShot(false);  

在这里有一点需要注意的是AnimationDrawable的start()不能在直接在onClick,onStart,onResume里面启动,是无效的,无法启动动画,只能写在比如事件监听当中。下面有几种启动方法(这里参考http://www611.iteye.com/blog/1217096):


第一种方式启动帧动画:(在Activity启动时会自动运行动画)

[java]  view plain copy
  1. AnimationDrawable ad;  
  2. ImageView iv = (ImageView) findViewById(R.id.animation_view);  
  3. iv.setBackgroundResource(R.drawable.animation);  
  4. ad = (AnimationDrawable) iv.getBackground();  
  5. iv.getViewTreeObserver().addOnPreDrawListener(opdl);  
  6. //当一个视图树将要绘制时产生事件,可以添加一个其事件处理函数  
  7. OnPreDrawListener opdl=new OnPreDrawListener(){  
  8.     @Override  
  9.     public boolean onPreDraw() {  
  10.         ad.start();  
  11.         return true//注意此行返回的值  
  12.     }  
  13. };  


第二种方式启动动画:(在Activity启动时会自动运行动画)

[java]  view plain copy
  1. ImageView image = (ImageView) findViewById(R.id.animation_view);  
  2. image.setBackgroundResource(R.anim.oldsheep_wait);  
  3. animationDrawable = (AnimationDrawable) image.getBackground();  
  4. RunAnim runAnim=new RunAnim();  
  5. runAnim.execute("");  
  6.   
  7. class RunAnim extends AsyncTask<String, String, String>  
  8. {  
  9.     @Override  
  10.     protected String doInBackground(String... params) {  
  11.         if (!animationDrawable.isRunning()) {  
  12.             animationDrawable.stop();  
  13.             animationDrawable.start();  
  14.         }  
  15.         return "";  
  16.     }  
  17. }  


第三种方式启动动画:(在Activity启动时会自动运行动画)

[java]  view plain copy
  1. ImageView image = (ImageView) findViewById(R.id.animation_view);  
  2. image.setBackgroundResource(R.anim.oldsheep_wait);  
  3. animationDrawable = (AnimationDrawable) image.getBackground();  
  4. image.post(new Runnable() {  
  5.     @Override  
  6.     public void run() {  
  7.         animationDrawable.start();  
  8.     }  
  9. });  


第四种方式启动动画:(在Activity启动时会自动运行动画)
 

[java]  view plain copy
  1. ImageView image = (ImageView) findViewById(R.id.animation_view);  
  2. image.setBackgroundResource(R.anim.oldsheep_wait);  
  3. animationDrawable = (AnimationDrawable) image.getBackground();  
  4.   
  5. @Override  
  6. public void onWindowFocusChanged(boolean hasFocus) {  
  7.     animationDrawable.start();  
  8.     super.onWindowFocusChanged(hasFocus);  
  9. }  


 

 

完整代码如下:

[java]  view plain copy
  1. package com.lau.frame;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.drawable.AnimationDrawable;  
  5. import android.os.Bundle;  
  6. import android.widget.ImageView;  
  7.   
  8. public class AnimationFrameDemoActivity extends Activity {  
  9.       
  10.     private ImageView imageView;  
  11.     private AnimationDrawable animationDrawable;  
  12.       
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         imageView = (ImageView) findViewById(R.id.imageView1);  
  19.         imageView.setBackgroundResource(R.drawable.frame_list);  
  20.         animationDrawable = (AnimationDrawable) imageView.getBackground();  
  21.         animationDrawable.start();  
  22.           
  23. //        animationDrawable = new AnimationDrawable();  
  24. //        for(int i=1; i<5; i++) {  
  25. //          int id = getResources().getIdentifier("img0" + i, "drawable", getPackageName());  
  26. //          Log.d("DEBUG", "ID: " + id);  
  27. //          Drawable drawable = getResources().getDrawable(id);  
  28. //          animationDrawable.addFrame(drawable, 100);  
  29. //        }  
  30. //        imageView.setBackgroundDrawable(animationDrawable);  
  31. //        animationDrawable.setOneShot(false);  
  32. //        imageView.post(new Runnable() {  
  33. //          @Override  
  34. //          public void run() {  
  35. //              animationDrawable.start();  
  36. //          }  
  37. //      });  
  38.     }  
  39.       
  40. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值