等待动画完成

时间:2012-03-01 15:22:53

标签: android android-animation

我试图从其他问题中了解该怎么做但我被卡住了。

我正在使用动画来动画玩家所做的动作(纸牌游戏)。 问题是,当动画发生时,它会继续并开始下一个玩家的移动。

这是执行动画的对象

final class MyAnimation implements AnimationListener {

    private ImageView  animationSource;
    private ImageView  animationTarget;
    private int animationDuration; 
    private int animationDelay;
    private int cardResource;

    MyAnimation(ImageView  animationSource, ImageView animationTarget, int cardResource, int animationDuration, 
             int animationDelay){
        this.animationSource = animationSource;
        this.animationTarget = animationTarget;
        this.cardResource = cardResource;
        this.animationDuration = animationDuration;
        this.animationDelay = animationDelay;
    }

    public void animate(){
        int sourceCoords[] = {0,0};
        int targetCoords[] = {0,0};
        int xDelta;
        int yDelta;
        animationSource.getLocationOnScreen(sourceCoords);
        animationTarget.getLocationOnScreen(targetCoords);
        xDelta = targetCoords[0] - sourceCoords[0];
        yDelta = targetCoords[1] - sourceCoords[1];
        TranslateAnimation cardDealingAnimation = new TranslateAnimation(0,  xDelta, 0, yDelta);
        cardDealingAnimation.setAnimationListener(this);
        cardDealingAnimation.setDuration(animationDuration);
        cardDealingAnimation.setStartOffset(animationDelay);
        animationSource.startAnimation(cardDealingAnimation);
    }
    @Override
    public void onAnimationEnd(android.view.animation.Animation animation) { 
        animationTarget.setImageResource(cardResource);
        animationTarget.setVisibility(View.VISIBLE);
        inAnimation = false;
                    completionMonitor.notifyAll();
    }

    @Override
    public void onAnimationRepeat(android.view.animation.Animation animation) {

    }

    @Override
    public void onAnimationStart(android.view.animation.Animation animation) {
        inAnimation = true;
    }

}

以及调用它的代码:

private static void animate(final ImageView  animationSource, final ImageView  animationTarget, final int cardResource,
                           final int animationDuration, final int animationDelay)
{

    synchronized(completionMonitor){
    runOnUiThread(new Runnable(){

    @Override
    public void run() {
    MyAnimation anim = (new CardAnimator()).new MyAnimation(animationSource,animationTarget, cardResource, 
                                                            animationDuration, animationDelay);

        anim.animate();
    }
    });
    }
    synchronized(completionMonitor){        
    try {
        completionMonitor.wait();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

}

我实际上需要它等到动画完成才返回。

我尝试过使用同步,但是无法正确使用。

要么冻结我,要么崩溃。

0 个答案:

没有答案