Android - 在继续之前等待动画完成?

标签 android animation wait

我花了很长时间试图找出如何在继续代码之前等待动画完成。 到目前为止,我已经尝试过 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;
    }

最佳答案

在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;
    }

关于Android - 在继续之前等待动画完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31408862/

相关文章:

java - Android:设置为按钮背景的图像没有被缩小?

android - 集成TensorFlow和DJI SDK时无法合并dex

ios - swift 4 : Mask not working for UIImage?

jquery - 动画 css 不起作用

c - 为什么 "Cat"在我构建的 C-shell 中不起作用?

java - wait() 如何在 Java 中取回锁

android - Firebase 身份验证单元测试错误 No Firebase App

javascript - 如何创建无限改变不同背景颜色的循环动画

javascript - 如何等待工厂函数完成,然后在 Angular 中处理输出?

android - 什么是 Android Play 服务 SDK 级别?