我有点不熟悉JavaFX(来自Swing)并且倾向于使用StackPanes内部的几个fxml子面板,其中一个不同的部分在某个按钮点击或菜单选择时被带到前面()。是否可以通过淡出/淡入过渡来制作动画?我一直在谷歌搜索几天,但找不到与这个特殊情况有关的任何事情。
toFront()
只需将节点移动到其父级子列表的末尾即可工作(子列表的顺序决定了z顺序)。所以“最前面的节点”就是列表末尾的节点。您可以使用Bindings.valueAt(...)
和Bindings.size(...)
的组合创建包含此节点的可观察值。
ObjectBinding<Node> frontNode = Bindings.valueAt(stackPane.getChildren(),
Bindings.size(stackPane.getChildren()).subtract(1));
或者,对于替代实现:
ObjectBinding<Node> frontNode = new ObjectBinding<Node>() {
{ bind(stackPane.getChildren()); }
@Override
protected Node computeValue() {
if (stackPane.getChildren().isEmpty()) return null ;
return stackPane.getChildren().get(stackPane.getChildren().size() - 1) ;
}
}
然后你可以注册一个监听器,它将旧值淡出并淡化新值。以下内容应该为您提供一个起点,您可以从中开始工作。
frontNode.addListener((obs, oldNode, newNode) -> {
SequentialTransition fadeOutIn = new SequentialTransition();
if (oldNode != null) {
FadeTransition fadeOut = new FadeTransition(Duration.seconds(1), oldNode);
fadeOut.setToValue(0);
fadeOutIn.getChildren().add(fadeOut);
}
if (newNode != null) {
FadeTransition fadeIn = new FadeTransition(Duration.seconds(1), newNode);
fadeIn.setFromValue(0);
fadeIn.setToValue(1);
fadeOutIn.getChildren().add(fadeIn);
}
fadeOutIn.play();
});
根据应用程序中的设置方式,您可能需要将所有堆栈窗格子项的不透明度初始化为零。
请注意,所有这些代码需要的是对堆栈窗格的引用。