Java – There is a 1-second delay between two animations

There is a 1-second delay between two animations… here is a solution to the problem.

There is a 1-second delay between two animations

I want to build a splash screen for android where the logo is animated twice:

  • Fly from the left to the middle
  • After 1 second, fly right from the middle

The first thing is good :

Animation animLeft2Center = AnimationUtils.loadAnimation(this, R.anim.translate_left_to_center);
mLogo.startAnimation(animLeft2Center);

But I didn’t let the second animation work.

Animation animCenter2Right = AnimationUtils.loadAnimation(this, R.anim.translate_center_to_right);
mLogo.startAnimation(animCenter2Right);

How do I set a 1 second delay between the two and then start the second animation?

I can’t find anything like setStartDelay, and it doesn’t trigger two animations in turn.

Solution

Try to do this:

Animation animLeft2Center = AnimationUtils.loadAnimation(this, R.anim.translate_left_to_center);

mLogo.startAnimation(animLeft2Center);  
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    Do something after 1 second
      Animation animCenter2Right = AnimationUtils.loadAnimation(this, R.anim.translate_center_to_right);
      mLogo.startAnimation(animCenter2Right);
  }
}, 1000);

Related Problems and Solutions