显示预载器和载物台之间的延迟 [英] Delay between Preloader and Stage shown

查看:69
本文介绍了显示预载器和载物台之间的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,它使用简单的预加载器,而init()方法创建了主gui.一切正常,但是init()方法完成并且预加载器消失之后,在显示主阶段之前会有明显的延迟.我说这很引人注目,因为它可能长达7秒,足以使用户感到困惑.

I have a program that uses a simple preloader while the init() method creates the main gui. Everything works fine but after the init() method completes and the preloader disappears, there's a noticeable delay before the main stage shows up. I say noticeable because it can be as much as 7 seconds, enough for a user to get confused.

我尝试尽量减少start()方法:

public void start(Stage stage) {
    /*Scene*/
    scene = new Scene(root, 1200, 700);
    stage.setScene(scene);
    scene.setFill(null);

    /*Stage*/
    stage.initStyle(StageStyle.TRANSPARENT);
    stage.centerOnScreen();
    stage.show();

}

有没有办法减少/消除这种延迟?完全废弃预加载器并将其作为主程序的一个阶段会更好吗?预先感谢.

Is there a way to reduce/eliminate this delay? Would it be better to scrap the preloader altogether and implement is as a stage in the main program? Thanks in advance.

我接受了Maverick283的建议,并实现了预加载器的fadeOut.仍然有一些延迟,所以我在显示主要阶段后的之后发送了最后的通知(从主程序到预加载器),并且运行良好!

I took Maverick283's advice and implemented a fadeOut of the preloader. There was still a bit of delay so I sent the final notification (from the main program to the preloader) after showing the main stage and it worked perfectly!

public void start(Stage stage) {
    /*Scene*/
    scene = new Scene(root, 1200, 700);
    stage.setScene(scene);
    scene.setFill(null);

    /*Stage*/
    stage.initStyle(StageStyle.TRANSPARENT);
    stage.centerOnScreen();
    stage.show();
    notifyPreloader(new Preloader.ProgressNotification(0.99));
}

推荐答案

来自 Oracle :

在应用程序启动之前,预加载器收到的最后一个状态更改通知是StateChangeNotification.Type.BEFORE_START.处理完之后,将调用应用程序的start()方法.但是,在调用start()方法后,应用程序准备显示其阶段可能要花费一些时间.如果预加载器阶段已经隐藏,则可能会在一段时间内应用程序在屏幕上什么都没有显示.

The last state change notification received by the preloader before the application starts is StateChangeNotification.Type.BEFORE_START. After it is processed, the application's start() method is called. However, it can take time before the application is ready to display its stage after the start() method is called. If the preloader stage is already hidden, then there could be a period of time when the application shows nothing on the screen.

因此,他们提供了示例代码来解决此问题:

Thus they provide example code how to fix that:

@Override
public void handleStateChangeNotification(StateChangeNotification evt) {
    if (evt.getType() == StateChangeNotification.Type.BEFORE_START) {
        if (isEmbedded && stage.isShowing()) {
            //fade out, hide stage at the end of animation
            FadeTransition ft = new FadeTransition(
                Duration.millis(1000), stage.getScene().getRoot());
                ft.setFromValue(1.0);
                ft.setToValue(0.0);
                final Stage s = stage;
                EventHandler<ActionEvent> eh = new EventHandler<ActionEvent>() {
                    public void handle(ActionEvent t) {
                        s.hide();
                    }
                };
                ft.setOnFinished(eh);
                ft.play();
        } else {
            stage.hide();
        }
    }
}

如果继续阅读,甚至可以共享两个阶段(预加载器和主应用程序阶段)...

If you continue reading there is even a way of sharing the two stages (preloader and main application stage)...

这篇关于显示预载器和载物台之间的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆