2013-07-25 30 views
0

尊敬的程序員!將處理程序與ViewFlipper的動畫結合使用

我想爲5個viewflipper孩子之間的隨機時間間隔隨機翻轉應用程序的動畫菜單。在每次翻轉之間,我想根據逐幀可繪製的方式「插入」動畫。到目前爲止,我能夠基於動畫方法的調用是在「運行」方法的開始處還是在結尾處顯示隨機翻轉或動畫。我無法弄清楚如何確保它在處理程序的每次迭代中「在」之間執行。

下面是代碼:

public class BTG extends Activity { 

private ViewFlipper fliptest; 
private Handler testHandler = new Handler(); 
private Random mRand = new Random(); 
private Random timerMenu = new Random(); 
int randomTime; 
AnimationDrawable menuAnimation; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_btg); 

    fliptest = (ViewFlipper) findViewById(R.id.menuFlipper); 
    testHandler.postDelayed(mFlip, randomTime); 


} 

private Runnable mFlip = new Runnable() { 

    @Override 
    public void run() { 
     //if this call is at the beginning, the menu only flips between the 5 first 
     //children of the viewflipper and the animation is never shown 
     startAnimation(); 

     randomTime = (timerMenu.nextInt(6) + 1) * 2000; 
     System.out.println("executes the run method " + randomTime); 
     fliptest.setDisplayedChild(mRand.nextInt(5)); 
     testHandler.postDelayed(this, (mRand.nextInt(6)+ 1) * 2000); 
     //if this call is at the end, the menu only displays the animation 
     //which launches itself after a random time as set in the handler 
     //startAnimation(); 


    } 

    class Starter implements Runnable { 
      public void run() { 
       menuAnimation.start(); 
      } 
     } 

    private void startAnimation() { 
     System.out.println("The start Animation method is run"); 
     fliptest.setDisplayedChild(5); 
     menuAnimation = new AnimationDrawable(); 
     menuAnimation.addFrame(getResources().getDrawable(R.drawable.animate_menu_trans1), 100); 
     menuAnimation.addFrame(getResources().getDrawable(R.drawable.animate_menu_trans2), 100); 
     menuAnimation.addFrame(getResources().getDrawable(R.drawable.animate_menu_trans3), 100); 
     menuAnimation.addFrame(getResources().getDrawable(R.drawable.animate_menu_trans1), 100); 
     menuAnimation.addFrame(getResources().getDrawable(R.drawable.animate_menu_trans2), 100); 
     menuAnimation.addFrame(getResources().getDrawable(R.drawable.animate_menu_trans3), 100); 
     menuAnimation.setOneShot(true); 

      ImageView imageView = (ImageView) findViewById(R.id.menu_animation); 
      imageView.setImageDrawable(menuAnimation); 
      imageView.post(new Starter()); 

    }  
}; 

回答

0

好了,玩了,並花費數小時#2之後,我找到了一個可行的解決我的問題。這是最終的代碼。 請隨時提出一個「更好」的編程替代方案,因爲這只是我的新手修復!

public class BTG extends Activity { 

private ViewFlipper fliptest; 
private Handler menuHandler = new Handler(); 
private Random mRand = new Random(); 
private Random timerMenu = new Random(); 
int randomTime; 
AnimationDrawable menuAnimation; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_btg); 
    fliptest = (ViewFlipper) findViewById(R.id.menuFlipper); 
    menuHandler.postDelayed(mFlip, randomTime); 
} 


private Runnable mFlip = new Runnable() { 

     @Override 
     public void run() { 

     randomTime = (timerMenu.nextInt(6) + 1) * 2000; 
      System.out.println("executes the run method " + randomTime); 
      menuHandler.postDelayed(this, randomTime); 
      startAnimation(); 
     } 
}; 


class Starter implements Runnable { 
     public void run() { 
      menuAnimation.start(); 
     } 
} 

    private void startAnimation() {  

     System.out.println("The start Animation method is run"); 

     fliptest.setDisplayedChild(5); 
     menuAnimation = new AnimationDrawable(); 
     menuAnimation.addFrame(getResources().getDrawable(R.drawable.animate_menu_trans1), 100); 
     menuAnimation.addFrame(getResources().getDrawable(R.drawable.animate_menu_trans2), 100); 
     menuAnimation.addFrame(getResources().getDrawable(R.drawable.animate_menu_trans3), 100); 
     menuAnimation.addFrame(getResources().getDrawable(R.drawable.animate_menu_trans1), 100); 
     menuAnimation.addFrame(getResources().getDrawable(R.drawable.animate_menu_trans2), 100); 
     menuAnimation.addFrame(getResources().getDrawable(R.drawable.animate_menu_trans3), 100); 
     menuAnimation.setOneShot(true); 

     ImageView imageView = (ImageView) findViewById(R.id.menu_animation); 
     imageView.setImageDrawable(menuAnimation); 
     imageView.post(new Starter()); 

     Thread timer = new Thread(){ 
      public void run(){ 
       try{ 
        sleep(700); 
       }catch (InterruptedException e){ 
         e.printStackTrace(); 
       }finally{ 
        System.out.println("the thread sleep works"); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          fliptest.setDisplayedChild(mRand.nextInt(5)); 
         } 
        }); 
       } 
      } 

     }; 
     timer.start(); 
    }  


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.beat_the_game, menu); 
    return true; 
} 

}