2012-06-20 104 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方法中,而不是在將動畫設置到視圖之後。