0

I have an ImageButton in android which rotates when clicked. The issue is it does not finish rotating when the user taps it and proceeds to the new Activity on the next line. I have tried Thread.sleep(..) and wait(..) but putting RotateAnimation(..) along with these actually sleeps before the animation starts.

I need the animation to actually finish and then proceed to the startActivity(new Intent(..))

Here's the code

 amazingPicsButton.setOnClickListener(new View.OnClickListener() {          
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            amazingPicsSound = createRandButSound();
            amazingPicsSound.start();               
            rotateAnimation(v);

            startActivity(new Intent("com.jasfiddle.AmazingInterface.AMAZINGPICS"));            
        }
    });         
}


/** function that produces rotation animation on the View v.
 * Could be applied to button, ImageView, ImageButton, etc.
 */
public void rotateAnimation(View v){
    // Create an animation instance
    Animation an = new RotateAnimation(30, 360, v.getWidth()/2, v.getHeight()/2);

    // Set the animation's parameters
    an.setDuration(20);               // duration in ms
    an.setRepeatCount(10);                // -1 = infinite repeated
  //  an.setRepeatMode(Animation.REVERSE); // reverses each repeat
    an.setFillAfter(true);               // keep rotation after animation

    v.setAnimation(an);
    // Apply animation to the View

}
jmishra
  • 2,086
  • 2
  • 24
  • 38

2 Answers2

0

You never ask to your app to wait the end of the animation to start the new activity. See http://developer.android.com/reference/android/view/animation/Animation.html#setAnimationListener(android.view.animation.Animation.AnimationListener)

to learn how use AnimationListener

VinceFR
  • 2,551
  • 1
  • 21
  • 27
0

Animation is an asynchronous process, therefore if you want animation to finish before proceeding, then you need to add animation listener and execute your next line of code when the animation completes:

amazingPicsButton.setOnClickListener(new View.OnClickListener() {          
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        amazingPicsSound = createRandButSound();
        amazingPicsSound.start();
        rotateAnimation(v);
    }
});         

and then

public void rotateAnimation(View v){
    // Create an animation instance
    Animation an = new RotateAnimation(30, 360, v.getWidth()/2, v.getHeight()/2);

    // Set the animation's parameters
    an.setDuration(20);               // duration in ms
    an.setRepeatCount(10);                // -1 = infinite repeated
    //  an.setRepeatMode(Animation.REVERSE); // reverses each repeat
    an.setFillAfter(true);               // keep rotation after animation

    an.addAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                startActivity(new Intent("com.jasfiddle.AmazingInterface.AMAZINGPICS"));
            }
        });

    v.setAnimation(an);

}

Note that startActivity call is not inside the onAnimationEnd method of the AnimationListener instead of being after setting the animation onto the view.

Aleks G
  • 56,435
  • 29
  • 168
  • 265