是否可以在StackPane中发送子项toFront()时应用淡入淡出转换?

时间:2017-12-27 23:10:51

标签: javafx-8

我对JavaFX(来自Swing)有点不熟悉,并倾向于使用StackPanes内部的几个fxml子面板,其中一个不同的部分在某个按钮点击或菜单选择时被带到前面() 。是否可以通过淡出/淡入过渡来制作动画?我已经谷歌搜索了几天,但无法找到与此特定案例相关的任何内容。

1 个答案:

答案 0 :(得分:0)

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

根据应用程序中的设置方式,您可能需要将所有堆栈窗格子项的不透明度初始化为零。

请注意,所有这些代码需要的是对堆栈窗格的引用。