本文介绍了将Node的动画添加到VBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一个解决方案,通过淡入或翻转或任何动画添加节点。
感谢您的帮助。
I am looking for a solution to add node's with fade-in or flipping or any animation.Thanks for any help.
我发现了同样的问题,但没有任何进一步的帮助:
I found the same question but without any further help here: Animated component adding-delete in a VBox
推荐答案
正如珠宝已经提到过的,你会使用Transitions。
以下是如何为您的用例执行此操作的示例:
As jewelsea already mentioned, you would use Transitions.Here is an example of how to do it for your use case :
public static void addFadingIn(final Node node, final Group parent) {
final FadeTransition transition = new FadeTransition(Duration.millis(250), node);
transition.setFromValue(0);
transition.setToValue(1);
transition.setInterpolator(Interpolator.EASE_IN);
parent.getChildren().add(node);
transition.play();
}
public static void removeFadingOut(final Node node, final Group parent) {
if (parent.getChildren().contains(node)) {
final FadeTransition transition = new FadeTransition(Duration.millis(250), node);
transition.setFromValue(node.getOpacity());
transition.setToValue(0);
transition.setInterpolator(Interpolator.EASE_BOTH);
transition.setOnFinished(finishHim -> {
parent.getChildren().remove(node);
});
transition.play();
}
}
或者使用Java8的更实际的实现:
Or a more gerneral implementation using Java8 :
public static void addAnimating(final Node node, final Group parent,
final Supplier<Animation> animationCreator) {
parent.getChildren().add(node);
animationCreator.get().play();
}
public static void removeAnimating(final Node node, final Group parent,
final Supplier<Animation> animationCreator) {
if (parent.getChildren().contains(node)) {
final Animation animation = animationCreator.get();
animation.setOnFinished(finishHim -> {
parent.getChildren().remove(node);
});
animation.play();
}
}
您可以在上面找到完整的示例
You can find a full example on https://gist.github.com/bugabinga/9576634
这篇关于将Node的动画添加到VBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!