2013-07-08 12 views
5

私はすでにHow to wait for a transition to end in javafx 2.1?を見てきましたが、私の問題は解決していません。JavaFXのImageViewに移行するには

私はImageViewのオブジェクトのリストを持っている、と私は、このリストを反復処理し、リストの各「スライド」に関する次のアクションを実行したい:数秒間

  1. フェード
  2. での滞在
  3. フェードアウト

を私は場所に次のコードを持っていますが、遷移が非同期であるため、ループは同時にすべての「スライド」への移行を適用します。

// The method I am running in my class 

public void start() { 

    for (ImageView slide : slides) { 

     SequentialTransition sequentialTransition = new SequentialTransition(); 

     FadeTransition fadeIn = Transition.getFadeTransition(slide, 0.0, 1.0, 2000); 
     FadeTransition stayOn = Transition.getFadeTransition(slide, 1.0, 1.0, 2000); 
     FadeTransition fadeOut = Transition.getFadeTransition(slide, 1.0, 0.0, 2000); 

     sequentialTransition.getChildren().addAll(fadeIn, stayOn, fadeOut);    
     this.root.getChildren().add(slide);    
     sequentialTransition.play(); 

    } 
} 

// the method in the Transition helper class: 

public static FadeTransition getFadeTransition(ImageView imageView, double fromValue, double toValue, int durationInMilliseconds) { 

    FadeTransition ft = new FadeTransition(Duration.millis(durationInMilliseconds), imageView); 
    ft.setFromValue(fromValue); 
    ft.setToValue(toValue); 

    return ft; 

} 

私はImageViewのこれらのリストは、一つずつ(関係なくImageViewsの数のリストにあるので、私はハードコードにしたくないが)オブジェクトをアニメーション化することができる方法上の任意のアイデア。ありがとう。

答えて

4

ので、これにobivous修正プログラムは、これらのトランジションを果たしているあなたのSequentialTransitionsため、周囲のSequentialTransitionを使用することであろうだけでなく、順次代わりに、すべての一度に:

public void start() { 

    SequentialTransition slideshow = new SequentialTransition(); 

    for (ImageView slide : slides) { 

     SequentialTransition sequentialTransition = new SequentialTransition(); 

     FadeTransition fadeIn = Transition.getFadeTransition(slide, 0.0, 1.0, 2000); 
     FadeTransition stayOn = Transition.getFadeTransition(slide, 1.0, 1.0, 2000); 
     FadeTransition fadeOut = Transition.getFadeTransition(slide, 1.0, 0.0, 2000); 

     sequentialTransition.getChildren().addAll(fadeIn, stayOn, fadeOut);    
     this.root.getChildren().add(slide);    
     slideshow.getChildren().add(sequentialTransition); 

    } 
    slideshow.play(); 
} 

第二に、あなたの代わりにPauseTransitionを使用する必要があります1.0から1.0まで補間するFadeTransitionです。

本の完全な例のアプリケーションは、次のようになります。

package slideshow; 

import javafx.animation.FadeTransition; 
import javafx.animation.PauseTransition; 
import javafx.animation.SequentialTransition; 
import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class SimpleSlideShowTest extends Application{ 

    class SimpleSlideShow { 

    StackPane root = new StackPane(); 
    ImageView[] slides; 

    public SimpleSlideShow() { 
     this.slides = new ImageView[4]; 
     Image image1 = new Image(SlideShowTest.class.getResource("pic1").toExternalForm()); 
     Image image2 = new Image(SlideShowTest.class.getResource("pic2").toExternalForm()); 
     Image image3 = new Image(SlideShowTest.class.getResource("pic3").toExternalForm()); 
     Image image4 = new Image(SlideShowTest.class.getResource("pic4").toExternalForm()); 
     slides[0] = new ImageView(image1); 
     slides[1] = new ImageView(image2); 
     slides[2] = new ImageView(image3); 
     slides[3] = new ImageView(image4); 

    } 

    public StackPane getRoot() { 
     return root; 
    } 

    // The method I am running in my class 

    public void start() { 

     SequentialTransition slideshow = new SequentialTransition(); 

     for (ImageView slide : slides) { 

     SequentialTransition sequentialTransition = new SequentialTransition(); 

     FadeTransition fadeIn = getFadeTransition(slide, 0.0, 1.0, 2000); 
     PauseTransition stayOn = new PauseTransition(Duration.millis(2000)); 
     FadeTransition fadeOut = getFadeTransition(slide, 1.0, 0.0, 2000); 

     sequentialTransition.getChildren().addAll(fadeIn, stayOn, fadeOut); 
     slide.setOpacity(0); 
     this.root.getChildren().add(slide); 
     slideshow.getChildren().add(sequentialTransition); 

     } 
     slideshow.play(); 
    } 

// the method in the Transition helper class: 

    public FadeTransition getFadeTransition(ImageView imageView, double fromValue, double toValue, int durationInMilliseconds) { 

     FadeTransition ft = new FadeTransition(Duration.millis(durationInMilliseconds), imageView); 
     ft.setFromValue(fromValue); 
     ft.setToValue(toValue); 

     return ft; 

    } 
    } 

    public static void main(String[] args) { 
    launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
    SimpleSlideShow simpleSlideShow = new SimpleSlideShow(); 
    Scene scene = new Scene(simpleSlideShow.getRoot()); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
    simpleSlideShow.start(); 
    } 
} 
+0

おかげセバスチャンを! :)私はそれを試していない(私は家に帰るときに行うだろうが)それは意味がLOLになります..私はとても愚かな..ありがとう! :)私は家に帰るとあなたを更新します。 –

+0

問題はありません。もし私が追加したいのであれば、TimerとTimerTaskを使って次のスライドをJavaFXとは別に起動させることになります。私は数週間前に自分でスライドショーを書きました。ボタンを追加したり、スライドショーを止めたりすることは、後でアプリケーションに追加したいことかもしれません。 – Sebastian

+0

こんにちはバスティアン、私はあなたが示唆したソリューションを試してみました。コードを実行すると、リストの最後のスライドだけが表示されます。正面に表示されてから消えてフェードアウトし、画面を空白にします。私はここで起こっていない各スライドのためのトランジションをする必要があると思う..任意のアイデア?ありがとうございます:) –

関連する問題