如何添加动画以最小化JavaFX



我使用的是AnimateFX库,它只是节点动画的编译。

AnimationFX fx = new ZoomOut(background);
fx.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
//background.setScaleX(1.0D);
//background.setScaleY(1.0D);
//background.setScaleZ(1.0D);
//background.setOpacity(1.0D);
// -- some wait function --
Stage stage = (Stage) background.getScene().getWindow();
stage.setIconified(true);
}
});
fx.play();

这个块导致窗口最小化,但现在窗口既透明又微小。

如果我取消注释背景节点转换并注释stage.setIconified(true),则动画将以窗口恢复到完全大小而结束,正如预期的那样。

但是,如果我在不注释stage.setIconified(true)的情况下取消注释后台节点转换,则程序将在不运行后台节点转换的情况下最小化。

我认为这是一些同步问题,但在// -- some wait function --区域设置中添加等待函数只会导致程序在没有运行后台节点转换的情况下等待,然后最小化。

有点困惑为什么会发生这种情况。

编辑

下面是一些可运行的代码,它复制了这个问题。我发现,如果我用Button替换ImageView,并将事件更改为按钮操作,那么问题就不再发生。

package app;
import animatefx.animation.AnimationFX;
import animatefx.animation.ZoomOut;
import javafx.fxml.FXML;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Controller {
@FXML
VBox background;
@FXML
ImageView minimize;
@FXML
public void onMinimizeClicked(MouseEvent mouseEvent) {
AnimationFX fx = new ZoomOut(background);
fx.setSpeed(0.75D);
fx.setResetOnFinished(true);
Stage stage = (Stage) background.getScene().getWindow();
fx.setOnFinished(actionEvent -> stage.setIconified(true));
fx.play();
}
}

编辑#2

这是我的主舱

package app;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.initStyle(StageStyle.TRANSPARENT);
Scene scene = new Scene(root);
scene.setFill(Color.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

这是我的示例.fxml文件

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<VBox fx:id="background" prefHeight="500.0" prefWidth="400.0" style="-fx-background-color: transparent" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="app.Controller">
<AnchorPane fx:id="content" prefHeight="464.0" prefWidth="500.0" style="-fx-background-color: #3D4956&#10;" VBox.vgrow="ALWAYS">
<children>
<ImageView fx:id="minimize" fitHeight="150.0" fitWidth="200.0" layoutX="100.0" layoutY="175.0" onMouseClicked="#onMinimizeClicked" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>
</VBox>

我不知道这种行为的原因,但我设法通过在FXML中的ImageView内部添加图像来解决它

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<VBox fx:id="background" prefHeight="500.0" prefWidth="400.0" style="-fx-background-color: transparent" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="sample.Controller">
<AnchorPane fx:id="content" prefHeight="464.0" prefWidth="500.0" style="-fx-background-color: #3D4956&#10;" VBox.vgrow="ALWAYS">
<children>
<ImageView fx:id="minimize" fitHeight="150.0" fitWidth="200.0" layoutX="100.0" layoutY="175.0" onMouseClicked="#onMinimizeClicked" pickOnBounds="true" preserveRatio="true" >
<Image url="file:image/iamge.png"/>
</ImageView>
</children>
</AnchorPane>
</VBox>

最新更新