Je fais une animation coulissante pour passer d'une scène à une autre scène, mais quand j'appelle cette méthode, il a un délai pour changer de scène. J'ai trouvé que la cause est une méthode snapshot()
de la classe Scene
. Est-ce que quelqu'un a une solution?Javafx Scene.snapshot méthode avec des performances lentes
code:
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();
}
Vous ne savez pas exactement ce que vous essayez de faire là-bas. Mais, pourquoi ne faites-vous pas un appel asynchrone au [snapshot] (http://docs.oracle.com/javafx/2/api/javafx/scene/Scene.html#snapshot (javafx.util.Callback,% 20javafx.scene.image.WritableImage)) méthode? – ItachiUchiha