Android-等待动画完成再继续吗? [英] Android - wait for animation to finish before continuing?

查看:467
本文介绍了Android-等待动画完成再继续吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've spend quite a while trying to find out how to wait for an animation to finish before continuing the code. So far I've tried while(animation.hasEnded() == False), using Thread.sleep, using a timer but can't seem to get anything to work.

出现了很多次的事情是在动画中添加了一个动画侦听器,我已经尝试过,但是不确定如何进一步发展。

Something that cropped up quite a few times was to add an Animation Listener to the animation, which I've tried but not sure how to progress this further.

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

I create the animation, start the animation and then have a line of code after, which I only want to be executed after the animation has finished:

    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)方法创建动画:

and here is the allAnimStuff(duration) method used to create the animation:

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方法中编写以下行

write following line in onAnimationEnd method

 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-等待动画完成再继续吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆