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

帧动画自启动大概有4种: 

第一种方式启动帧动画:(在Activity启动时会自动运行动画) 
[java]   viewplain copy
  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]   viewplain copy
  1.  //当一个视图树将要绘制时产生事件,可以添加一个其事件处理函数   
  2. OnPreDrawListener opdl=new OnPreDrawListener(){   
  3. @Override   
  4. public boolean onPreDraw()   
  5. ad.start();   
  6. return true//注意此行返回的值   
  7.   
  8.   
  9. };   

 第二种方式启动动画:(在Activity启动时会自动运行动画)
[java]   viewplain 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. ------------------------------------------------------------  
  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]   viewplain copy
  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]   viewplain copy
  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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值