javafx - 带有动画 gif 的 JAVAFX SplashScreen
问题描述
是否可以在我的启动画面中显示动画 gif 或视频。
这是我的代码
启动器类: ** 已编辑所有需要的文件来运行简单的项目。
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);
}
}
主班
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();
}
}
}
闪屏类
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 将在主阶段加载后进行动画处理。
飞溅 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 评论,我最终修改了主类以在后台线程中执行任务,然后,当一切准备就绪时,我展示了主应用程序的阶段。
[...]
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 是动画的,所有后台任务完成后都会加载启动画面。
推荐阅读
- html - Internet Explorer 11 上未显示网站背景图片
- python-3.x - start_workspaces() 只接受关键字参数
- python - 列出具有多个条件的数据框中的理解
- android - Android Studio 不显示创建 Firestore 索引的链接
- android - 如何设置应用内更新的优先级?
- macos - 插入(虚拟测角仪)与运行的 MeshLab 版本不同的浮点精度
- javascript - 根据条件重新排列javascript数组
- android - MaterialCardView 包括所有孩子到位图
- java - 将对象添加到 ArrayList
- sql - 使用 TypeORM 在 Postgres 中回滚数据库更改