zoukankan      html  css  js  c++  java
  • android AnimationDrawable运行的几种方式

    项目开发用到了AnimationDrawable,调用start后没有运行,很纳闷。google搜了下。记录一下。

           这个AnimationDrawable.start不能直接写在onClick,onStart,onResume里面,是无效的,无法启动动画,只能写在比如事件监听当中。

      

           以下有几种运行AnimationDrawable的方式。

    第一种:在事件监听中start AnimationDrawable 下面一个例子举例 当一个视图树将要绘制时产生事件

    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.   
    14. };  


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

    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.         {  
    12.             if (!animationDrawable.isRunning())  
    13.             {  
    14.                 animationDrawable.stop();  
    15.                 animationDrawable.start();  
    16.             }  
    17.             return "";  
    18.         }  
    19. }  


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

    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. {  
    6.             @Override  
    7.             public void run()  
    8.             {  
    9.                 animationDrawable.start();  
    10.             }  
    11.         });  


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

    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.     {  
    8.         animationDrawable.start();  
    9.         super.onWindowFocusChanged(hasFocus);  
    10.     }  


  • 相关阅读:
    Two strings CodeForces
    Dasha and Photos CodeForces
    Largest Beautiful Number CodeForces
    Timetable CodeForces
    Financiers Game CodeForces
    AC日记——整理药名 openjudge 1.7 15
    AC日记——大小写字母互换 openjudge 1.7 14
    AC日记——将字符串中的小写字母换成大写字母 openjudge 1.7 13
    AC日记——加密的病历单 openjudge 1.7 12
    AC日记——潜伏着 openjudge 1.7 11
  • 原文地址:https://www.cnblogs.com/srluv/p/3157482.html
Copyright © 2011-2022 走看看