Android中幀動畫在Activity啓動時自動運行的幾種方式

 

http://www.eoeandroid.com/blog-468046-657.html

幀動畫:

第一種方式啓動幀動畫:(在Activity啓動時會自動運行動畫)

AnimationDrawable ad

ImageView iv = (ImageView) findViewById(R.id.animation_view);

iv.setBackgroundResource(R.drawable.animation);

ad = (AnimationDrawable) iv.getBackground();

iv.getViewTreeObserver().addOnPreDrawListener(opdl);

//當一個視圖樹將要繪製時產生事件,可以添加一個其事件處理函數

 

OnPreDrawListener opdl=new OnPreDrawListener(){

      @Override

      public boolean onPreDraw() {

         ad.start();

         return true; //注意此行返回的值

      }

   };

第二種方式啓動動畫:(在Activity啓動時會自動運行動畫)

ImageView image = (ImageView) findViewById(R.id.animation_view);

image.setBackgroundResource(R.anim.oldsheep_wait);

        animationDrawable = (AnimationDrawable) image.getBackground();

        RunAnim runAnim=new RunAnim();

        runAnim.execute("");

 

class RunAnim extends AsyncTask<String, String, String>

{

        @Override

        protected String doInBackground(String... params)

        {

            if (!animationDrawable.isRunning())

            {

                animationDrawable.stop();

                animationDrawable.start();

            }

            return "";

        }

}

 

第三種方式啓動動畫:(在Activity啓動時會自動運行動畫)

ImageView image = (ImageView) findViewById(R.id.animation_view);

image.setBackgroundResource(R.anim.oldsheep_wait);

        animationDrawable = (AnimationDrawable) image.getBackground();

image.post(new Runnable()

 {

            @Override

            public void run()

            {

                animationDrawable.start();

            }

        });

 

第四種方式啓動動畫:(在Activity啓動時會自動運行動畫)

ImageView image = (ImageView) findViewById(R.id.animation_view);

image.setBackgroundResource(R.anim.oldsheep_wait);

        animationDrawable = (AnimationDrawable) image.getBackground();

 

@Override

    public void onWindowFocusChanged(boolean hasFocus)

    {

        animationDrawable.start();

        super.onWindowFocusChanged(hasFocus);

    }

這個ad.start不能直接寫在onClickonStartonResume裏面,是無效的,無法啓動動畫,只能寫在比如事件監聽當中

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章