2011-11-06 58 views
1

我試圖展示兩種不同的動畫。它與第一個工作正常。但是,當我停止第一個動畫並用第二個動畫(然後調用start())第二次調用setBackgroundResource()時,它僅顯示第一幀。 是否有可能兩次調用setBackgroundResource()來設置不同的動畫?使用不同的幀動畫調用setBackgroundResource()兩次

public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
    if (hasFocus) { 
     ImageView imgView= (ImageView) findViewById(R.id.imgView); 
     imgView.setBackgroundResource(R.anim.animation1); 
     AnimationDrawable frameAnimation = (AnimationDrawable) imgView 
       .getBackground(); 

     frameAnimation.setCallback(imgView); 
     frameAnimation.start(); 


      //Schedule the timer 

     Timer timer = new Timer(); 
     timer.schedule(new TimerTask() { 

       @Override 
       public void run() { 
        showAnimation2(); 
        } 
     }, 20000); 
    }//if hasFocus 

} 

/** 
* Show second animation 
*/ 
public void showAnimation2(){ 
    ImageView imgView= (ImageView) findViewById(R.id.imgView); 

    AnimationDrawable frameAnimation = (AnimationDrawable) imgView 
       .getBackground(); 
    // stop first animation 
    frameAnimation.stop(); 

    imgView.setBackgroundResource(R.anim.animation2); 
    frameAnimation.setCallback(imgView); 

    //start second animation 
    frameAnimation.start(); 
    // here I see the first frame of second animation only 
} 

我也試圖與調用setVisible(...)玩,但它並不能幫助

+0

向我們展示一些代碼... –

+0

我將代碼添加到問題 – gflower

+0

我會appriciate任何幫助因爲我堅持這個問題! – gflower

回答

0

我用兩個ImageView的和它們之間的切換解決了這個問題。我使用setVisibility()方法隱藏第一個動畫的一個視圖並顯示第二個視圖。

相關問題