Animation 将处理程序与带有ViewFlipper的动画结合使用

Animation 将处理程序与带有ViewFlipper的动画结合使用,animation,random,handler,children,viewflipper,Animation,Random,Handler,Children,Viewflipper,你好,亲爱的程序员 我正在尝试为一个应用程序制作一个动画菜单,该应用程序在5个viewflipper子对象之间以随机时间间隔随机翻转。在每次翻转之间,我想“插入”一个基于逐帧可绘制的动画。到目前为止,我能够根据对动画方法的调用是在“run”方法的开头还是结尾显示随机翻转或动画。我不知道如何确保它在处理程序每次迭代的“中间”执行 代码如下: public class BTG extends Activity { private ViewFlipper fliptest; private Hand

你好,亲爱的程序员

我正在尝试为一个应用程序制作一个动画菜单,该应用程序在5个viewflipper子对象之间以随机时间间隔随机翻转。在每次翻转之间,我想“插入”一个基于逐帧可绘制的动画。到目前为止,我能够根据对动画方法的调用是在“run”方法的开头还是结尾显示随机翻转或动画。我不知道如何确保它在处理程序每次迭代的“中间”执行

代码如下:

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());

    }    
};

好的,在玩了几小时Stackoverflow之后,我找到了一个可行的解决方案。这是最后的代码。 请随时提出一个“更好”的编程选择,因为这只是我的新手修复

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;
}
}