问题
How can i find out if there is already a transition running on the node - for example a FadeTransition ?
回答1:
You can always use the Transition statusProperty since Transition extends the Animation object. No need for another variable, you can simply:
FadeTransition fade = new FadeTransition()
fade.statusProperty().addListener(new ChangeListener<Status>() {
@Override
public void changed(ObservableValue<? extends Status> observableValue,
Status oldValue, Status newValue) {
if(newValue==Status.RUNNING)
System.out.println("Animation is still running");
}
});
fade.play();
I'm assuming you will track all of your animation, I don't believe there is a way to ask a node if it's in an animation. Anyway, you may want to check the documentation of the Node class, since it's a very powerful object. I hope it helped, cheers
回答2:
Note: I have not tried anything related to this myself. This is more of a sketch than a solution. Too long for a comment, that's why I write the outline here.
It seems to me you could create a boolean that is associated with the node ("transitionPlaying"), and each time a transition is started on the node you set it to true.
Then for each transition you define a behavior when it is finished:
yourTransition.setOnFinished(new EventHandler<ActionEvent>(){
public void handle(ActionEvent AE){
transitionPlaying = false;
}
Note that this merely answers your most basic question- it is hard to write anything more as I don't know why you want to know whether the transition is finished (If you want to do stuff after the transition is done, that opens a whole can of worms. Perhaps if a transition is playing you could add the next transition you want to run to a queue and then define setOnFinished to call the next transition in the queue when it is done.)
Ps. this solution has the restriction that at most one transition can play on a node at any moment.
Update to find whether all transitions on a node are done
You could use an int instead of a bool and for each transition that begins you increment the counter by one and when it is done you decrement it by one. This way you know that all transitions are done when the counter reads "0". This way my solution solves the problem of knowing whether all transitions on a node are done.
回答3:
Thank you for your help. I want to share the solution that i've choosen finally for my specific problem. Its the following: I wanted to fade a node on mouse click. When a lot of clicks appear, a new FadeTransition should only start when the current one is finished to prevent flickering or other phenomena.
My approach can be seen in the first line of the function:
@FXML
public void chartMouseClicked(MouseEvent event){
if (ft.getCurrentRate()!=0.0d) return;
ft.play();
}
In this case, ft is a private field of the type FadeTransition which is instantiated once within an "init" function. According to the JavaFX docs, getCurrentRate() returns 0.0 when the transition is paused or stopped. I don't need to pause the transition, so this is the way to go for me - i'm just checking the value and let my transition play again when it's stopped.
来源:https://stackoverflow.com/questions/12663730/how-to-find-out-if-a-transition-is-already-running-on-a-node