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不能直接寫在onClick,onStart,onResume裡面,是無效的,無法啟動動畫,只能寫在比如事件監聽當中