0
我正在製作一個滑動動畫來將場景切換到另一個場景,但是當我調用此方法時,它將有切換場景的延遲。我發現原因是類Scene
的方法snapshot()
。 有沒有人有解決方案?Javafx Scene.snapshot方法性能低下
代碼:
public void switchScene(Scene target) {
Scene current = getPrimaryStage().getScene();
WritableImage beforeImage;
WritableImage afterImage;
int width = ((int) ((Region) current.getRoot()).getWidth());
int height = ((int) ((Region) current.getRoot()).getHeight());
beforeImage = new WritableImage(width, height);
ImageView leftImage = new ImageView(current.snapshot(beforeImage));
afterImage = new WritableImage(width, height);
ImageView rightImage = new ImageView(target.snapshot(afterImage));
leftImage.setTranslateX(0);
rightImage.setTranslateX(width);
StackPane animation = new StackPane(leftImage, rightImage);
animation.setPrefSize(target.getWidth(), target.getHeight());
primaryStage.setScene(new Scene(animation));
Timeline timeline = new Timeline();
KeyValue kv = new KeyValue(rightImage.translateXProperty(), 0, Interpolator.EASE_BOTH);
KeyFrame kf = new KeyFrame(Duration.seconds(0.75), kv);
timeline.getKeyFrames().add(kf);
timeline.setOnFinished(t -> {
// remove pane and restore scene 1
primaryStage.setScene(target);
});
timeline.play();
}
不知道你在那裏試圖做什麼。但是,爲什麼不對異步調用[snapshot](http://docs.oracle.com/javafx/2/api/javafx/scene/Scene.html#snapshot(javafx.util.Callback,%) 20javafx.scene.image.WritableImage))方法? – ItachiUchiha