2017-09-14 5 views
0

장면을 다른 장면으로 전환하기 위해 슬라이딩 애니메이션을 만들지 만,이 메서드를 호출하면 전환 장면이 지연됩니다. 원인이 Scenesnapshot()이라는 것을 발견했습니다. 해결책이있는 사람이 있습니까?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(); 
} 
+0

당신이 무엇을하려고하는지 잘 모르겠습니다. 그러나 [스냅 샷] (snapshot)에 대한 비동기 호출을 만드십시오. (http://docs.oracle.com/javafx/2/api/javafx/scene/Scene.html#snapshot (javafx.util.Callback, % 20javafx.scene.image.WritableImage)) method? – ItachiUchiha

답변

0

이 방법으로 스냅 샷을 촬영 자바 땅에 머무는 동안 속도를 위해 할 수있는 많은이 아니라, 본질적으로 느린 작업입니다. 코멘트에서 제안 된 것처럼, 스냅 샷을 취하고 싶다면 비동기 메소드를 사용하는 것이 더 나은 접근 방법입니다. 비동기 메소드는 실행되는 동안 UI 스레드를 차단하지 않습니다 (지연되는 동안, 앱 여전히 응답 할 것입니다.)

그러나 예제를 올바르게 이해했다면 전혀 스크린 샷을 사용할 필요가 없습니다. 노드 자체에 애니메이션을 적용하는 대신 이미지를 사용하는 이유는 무엇입니까? 모든 JavaFX 요소는 장면 그래프의 노드이므로 동일한 방식으로 애니메이션 될 수 있습니다. 그래서 대신 :

StackPane animation = new StackPane(leftImage, rightImage); 

당신은 할 수 있어야한다 :

StackPane animation = new StackPane(source, target); 

... 다음 직접 스크린 샷을 복용의 느린 과정을 거치지 않고 창 애니메이션을 사용합니다.