JavaFX 动画 - 性能下降

标签 java animation javafx

<分区>

我创建了一个包含多个文本节点的动画。用户应该阅读从服务器接收到的文本。问题是几分钟后(大约 5 分钟)性能开始下降。从 60 fps 到 30 fps 及以下。因此,文本很难阅读。

编辑 2:

我创建了一个最小的、完整的和可验证的示例:

项目中有3个文件:

MainFxApp:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.util.List;

public class MainFxApp extends Application {

    @Override
    public void start(Stage primaryStage) {

        Pane root = new Pane();
        root.setStyle("-fx-background-color: black;");

        MyAnimationTimer myAnimationTimer = new MyAnimationTimer((List<MyText>) (List<?>) root.getChildren());

        MyText newText;
        for (int i = 65; i < 85; i++) {
            newText = new MyText("" + ((char) i));
            newText.setFill(Color.GREEN);
            newText.setFont(Font.font(40));
            myAnimationTimer.addNode(newText);
        }

        Scene scene = new Scene(root, 1200, 600);

        primaryStage.setTitle("Performance test");
        primaryStage.setScene(scene);
        primaryStage.show();

        myAnimationTimer.start();


    }

    public static void main(String[] args) {
        launch(args);
    }
}

我的动画定时器:

import javafx.animation.AnimationTimer;
import javafx.application.Platform;
import javafx.scene.CacheHint;
import javafx.scene.Node;

import java.util.List;

public class MyAnimationTimer extends AnimationTimer {
    private List<MyText> nodes;
    private double panelWidth;
    private double panelHeight;
    private double basicVelocity; // Distance per nanosecond

    private long lastFrameTime;

    private long timeCount = 0;
    private int frameCount = 0;

    MyAnimationTimer(List<MyText> nodes) {
        super();
        this.nodes = nodes;
        this.panelWidth = 1200;
        this.panelHeight = 600;
        this.setBasicVelocity();
    }

    @Override
    public void start() {
        this.lastFrameTime = System.nanoTime();
        super.start();
    }

    @Override
    public void handle(long now) {

        long deltaT = now - lastFrameTime;
        double deltaX = this.basicVelocity * deltaT;

        for (MyText node : this.nodes) {
            node.setTranslateX(node.getTranslateX() + node.direction * deltaX);
            if (node.getTranslateX() < 0) {
                node.direction = 1;
            } else if (node.getTranslateX() > 1200) {
                node.direction = -1;
            }
        }

        this.lastFrameTime = now;

        this.timeCount += deltaT;
        this.frameCount++;

        if (timeCount > 1000000000) {
            System.out.println(this.frameCount / (double) timeCount * 1000000000);
            this.frameCount = 0;
            this.timeCount = 0;
        }
    }

    void addNode(final MyText node) {  // Not sure about the final thing
        Platform.runLater(() -> {
            node.setCache(true);
            node.setCacheHint(CacheHint.SPEED);
            node.setTranslateY(panelHeight / 2);

            double nodePositionX = panelWidth - 20;

            if (nodes.size() >= 1) {
                Node lastNode = nodes.get(nodes.size() - 1);
                double lastNodeEnd = lastNode.getTranslateX() + 50;
                if (lastNodeEnd > nodePositionX) {
                    nodePositionX = lastNodeEnd;
                }
            }

            node.setTranslateX(nodePositionX);

            nodes.add(node);
        });
    }

    private void setBasicVelocity() {
        Platform.runLater(() -> {
            this.basicVelocity = ((panelWidth / 4) * 3 / (double) 5000 / 1000000.0);
        });
    }
}

我的文本:

import javafx.scene.text.Font;
import javafx.scene.text.Text;

class MyText extends Text {
    int direction = -1;

    MyText(String text) {
        super(text);
        this.setFont(new Font("Arial Regular", 40));
    }
}

即使是这个简单的例子,性能下降也很明显。场景中有 20 个节点,fps 下降到 20 以下。我的 CPU 是 i5-4440 CPU (3.10GHz × 4)。这个问题出现在我测试过的每个平台上——JavaFX 8、JavaFX 9 和 Ubuntu 16.04。

编辑 3:

这个问题似乎只存在于 Linux 平台上。

然而,即使在 Windows 上,当我经常通过 Platform.runLater 方法访问 JavaFX 线程时,动画似乎并不流畅,即使它保持在 60fps。有谁知道如何改进它?

谢谢,简

最佳答案

看起来您的 Linux 硬件加速在 JavaFX 渲染方面存在一些问题。我在具有类似功能的机器上没有遇到任何性能问题。

尝试在纯软件模式下运行您的代码:

java -Dprism.order=j2d -jar myfxapp.jar 

关于JavaFX 动画 - 性能下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44851916/

相关文章:

java - 持久性 Java 数据库

java - 制作一个可以自定义的自定义JButton?

java - 如何仅从日志文件中删除 Tomcat 信息

android - 动画相对布局以像 Snack Bar 一样向下/向上移动

java - 如何输出记分牌的更新值

javafx - TextArea 的良好可观察附加基础是什么?

Java - TitledBorder 占用太多垂直空间(仅限 Windows)

objective-c - 如何通过触摸和滑动来移动和滑开 UIView?

javascript - 添加和删​​除类 javascript 循环

java - 从文本文件Javafx中顺序读取