Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animation BEFORE activity change

Tags:

android

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?

like image 301
Stephan Avatar asked Nov 30 '10 23:11

Stephan


1 Answers

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
like image 185
CaseyB Avatar answered Sep 27 '22 16:09

CaseyB