简体   繁体   中英

How do I call a method only after an animation is done in java?

I want to call a method after I animated a dice

Here is my code:

//animation:

public void rollDice(){

    diceImageView.animate()
            .rotation(1800)
            .setDuration(2000);

//method I want to call:

    checkNumber();

}

When I call rollDice() , you can't see the animation, it just skips the animation and calls the method checkNumber() .

How do I wait until the animation is done to call the method checkNumber() ?

Well, that usually is done with callbacks, as I think that that animation runs in a seperate thread. What the API of that animation should do is declare a callback interface:

public interface AnimationCallback {
    public void onAnimationDone();
}

And add a method in its API that registers that class with instance and then after the animation the callbacks get called. If it is not your animation API, then you might have to try sleeping the current thread at the duration of that animation, however that seems really bad practice to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM