gpt4 book ai didi

java - 为了在 JavaFX 中的 ImageView 上进行转换

转载 作者:搜寻专家 更新时间:2023-10-31 20:12:41 25 4
gpt4 key购买 nike

我已经看了How to wait for a transition to end in 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 对象列表逐个动画化的任何想法(无论列表中有多少个 ImageView,所以我不想对其进行硬编码)。谢谢。

最佳答案

因此,解决此问题的明显方法是为您的 SequentialTransitions 使用周围的 SequentialTransition 来播放这些 Transitions,好吧,顺序播放而不是一次播放:

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();
}
}

关于java - 为了在 JavaFX 中的 ImageView 上进行转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17536466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com