5

I'm trying to do something simple, but I can't understand why it's not working.
What I'm trying to do is: when I touch an ImageView, it will show an animation on it. And then, only when that animation ends it will start the new activity.
Instead, what happens is that the new activity starts right away and the animation is not shown.

Here is the animation xml:

<rotate android:interpolator="@android:anim/decelerate_interpolator"
    android:fromDegrees="-45"
    android:toDegrees="-10"
    android:pivotX="90%"
    android:pivotY="10%"
    android:repeatCount="3"
    android:fillAfter="false"
    android:duration="10000" />

And this is the code I use to call it:

public void onCreate( Bundle savedInstanceState )
{
    final ImageView ib = (ImageView)this.findViewById( R.id.photo );
    ib.setOnClickListener( new OnClickListener( ) {

        @Override
        public void onClick( View v )
        {
            Animation hang_fall = AnimationUtils.loadAnimation( Curriculum.this, R.anim.hang_fall );
            v.startAnimation( hang_fall );
            Intent i = new Intent( ThisActivity.this, NextActivity.class );
            ThisActivity.this.startActivity( i );
        }// end onClick
    } );
}// end onCreate

As you see I tried putting a loooong time for the animation, but it doesn't work. The NextActivity starts right away, it doesn't wait for the animation in ThisActivity to finish.
Any idea on why this happens?

Stephan
  • 1,858
  • 2
  • 25
  • 46

2 Answers2

4

That's because you're starting the intent and the animation at the same time. You need to start the intent after the animation is over, like this:

@Override
public void onClick( View v )
{
    Animation hang_fall = AnimationUtils.loadAnimation( Curriculum.this, R.anim.hang_fall );
    hang_fall.setAnimationListener(new Animation.AnimationListener()
        {
            public void onAnimationEnd(Animation animation)
            {
                Intent i = new Intent( ThisActivity.this, NextActivity.class );
                ThisActivity.this.startActivity( i );
            }

            public void onAnimationRepeat(Animation animation)
            {
                // Do nothing!
            }

            public void  onAnimationStart(Animation animation)
            {
                // Do nothing!
            }
        });
    v.startAnimation( hang_fall );
}// end onClick
CaseyB
  • 24,780
  • 14
  • 77
  • 112
  • Worked as a treat, thanks! I wish there was a little more documentation on some of this matters. :) – Stephan Dec 01 '10 at 09:06
  • http://developer.android.com/reference/packages.html is the best reference there is. Just search for any classes you want to know how to use. – CaseyB Dec 01 '10 at 14:01
2

Some solutions have been given in How to provide animation when calling another activity in Android?

Community
  • 1
  • 1
Dahlgren
  • 781
  • 4
  • 13