5

私はすでにjavafx 2.1 でトランジションが終了するのを待つ方法を見てきましたか? しかし、それは私の問題を完全には解決しません。

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 オブジェクトのリストを 1 つずつアニメーション化する方法についてのアイデア (リストにある ImageView の数に関係なく、ハードコーディングしたくありません)。ありがとう。

4

1 に答える 1

4

したがって、これに対する明らかな修正は、これらのトランジションを一度にではなく、順番に再生する 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();
}

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

これの完全な適用例は次のようになります。

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();
  }
}
于 2013-07-09T12:14:28.690 に答える