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


  • 相关阅读:
    《构建之法》4、17章精读
    2016012005+小学四则运算练习软件项目报告
    Week2-作业一——《构建之法》三章精读之想
    虚拟到现实
    脚踏实地,莫问前程
    2016012010 赵瑞雪 散列函数的应用及其安全性
    结对项目报告
    《构建之法》第四章、第十七章读书笔记
    2016012010+小学四则运算练习软件项目报告
    《构建之法》第一、二、十六章读书笔记
  • 原文地址:https://www.cnblogs.com/srluv/p/3157482.html
Copyright © 2011-2022 走看看