带有动画 gif 的 JAVAFX SplashScreen

JAVAFX SplashScreen with an animated gif

是否可以在我的启动画面中添加动画 gif 或视频。

这是我的代码

Launcher class: ** 将所有需要的文件编辑到 运行 简单项目。

package com.example;
import javafx.application.Application;
public class Launcher 
{    
    public static void main(String[] args) {  
        System.setProperty("javafx.preloader", "com.example.SplashScreen");      
        Application.launch(Main.class);        
    }
}

主要class

package com.example;
import javafx.application.Application;
import javafx.application.Preloader.StateChangeNotification;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application{

Stage stage;
Scene scene;

@Override
public void start(Stage primaryStage) throws Exception {
    
    try {
        Thread.sleep(5000);
        this.stage = primaryStage;
        AnchorPane pane;
        FXMLLoader mainLoader = new FXMLLoader(getClass().getClassLoader().getResource("layouts/main.fxml"));   
        pane = mainLoader.load();                                               
        scene = new Scene(pane, 600, 445);
        primaryStage.setScene(scene);
        
        primaryStage.show();
        notifyPreloader(new StateChangeNotification(StateChangeNotification.Type.BEFORE_START));
        
    } 
    catch (final Exception e) {
        e.printStackTrace();
    }        
}

}

SplashScreenClass

package com.example;

import javafx.application.Preloader;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class SplashScreen extends Preloader {

    Stage stage;

@Override
public void start(Stage stage) throws Exception {
    this.stage = stage;
    StackPane root = (StackPane) FXMLLoader.load(getClass().getClassLoader().getResource("layouts/splash.fxml"));        
    Scene scene = new Scene(root, 690, 380,true, SceneAntialiasing.BALANCED);
    scene.setFill(Color.TRANSPARENT);
    stage.initStyle(StageStyle.UNDECORATED);
    stage.setScene(scene);
    stage.show();
    
}

@Override
public void handleApplicationNotification(PreloaderNotification pn) {
    if (pn instanceof StateChangeNotification) {
        //hide after get any state update from application
        stage.hide();
    }
}  

}

所以基本上这就是所有需要的 java 代码。我添加了 5 秒的睡眠来模拟从后端加载。这将生成启动画面,但 不是动画。如果我不隐藏闪屏阶段,gif 将在主阶段加载后动画化。

splash fxml

<StackPane xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="380.0" prefWidth="690.0">
     <children>
        <ImageView fitHeight="374.0" fitWidth="686.0" pickOnBounds="true" preserveRatio="true">
           <image>
              <Image url="@../test.gif" />
           </image>
        </ImageView>
     </children>
  </StackPane>

最后是main.fxml

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" >
   <children>      
      <Label layoutX="238.0" layoutY="224.0" prefHeight="63.0" prefWidth="125.0" text="Label" />
   </children>
</AnchorPane>

感谢@jewelsea 的评论,我最终修改了主要 class 以在后台线程中执行任务,然后,当一切准备就绪时,我展示了主应用程序的舞台。

[...]
    
    try {
        final Task<Integer> task = new Task<Integer>() {
            @Override
            protected Integer call() throws Exception {
                Thread.sleep(5000);
                Platform.runLater(new Runnable() {
                    public void run() {
                        showStage();
                    }
                }); 
                return 0;
            }
        };
        
        Thread thread = new Thread(task);
        thread.start();
    } catch (final Exception e) {
        e.printStackTrace();
    }
}

public void showStage() {
    stage.show();
    notifyPreloader(new StateChangeNotification(StateChangeNotification.Type.BEFORE_START));
}

GIF 是动画的,启动画面将在所有后台任务完成后加载。