几个小时后 JavaFX 时间轴动画停顿

JavaFX Timeline Animation Stutters after a couple hours

前言: 所以我有这个,好吧,应用程序并不是那么重要,它是一个 tcp 侦听器,整天与一些硬件聊天。但它也有一些显示内容。显示在屏幕的不同区域显示不同的自定义节点,屏幕底部是一个显示一些 "stuff" 的网格,但当没有任何 "stuff" 时,它会在屏幕上滚动一些文本。

问题: 正如我将 post 在构建时间轴的方法中一样,有一个文本元素在屏幕上从右向左过渡。这在头一个小时或两个小时内按预期工作,但过了一会儿它变得越来越紧张。 TCP 侦听器仍然很活泼,它产生的线程进出控制器并更改 UI 没有任何问题,但是滚动文本在 55" 屏幕上一次跳跃 2 英寸正在显示。我真的需要一种方法让文本像我第一次无限期地启动应用程序时那样平滑滚动。

private void buildChyron() throws IllegalArgumentException {
    setMessages();
    LowerGrid.getChildren().clear();
    StringBuilder txtstr = new StringBuilder();
    for (String s : messages) {
        txtstr.append(s).append("                           ");
    }
    txtstr = new StringBuilder(txtstr.toString().trim());
    Text msg = new Text(txtstr.toString());

    LowerGrid.add(msg, 0, 0);
    msg.setTextOrigin(VPos.TOP);
    msg.setFont(Font.font("Veranda", FontWeight.BOLD, 150));

    double sceneWidth = MainGrid.getWidth();
    double msgWidth = msg.getLayoutBounds().getWidth();

    KeyValue initKeyValue = new KeyValue(msg.translateXProperty(), sceneWidth);
    KeyFrame initFrame = new KeyFrame(Duration.ZERO, initKeyValue);
    KeyValue endKeyValue = new KeyValue(msg.translateXProperty(), -1.0 * msgWidth);
    KeyFrame endFrame = new KeyFrame(Duration.seconds((msgWidth + sceneWidth) / messageRate), endKeyValue);
    if ((timeline != null) && (timeline.getStatus().equals(Timeline.Status.PAUSED))) {
        timeline.playFromStart();
    } else {
        timeline = new Timeline(initFrame, endFrame);
        timeline.setCycleCount(Timeline.INDEFINITE);
        if (msgIndex == messages.size() - 1) msgIndex = 0;
        else msgIndex++;
        timeline.playFromStart();
    }
}

这是我调用它的地方,如您所见,我一直在尝试对其进行试验,认为这可能是硬件资源问题,但我无法控制它。我现在正在测试这个东西,它在我所在的设施中非常显眼的位置。超级尴尬

private void updateStack() {
    int count = 0;
    System.out.println(andonStack);
    andonStack.sortStack();
    LowerGrid.getChildren().clear();
    if (pokeyStack.size() > 0) {
        AndonPane pane = new AndonPane(pokeyStack.get(0).getPokayo());
        LowerGrid.add(pane, 0, 0, 9, 1);
    } else {

        int i;
        for (i = 0; i < andonStack.size(); i++) {
            if (i >= 9) break;
            panes[i] = new AndonPane(((AndonSignalStack.AndonSignal) andonStack.get(i)).getType(),
                    ((AndonSignalStack.AndonSignal) andonStack.get(i)).getStation(), this);
            count++;
        }
        for (; i < 9; i++) {
            panes[i] = new Pane();
            panes[i].setStyle("-fx-background-color: rgba(0,0,0, 0)");
            panes[i].setPrefWidth(MainGrid.getWidth() * (9 - i) / 9);
        }
        for (i = 0; i < count; i++) {
            LowerGrid.add(panes[i], i, 0);
        }
        if (i == 0) {
            Platform.runLater(this::buildChyron);
        }else{
            if (timeline != null) {
                System.gc();
                timeline.stop();
                timeline = null;
            }
        }
    }
}

Scrolling Text Picture

答案是设置面板中的清除方法。动画需要停止并删除

不要使用 pane.clear();当有动画时使用 LowerGrid.getChildren().removeAll();