2011-01-11 154 views
1

我寫一個遊戲應用。如果用戶按錯了按鈕,我想顯示一個動畫,當動畫完成時,完成()我的遊戲;Android的等待動畫活動之前完成結束

目前,完成被調用時我的動畫剛剛開始。我想要動畫完成。

下面是我的類,它實現AnimationListener

public final class DisplayNextView implements Animation.AnimationListener { 
    View changeView; 
    Drawable image; 

    public DisplayNextView(View parChangeView, Drawable parImage) { 
     this.changeView = parChangeView; 
     this.image = parImage; 
    } 

    public void onAnimationStart(Animation animation) { 
     changeView.post(new SwapViews(changeView, image)); 
     changeView.postDelayed(new SwapViews(changeView, null), 1000); 
    } 

    public void onAnimationEnd(Animation animation) { 

    } 

    public void onAnimationRepeat(Animation animation) { 

    } 
} 

這是我的一個啓動動畫

private void applyRotation(View parChangeView, Drawable image, float start, float end) { 
     // Find the center of image 
     final float centerX = parChangeView.getWidth()/2.0f; 
     final float centerY = parChangeView.getHeight()/2.0f; 

     // Create a new 3D rotation with the supplied parameter 
     // The animation listener is used to trigger the next animation 
     final Flip3DAnimation rotation = 
       new Flip3DAnimation(start, end, centerX, centerY); 
     rotation.setDuration(250); 
     rotation.setFillAfter(true); 
     rotation.setInterpolator(new AccelerateInterpolator()); 
     rotation.setAnimationListener(new DisplayNextView (parChangeView,image)); 
     parChangeView.startAnimation(rotation); 
     //return rotation; 
     } 

這是我的SwapView類,這是一個線程的主要活動方式

public final class SwapViews implements Runnable { 
    View changeView; 
    Drawable image; 

    public SwapViews(View parChangeView, Drawable parImage) { 
     this.changeView = parChangeView; 
     this.image = parImage; 
    } 

    public void run() { 
     final float centerX = changeView.getWidth()/2.0f; 
     final float centerY = changeView.getHeight()/2.0f; 
     Flip3DAnimation rotation; 
     changeView.setBackgroundDrawable(image); 
     //TODO should find a better way!! 
     if (image==null) 
      changeView.setBackgroundColor(Color.BLACK); 


     rotation = new Flip3DAnimation(-90, 0, centerX, centerY); 


     rotation.setDuration(250); 
     rotation.setFillAfter(true); 
     rotation.setInterpolator(new DecelerateInterpolator()); 

     changeView.startAnimation(rotation); 

    } 
} 

回答

0

你能更具體,這是什麼「動畫」是什麼?這是一個你自己玩框架或只是圖像或視頻的課程嗎?

,如果它是第一個最好的方法是創建一個回調方法,只是火,當動畫完成。

+0

我開始從我的主要活動的動畫。我使用動畫和動畫偵聽器的自定義類。我爲動畫修改了http://www.inter-fuser.com/2009/08/android-animations-3d-flip.html中的示例。動畫是一個圖像。 用戶點擊一個按鈕,如果「答案」是錯誤的,那麼卡翻轉,以顯示他的結果是錯誤的,然後我要完成的活動。但是,我的活動在動畫完成之前完成。另外,當用戶點擊右鍵時,卡片上顯示相同的動畫,顯示正確的結果。 – SingleMalt 2011-01-13 14:37:00

0

事情是這樣的:

public final class DisplayNextView implements Animation.AnimationListener { 
View changeView; 
Drawable image; 
boolean mIsCorrect; 
Activity mContext; 

public DisplayNextView(View parChangeView, Drawable parImage, 
     boolean isCorrect, Activity context) { 
    this.changeView = parChangeView; 
    this.image = parImage; 
    mIsCorrect = isCorrect; 
    mContext = context; 
} 

public void onAnimationStart(Animation animation) { 
    changeView.post(new SwapViews(changeView, image)); 
    changeView.postDelayed(new SwapViews(changeView, null), 1000); 
} 

public void onAnimationEnd(Animation animation) { 
    if (mIsCorrect == false) { 
     mContext.finish(); 
    } 
} 

public void onAnimationRepeat(Animation animation) { 

} 
} 

和你applyRotation應該通過猜測是否是正確的:

private void applyRotation(View parChangeView, Drawable image, float start, 
     float end, boolean isCorrect) { 
    // Find the center of image 
    final float centerX = parChangeView.getWidth()/2.0f; 
    final float centerY = parChangeView.getHeight()/2.0f; 

    // Create a new 3D rotation with the supplied parameter 
    // The animation listener is used to trigger the next animation 
    final Flip3DAnimation rotation = new Flip3DAnimation(start, end, 
      centerX, centerY); 
    rotation.setDuration(250); 
    rotation.setFillAfter(true); 
    rotation.setInterpolator(new AccelerateInterpolator()); 
    rotation.setAnimationListener(new DisplayNextView(parChangeView, image, isCorrect, this)); 
    parChangeView.startAnimation(rotation); 
    // return rotation; 
}