为什么我的JavaFX ImageView没有使用淡入淡出过渡显示/隐藏?

时间:2017-04-10 17:58:57

标签: javafx imageview fade transitions

我正在尝试添加淡入淡出转换到我的登录和注销ImageViews,我尝试使用与JFeonix Hamburger相同的模式,我也在oracle文档中使用了一些教程,但是,我的ImageViews隐藏了没有淡入淡出过渡立即显示。我错过了什么?

下面是我的代码:

@FXML
    private JFXHamburger menu;
    @FXML
    private ImageView login;
    @FXML
    private ImageView logout;

    private LoginLogic logic;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        final HamburgerSlideCloseTransition slideCloseTransition = new HamburgerSlideCloseTransition(menu);
        slideCloseTransition.setRate(-1);

        FadeTransition t = new FadeTransition(new Duration(3000), login);
        FadeTransition t1 = new FadeTransition(new Duration(3000), logout);

        t.setCycleCount(1);
        t1.setCycleCount(1);

        t.setAutoReverse(false);
        t1.setAutoReverse(false);

        t.setRate(-1);
        t1.setRate(-1);

        menu.addEventHandler(MouseEvent.MOUSE_PRESSED, (MouseEvent event) -> {
            t1.setRate(t1.getRate() * -1);
            t.setRate(t.getRate() * -1);
            slideCloseTransition.setRate(slideCloseTransition.getRate() * -1);
            slideCloseTransition.play();
            t.play();
            t1.play();
            login.setVisible(!login.isVisible());
            logout.setVisible(!logout.isVisible());
        });
    }

感谢。

1 个答案:

答案 0 :(得分:1)

您没有使用FadeTransition更改节点的外观,因为您仍在使用fromValuetoValuebyValue的默认值。< / p>

这意味着您只需切换开启和关闭的可见性......

通常只有FadeTransition修改不透明度。

实施例

@Override
public void start(Stage primaryStage) {
    Button btn = new Button("fade in/out");

    ImageView image = new ImageView("https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png");
    image.setOpacity(0);

    VBox root = new VBox(10, btn, image);
    root.setPadding(new Insets(10));

    FadeTransition t = new FadeTransition(Duration.seconds(3), image);
    t.setCycleCount(1);
    t.setAutoReverse(false);
    t.setRate(-1);
    t.setFromValue(0);
    t.setToValue(1);

    btn.setOnAction(evt -> {
        t.setRate(t.getRate() * -1);
        t.play();
    });

    Scene scene = new Scene(root);

    primaryStage.setScene(scene);
    primaryStage.show();
}