同步我的Android动画

时间:2012-12-03 16:26:10

标签: android android-asynctask android-animation

我正在制作一款简单的游戏,比如一款名为Simon Game的游戏。它的作用是当我按下屏幕上的开始按钮时,它会随机创建4个按钮序列(绿色,红色,蓝色或黄色)。当随机生成器创建它们时,所选颜色被假设为闪烁,但闪烁动画全部发生在每个Tone播放后的结尾。我想在选择按钮时同步,任何人都可以帮助我吗?

private OnClickListener startListenerS = new OnClickListener() {
    public void onClick(View v) {

         random = new Random();
         counter = 0;

         sequenceComparison = new int[4];


         Animation animation = new AlphaAnimation(1, (float) 0.5); // Change alpha from fully visible to invisible
         animation.setDuration(10); // duration - half a second
         animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
         animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in

         Button buttonStart = (Button)findViewById(R.id.Button01);        
         buttonStart.setOnClickListener(startListener); // Register the onClick listener with the implementation above
         buttonStart.setBackgroundColor (Color.RED);  

         Button buttonStart1 = (Button)findViewById(R.id.Button02);        
         buttonStart1.setOnClickListener(startListener1); // Register the onClick listener with the implementation above
         buttonStart1.setBackgroundColor (Color.GREEN);

         Button buttonStart2 = (Button)findViewById(R.id.Button03);        
         buttonStart2.setOnClickListener(startListener2); // Register the onClick listener with the implementation above
         buttonStart2.setBackgroundColor (Color.YELLOW);

         Button buttonStart3 = (Button)findViewById(R.id.button1);        
         buttonStart3.setOnClickListener(startListener3); // Register the onClick listener with the implementation above
         buttonStart3.setBackgroundColor (Color.BLUE);

         sequenceArray = new int[sequenceTest];
         for(int i = 0; i < sequenceTest; i++) {
             int randnum = random.nextInt (sequenceTest) + 1;

             if(randnum == 1) {

                 sequenceArray[i] = 1; // THIS IS TO STORE THE CORRECT SEQUENCE IN THE ARRAY REPRESENTING RED BUTTON

                 //buttonStart.refreshDrawableState();
                 try {
                     playTone();
                     buttonStart.startAnimation(animation);
                     buttonStart.refreshDrawableState();
                     Thread.sleep(1000); // 1 second pause
                  } 
                  catch (InterruptedException e) {
                     e.printStackTrace();
                 } 
                 Thread.currentThread();
                 System.out.println("test Sequence Test: " + i + " " + sequenceArray[i]);
             }
             else if(randnum == 2) {
                 sequenceArray[i] = 2; // THIS IS TO STORE THE CORRECT SEQUENCE IN THE ARRAY REPRESENTING GREEN BUTTON
                 //buttonStart1.refreshDrawableState();
                 try {
                     playTone2();
                     buttonStart1.startAnimation(animation);
                     buttonStart1.refreshDrawableState();
                     Thread.sleep(1000); // 1 second pause
                  } 
                  catch (InterruptedException e) {
                     e.printStackTrace();
                  } 
                 Thread.currentThread();
                 System.out.println("test Sequence Test: " + i + " " + sequenceArray[i]);
             }

             else if(randnum == 3) {
                 sequenceArray[i] = 3; // THIS IS TO STORE THE CORRECT SEQUENCE IN THE ARRAY REPRESENTING YELLOW BUTTON
                 try {
                     playTone3();
                     buttonStart2.startAnimation(animation);
                     buttonStart2.refreshDrawableState();
                     Thread.sleep(1000);
                 } 
                 catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 Thread.currentThread();
                 System.out.println("test Sequence Test: " + i + " " + sequenceArray[i]);
             }
             else {
                 sequenceArray[i] = 4; // THIS IS TO STORE THE CORRECT SEQUENCE IN THE ARRAY REPRESENTING BLUE
                 try {
                     playTone4();
                     buttonStart3.startAnimation (animation);
                     buttonStart3.refreshDrawableState();
                     Thread.sleep(1000);
                 } 
                 catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 Thread.currentThread();
                 System.out.println("test Sequence Test: " + i + " " + sequenceArray[i]);
             }
         }
         // testing if the correct sequence is stored
         for (int i = 0; i < sequenceTest; i++) {
             System.out.println("Sequence Test: " + i + " " + sequenceArray[i]);

         }

    }
};
// *********************************** BUTTON CLICK LISTENER ***********************************
       private OnClickListener startListener = new OnClickListener() {
           public void onClick(View v) {

               Animation animation = new AlphaAnimation(1, (float) 0.5); // Change alpha from fully visible to invisible
               animation.setDuration(500); // duration - half a second
               animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
               animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in 

               Button buttonStart = (Button)findViewById(R.id.Button01);        
               buttonStart.setOnClickListener(startListener); // Register the onClick listener with the implementation above
               buttonStart.setBackgroundColor (Color.RED);  
               buttonStart.startAnimation(animation);

               sequenceComparison[counter] = 1;
               System.out.println("red clicked" + sequenceComparison[counter]);
                playTone();      
                if(sequenceComparison[counter] == sequenceArray[counter]){
                    System.out.println("correct sequence");
                }
                else {
                    System.out.println ("Sorry, you clicked wrong");
                }
                counter++;
           }
       };

2 个答案:

答案 0 :(得分:0)

首先,要注意在Thread.Sleep()中使用MainThread始终

我找到了一个符合您需求的先前答案:

Android button animations synchronize with timing

让我知道!

答案 1 :(得分:0)

首先,在animation.start()来电后,使用animation.set()启动动画实例。 然后,不使用startAnimation(animation)方法,而是使用setAnimation(animation)。 这将同步使用动画的任何视图。