Дождитесь завершения анимации - PullRequest
1 голос
/ 01 марта 2012

Я пытался понять из других вопросов, что делать, но я застрял.

Я использую анимацию, чтобы оживить ход игрока (карточная игра).Проблема в том, что во время анимации она продолжается и начинает следующий ход игрока.

Этот объект выполняет анимацию

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

}

Мне действительно нужно подождать, пока анимация не завершится, прежде чем вернуться.

Я пытался использовать синхронизацию, просто не могу сделать это правильно.

Она либо зависает на мне, либоаварии.

...