문제

I am trying to animate buttons like in the game of Simon, where it generates a random sequence of 4 buttons to press, displays the order though the button animation, then takes user input to match the sequence generated. I found a pause method using Handlers to use in android. I thought I captured the functionality correctly but instead of pausing each request, it paused at the beginning of execution then animates all the buttons at once so knowing the sequence is practically impossible when it flashed all three buttons at once. Here is what I have as far as the button animation goes.

//Send the buttons to animate, this shows the sequence that is stored to match

private void show(final Button first,final Button second,final Button third,final Button fourth)
   {
       int caseChoice = 0;

       for (int i = 0; i < currentCount; i++)
       {               
           caseChoice = matching[i];
           switch (caseChoice)
           {
           case 0:
               mHandler.postDelayed(new Runnable() {
                    public void run() {
                        animDelay(first);
                    }
                }, 6000);   
               break;
           case 1:
               mHandler.postDelayed(new Runnable() {
                    public void run() {
                        animDelay(second);
                    }
                }, 6000);
               break;
           case 2:
               mHandler.postDelayed(new Runnable() {
                    public void run() {
                        animDelay(third);
                    }
                }, 6000);
               break;
           case 3:
               mHandler.postDelayed(new Runnable() {
                    public void run() {
                        animDelay(fourth);
                    }
                }, 6000);                  
               break;

           }
       }
   }

   //used to delay button animation, the button it is sent (reusable)
   private void animDelay(Button theButton)
   {
       Animation animAlpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
       theButton.startAnimation(animAlpha);
   }
도움이 되었습니까?

해결책

Your current code iterates through, posting a delayed action to occur in 6 seconds time.

The loop iterates through very fast, so all the posts for "something to happen in 6 sec" are very close together.

If you want each successive action to be posted later you should do something like:

mHandler.postDelayed(new Runnable() {
                    public void run() {
                        animDelay(first);
                    }
                }, (i * 6000) );   
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top