2012-06-20 72 views
0

我有一个ImageButton在android中点击旋转。问题在于,当用户点击它并继续到下一行的新Activity时,它不会完成旋转。我尝试过Thread.sleep(..)wait(..),但是在动画开始之前将RotateAnimation(..)与这些实际睡眠一起。RotateAnimation不会等待按钮旋转之前启动活动

我需要的动画实际完成,然后进行startActivity(new Intent(..))

下面的代码

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 

} 

回答

0

动画是一个异步过程,因此,如果您想要的动画,然后再继续完成,那么你需要添加动画监听器和执行的代码的下一行动画完成时:

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

然后

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); 

} 

startActivity调用不在AnimationListeneronAnimationEnd方法中,而不是在将动画设置到视图之后。