Android AnimationDrawable運行的幾種方式

1.注意事項

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

2.第一種:在事件監聽中start AnimationDrawable 下面一個例子舉例 當一個視圖樹將要繪製時產生事件

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; //注意此行返回的值
       }

};

3.第二種方式啓動動畫:(在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 "";
        }
}


4.第三種方式啓動動畫:(在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();
            }
        });

5.第四種方式啓動動畫:(在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);
    }






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