我创建了多个文本节点的动画。当从服务器接收到文本时,用户应该正在阅读该文本。问题是几分钟后(大约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);
}
}
MyAnimationTimer:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句