0

Need To Do One Animation, Sleep 1000 Then Do The Next Sleep 1000 And So On, Instead It Sleeps For The Entire Time Then Plays All Animations At Once. No Idea What Im Doing.

Tried Timers, Running The Animation Before The tread.sleep And Using A While Loop Instead Of A For.

private void playLaunchAnimation()
{

final Animation animation = AnimationUtils.loadAnimation(this, R.anim.fadein);

    for(int i=0; i < buttons.size();i++)
    {
        try {
            Thread.sleep(1000);
            buttons.get(i).startAnimation(animation);

        } catch (Exception e){
        /*
main declares that it throws InterruptedException. This is an exception that sleep throws when another thread interrupts the current thread while sleep is active. Since this application has not defined another thread to cause the interrupt, it doesn't bother to catch InterruptedException.

        */

        }

    }

}
Miguel
  • 2,019
  • 4
  • 29
  • 53
Grady
  • 1

3 Answers3

0

It sounds like you are calling Thread.Sleep from the User Interface thread. This will ultimately result in the entire User Interface freezing up for the duration of the sleep. What you really want is to launch sleep from a background thread.

For example:

AsyncTask.execute(new Runnable() {
   @Override
   public void run() {
      for(int i=0; i < buttons.size();i++)
      {
          try {
              Thread.sleep(1000);
              activity.runOnUiThread(new Runnable() {
                  buttons.get(i).startAnimation(animation);
              });
          } catch (Exception e){}
      }
});

Another way you could do this using post delay:

new android.os.Handler().postDelayed(
    new Runnable() {
        public void run() {
            buttons.get(i).startAnimation(animation);
            new android.os.Handler().postDelayed(this, 1000);
        }
    }, 
1000);
Dylan
  • 1,335
  • 8
  • 21
  • Wouldn't the `startAnimation` have to be done on the UI thread? `execute` target runs in background thread. –  Feb 15 '19 at 17:53
  • You could call forward to the main thread. I have no experience using animations on android. So I'm not certain. But even if it is, you can call something on the Main thread from the background thread. – Dylan Feb 15 '19 at 18:00
0

Hi, Make sure this code helps you

public static void main(String[] args) throws Exception {

        for(int i=0; i < 10;i++) {
            try {
                Thread.sleep(1000);
                System.out.println(i);
                if(i==9) {
                    i=0;
                }

            } catch (Exception e){
                System.out.println("error");
            }

        }

    }
0

(Answer assumes Android which wasn't entirely clear in OP.)

This is somewhat of a lazy way to do it - but maybe get you thinking. It would be more interesting to have the handler invoke the next handler so only one handler is declared - but that would be a little more work.

private void playLaunchAnimation() {

    final Animation animation = AnimationUtils.loadAnimation(this, R.anim.fadein);

    for(int i=0; i < buttons.size();i++)
    {
        // Create a handler on the UI thread to execute after a delay.
        // The delay is a function of loop index.
        // 
        // It may be necessary to declare buttons final - but your
        // OP did not list where it is defined.

        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
              buttons.get(i).startAnimation(animation);
            }
        }, ((i+1)*1000));

    }

}

References:

How to call a method after a delay in Android

Android basics: running code in the UI thread