Android - wait for animation to finish before continuing?

南楼画角 提交于 2020-01-04 14:36:23

问题


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

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

回答1:


write following line in onAnimationEnd method

 displayScore(score);

for example

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


来源:https://stackoverflow.com/questions/31408862/android-wait-for-animation-to-finish-before-continuing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!