2013-07-08 28 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; 

} 

有關如何逐一對這些List ImageView對象進行動畫處理的任何想法(無論ListView中有多少ImageView,因此我不想對其進行硬編碼)。謝謝。

回答

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

塞巴斯蒂安感謝! :)我還沒有嘗試過(當我回家時會這樣做),但它有道理大聲笑..我很愚蠢..非常感謝! :)我會更新你,一旦我回家.. –

+0

沒問題。如果我可以添加,我寧願使用Timer和TimerTask來觸發除JavaFX之外的下一張幻燈片,因爲這會更好地擴展。我幾個星期前爲自己寫了一個幻燈片,並且添加按鈕和停止幻燈片演示是以後可能要添加到應用程序中的東西。 – Sebastian

+0

嗨巴斯蒂安,我嘗試了你建議的解決方案,不幸的是它並沒有完全做到這一點。當我運行代碼時,我只能看到列表中的最後一張幻燈片。它顯示在前面,然後淡入並淡出屏幕。我認爲我需要爲每張幻燈片進行過渡,這不是在這裏發生的任何想法?非常感謝:) –