【问题标题】:Android - wait for animation to finish before continuing?Android - 等待动画完成后再继续?
【发布时间】:2015-10-03 05:20:04
【问题描述】:

我花了很长时间试图找出如何在继续代码之前等待动画完成。 到目前为止,我已经尝试了 while(animation.hasEnded() == False),使用 Thread.sleep,使用计时器,但似乎无法正常工作。

出现了好几次的事情是向动画添加一个动画侦听器,我已经尝试过,但不知道如何进一步推进。

我创建动画,启动动画,然后有一行代码,我只想在动画完成后执行:

    Animation moveDown = allAnimStuff(duration); //creates animation 

    image.startAnimation(moveDown); // Start the animation
    displayScore(score); // wait for animation to finish before executing this line

这里是用于创建动画的 allAnimStuff(duration) 方法:

private Animation allAnimStuff(final long duration) {

        Animation moveDown = new TranslateAnimation(image.getX(), image.getX(), (-image.getHeight()), pxHeight - image.getHeight() / 2); //creates animation
        moveDown.setDuration(duration);
        moveDown.setFillAfter(false);

        moveDown.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                 //some code to make it wait here? 
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                image.setY((pxHeight / 2 - image.getHeight()));

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });


        return moveDown;
    }

【问题讨论】:

    标签: android animation wait


    【解决方案1】:

    在 onAnimationEnd 方法中写入以下代码

     displayScore(score);
    

    例如

    private Animation allAnimStuff(final long duration) {
    
            Animation moveDown = new TranslateAnimation(image.getX(), image.getX(), (-image.getHeight()), pxHeight - image.getHeight() / 2); //creates animation
            moveDown.setDuration(duration);
            moveDown.setFillAfter(false);
    
            moveDown.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                     //some code to make it wait here? 
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
                    image.setY((pxHeight / 2 - image.getHeight()));
                 displayScore(score);
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
    
    
            return moveDown;
        }
    

    【讨论】:

    • 如果您的问题得到解决,请接受答案。
    • 目前可行,但如果我想在动画完成后运行更多代码行,我是否必须将整个代码放在 onAnimationEnd 方法中?
    • 我在评论中找到了解决问题的方法,非常感谢简单的解决方案!
    猜你喜欢
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 2019-11-01
    • 2021-05-06
    • 2021-05-21
    • 2014-02-03
    相关资源
    最近更新 更多