gpt4 book ai didi

带有动画 gif 的 JAVAFX SplashScreen

转载 作者:行者123 更新时间:2023-12-04 17:12:06 25 4
gpt4 key购买 nike

是否可以在我的初始屏幕中显示动画 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();
}
}

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 秒的 sleep 来模拟从后端加载。这将生成启动画面,但 gif不是动画。如果我不隐藏 splashscreen 阶段,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>

最后是ma​​in.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 是动画的,启动画面将在所有后台任务完成后加载。

关于带有动画 gif 的 JAVAFX SplashScreen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69237024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com