Android中帧动画在Activity启动时自动运行的几种方式

来源:互联网 发布:那谁剧场版知乎 编辑:程序博客网 时间:2024/05/22 13:23
帧动画自启动大概有4种: 

第一种方式启动帧动画:(在Activity启动时会自动运行动画) 
[java] viewplaincopy
  1. AnimationDrawable ad;   
  2.   
  3. ImageView iv (ImageView) findViewById(R.id.animation_view);   
  4.   
  5. iv.setBackgroundResource(R.drawable.animation);   
  6.   
  7. ad (AnimationDrawable) iv.getBackground();   
  8.   
  9. iv.getViewTreeObserver().addOnPreDrawListener(opdl);  
[java] viewplaincopy
  1.  //当一个视图树将要绘制时产生事件,可以添加一个其事件处理函数   
  2. OnPreDrawListener opdl=new OnPreDrawListener(){   
  3. @Override   
  4. public boolean onPreDraw()   
  5. ad.start();   
  6. return true//注意此行返回的值   
  7.   
  8.   
  9. };   

 第二种方式启动动画:(在Activity启动时会自动运行动画)
[java] viewplaincopy
  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. ------------------------------------------------------------  
  8. class RunAnim extends AsyncTask<String, String, String>   
  9.   
  10.   
  11.         @Override   
  12.         protected String doInBackground(String... params)   
  13.           
  14.             if (!animationDrawable.isRunning())   
  15.               
  16.                 animationDrawable.stop();   
  17.                 animationDrawable.start();   
  18.               
  19.             return ""  
  20.           
  21.   

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

[java] viewplaincopy
  1. ImageView image (ImageView) findViewById(R.id.animation_view);   
  2.   
  3. image.setBackgroundResource(R.anim.oldsheep_wait);   
  4.   
  5. animationDrawable (AnimationDrawable) image.getBackground();   
  6. image.post(new Runnable()   
  7.   
  8.             @Override   
  9.             public void run()   
  10.               
  11.                 animationDrawable.start();  
  12.               
  13.   
  14.         });   

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

[java] viewplaincopy
  1. ImageView image (ImageView) findViewById(R.id.animation_view);   
  2.   
  3. image.setBackgroundResource(R.anim.oldsheep_wait);   
  4. animationDrawable (AnimationDrawable) image.getBackground();   
  5.   
  6. -----------------------------------------------------------  
  7. @Override   
  8.     public void onWindowFocusChanged(boolean hasFocus)   
  9.       
  10.        animationDrawable.start();   
  11.         super.onWindowFocusChanged(hasFocus);   
  12.       

要实现动画的自启动直接写在onCreate,onStart,onResume里面,单纯的.start()一句启动是无效的,必须有事件启动,写在比如事件监听当中 。



转载自:http://blog.sina.com.cn/s/blog_8f5097be010127j2.html