В настоящее время у меня есть 5 фигур, которые движутся по экрану. Я использовал setCache и setCacheHint, чтобы загрузка ЦП не была go до 100%, однако после использования этих методов ничего не меняется.
Это мой код при инициализации:
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
//Path for the triangleShape1
Path path = new Path();
path.getElements().add(new MoveTo(100, 0));
path.getElements().add(new CubicCurveTo(480, 0, 380, 220, 200, 820));
path.getElements().add(new ClosePath());
//Path for the squareShape
Path squarePath = new Path();
squarePath.getElements().add(new MoveTo(0, 0));
squarePath.getElements().add(new LineTo(-620, -450));
squarePath.getElements().add(new ClosePath());
//Path for the triangleShape2
Path triangle2Path = new Path();
triangle2Path.getElements().add(new MoveTo(0,0));
triangle2Path.getElements().add(new LineTo(-500, -300));
triangle2Path.getElements().add(new ClosePath());
//Path for the circleShape
Path circlePath = new Path();
circlePath.getElements().add(new MoveTo(0, 0));
circlePath.getElements().add(new HLineTo(200));
circlePath.getElements().add(new ClosePath());
Path rectangePath = new Path();
rectangePath.getElements().add(new MoveTo(0,0));
rectangePath.getElements().add(new VLineTo(1000));
triangleShape1.setCache(true);
triangleShape1.setCacheHint(CacheHint.QUALITY);
triangleShape1.setCacheHint(CacheHint.SPEED);
PathTransition pathTransition = new PathTransition();
pathTransition.setNode(triangleShape1);
pathTransition.setDuration(Duration.seconds(35));
pathTransition.setPath(path);
pathTransition.setCycleCount(PathTransition.INDEFINITE);
pathTransition.play();
squareShape.setCache(true);
squareShape.setCacheHint(CacheHint.QUALITY);
squareShape.setCacheHint(CacheHint.SPEED);
PathTransition pathTransitionSquare = new PathTransition();
pathTransitionSquare.setNode(squareShape);
pathTransitionSquare.setDuration(Duration.seconds(25));
pathTransitionSquare.setPath(squarePath);
pathTransitionSquare.setCycleCount(PathTransition.INDEFINITE);
pathTransitionSquare.play();
triangleShape2.setCache(true);
triangleShape2.setCacheHint(CacheHint.QUALITY);
triangleShape2.setCacheHint(CacheHint.SPEED);
PathTransition pathTriangle2Transition = new PathTransition();
pathTriangle2Transition.setNode(triangleShape2);
pathTriangle2Transition.setDuration(Duration.seconds(35));
pathTriangle2Transition.setPath(triangle2Path);
pathTriangle2Transition.setCycleCount(PathTransition.INDEFINITE);
pathTriangle2Transition.play();
circleShape.setCache(true);
circleShape.setCacheHint(CacheHint.QUALITY);
circleShape.setCacheHint(CacheHint.SPEED);
PathTransition pathCircleTransition = new PathTransition();
pathCircleTransition.setNode(circleShape);
pathCircleTransition.setDuration(Duration.seconds(15));
pathCircleTransition.setPath(circlePath);
pathCircleTransition.setCycleCount(PathTransition.INDEFINITE);
pathCircleTransition.play();
rectangleShape.setCache(true);
rectangleShape.setCacheHint(CacheHint.QUALITY);
rectangleShape.setCacheHint(CacheHint.SPEED);
PathTransition pathRectangleTransition = new PathTransition();
pathRectangleTransition.setNode(rectangleShape);
pathRectangleTransition.setDuration(Duration.seconds(25));
pathRectangleTransition.setPath(rectangePath);
pathRectangleTransition.setCycleCount(PathTransition.INDEFINITE);
pathRectangleTransition.play();
}
В настоящее время я не понимаю, почему мой процессор все еще очень высок при запуске анимации. Я новичок в JavaFX, поэтому в настоящее время я безуспешно следую этому do c. Любая помощь, которая может уменьшить использование процессора для анимации, будет полезна.
Обновлено (минимальный воспроизводимый пример):
Я добавил небольшую версию той же проблемы, такая же проблема произойдет, если этот проект будет выполнен.
Приложение. java
public class App extends Application {
@Override
public void start(Stage stage) {
Parent root = FXMLLoader.load(getClass().getResource("LoginView.fxml"));
stage.setScene(new Scene(root));
stage.show();
}
public static void main() {
launch();
}
}
View.class
public class View implements Initializable {
@FXML
Polygon triangleShape1;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
//Path for the triangleShape1
Path path = new Path();
path.getElements().add(new MoveTo(100, 0));
path.getElements().add(new CubicCurveTo(480, 0, 380, 220, 200, 820));
path.getElements().add(new ClosePath());
triangleShape1.setCache(true);
triangleShape1.setCacheHint(CacheHint.QUALITY);
triangleShape1.setCacheHint(CacheHint.SPEED);
PathTransition pathTransition = new PathTransition();
pathTransition.setNode(triangleShape1);
pathTransition.setDuration(Duration.seconds(35));
pathTransition.setPath(path);
pathTransition.setCycleCount(PathTransition.INDEFINITE);
pathTransition.play();
}
}
View.f xml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Ellipse?>
<?import javafx.scene.shape.Polygon?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.control.Button?>
<AnchorPane fx:id="loginPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="849.0" prefWidth="1246.0" style="-fx-background-color: #FFFFFF;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.openjfx.View">
<Pane fx:id="welcomePane" layoutY="-2.0" prefHeight="858.0" prefWidth="630.0" style="-fx-background-color: #000000;">
<effect>
<DropShadow color="#000000a6" height="51.18" offsetX="5.0" radius="39.425" width="108.52" />
</effect>
<children>
<Text fill="WHITE" layoutX="149.0" layoutY="314.0" stroke="TRANSPARENT" strokeType="OUTSIDE" strokeWidth="0.0" text="Welcome Back!">
<font>
<Font name="SansSerif Bold" size="45.0" />
</font>
</Text>
<Text fill="WHITE" layoutX="256.0" layoutY="408.0" strokeType="OUTSIDE" strokeWidth="0.0" text="To open main screen ">
<font>
<Font name="SansSerif Bold" size="20.0" />
</font>
</Text>
<Text fill="WHITE" layoutX="290.0" layoutY="437.0" strokeType="OUTSIDE" strokeWidth="0.0" text="enter your credentials!" wrappingWidth="241.96484375">
<font>
<Font name="SansSerif Bold" size="20.0" />
</font>
</Text>
<Button layoutX="170.0" layoutY="534.0" mnemonicParsing="false" prefHeight="52.0" prefWidth="291.0" style="-fx-background-color: transparent; -fx-border-color: #FFFFFF; -fx-border-radius: 20;" text="SIGN UP" textFill="WHITE">
<font>
<Font name="SansSerif Bold" size="13.0" />
</font>
</Button>
<Rectangle fx:id="squareShape" arcHeight="5.0" arcWidth="5.0" fill="WHITE" height="82.0" layoutX="517.0" layoutY="467.0" opacity="0.2" stroke="TRANSPARENT" strokeType="INSIDE" width="87.0" />
</children>
</Pane>
<children>
<Polygon fx:id="triangleShape1" layoutX="128.0" layoutY="-59.0" opacity="0.2" points="-50.0, 40.0, 90.0, 26.0, 0.0, -60.0" stroke="TRANSPARENT" strokeType="INSIDE" />
<Ellipse fx:id="circleShape" layoutX="-158.0" layoutY="819.0" opacity="0.2" radiusX="153.0" radiusY="158.0" stroke="TRANSPARENT" strokeType="INSIDE" style="-fx-rotate: 45;" />
<Polygon fx:id="triangleShape2" layoutX="540.0" layoutY="926.0" opacity="0.2" points="-66.0, -45.72857666015625, 90.0, 26.0, 19.0, -76.72857666015625" stroke="TRANSPARENT" strokeType="INSIDE" />
<Rectangle fx:id="rectangleShape" arcHeight="5.0" arcWidth="5.0" height="31.0" layoutX="476.0" layoutY="-28.0" opacity="0.2" stroke="#ffffff00" strokeType="INSIDE" width="58.0" />
</children>
</AnchorPane>