使用淡入淡出和时间线转换的JavaFX

使用淡入淡出和时间线转换的JavaFX,java,javafx,Java,Javafx,我想制作带有淡入淡出和时间线转换的图像幻灯片,所以当我按下按钮时,一个图像消失(通过淡入淡出动画),另一个将出现(通过淡入淡出和时间线转换)。现在我有一个这样的代码,只使第一个图像消失,然后只有时间线工作。。。(我使用并行转换,我也尝试了顺序转换,但不起作用) public类MainPaneController实现可初始化{ @FXML 私有边框窗格边框窗格; @FXML 私人菜单栏菜单栏; @FXML 私人锚网锚网; @FXML 私人HBox; @FXML 私有菜单项打开文件夹; @FXML

我想制作带有淡入淡出和时间线转换的图像幻灯片,所以当我按下按钮时,一个图像消失(通过淡入淡出动画),另一个将出现(通过淡入淡出和时间线转换)。现在我有一个这样的代码,只使第一个图像消失,然后只有时间线工作。。。(我使用并行转换,我也尝试了顺序转换,但不起作用)

public类MainPaneController实现可初始化{
@FXML
私有边框窗格边框窗格;
@FXML
私人菜单栏菜单栏;
@FXML
私人锚网锚网;
@FXML
私人HBox;
@FXML
私有菜单项打开文件夹;
@FXML
私人主播中心;
@FXML
私人影像视图;
@FXML
私人按钮幻灯片按钮;
@FXML
私人菜单;
私有图像;
专用图像分析器;
私人可观察列表图像列表;
私有int indexPrev=0;
私有int indexNext=0;
私人时间表;
私人动画定时器;
int i=0;
@凌驾
公共void初始化(URL位置、ResourceBundle资源)
抛出IndexOutOfBoundsException{
parser=新的ImageParser();
imagesList=FXCollections.observableArrayList();
DirectoryChooser dc=新的DirectoryChooser();
openFolder.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
File dir=dc.showDialog(new Stage());
imagesList=parser.createImagesListFromFileList(dir);
setImage(imagesList.get(0));
}
});
slideShowButton.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
FadeTransition ft=新的FadeTransition();
ft.setNode(图像视图);
ft.setDuration(新的Duration(2000));
ft.setFromValue(1.0);
ft.setToValue(0.0);
ft.setCycleCount(imagesList.size());
ft.setAutoReverse(真);
setImage(imagesList.get(0));
时间线=新时间线();
timeline.setCycleCount(timeline.unfinite);
timeline.setAutoReverse(真);
关键帧关键帧=新关键帧(持续时间。秒(1),
新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
如果(i
我只需要使用一个
时间轴就可以了。例如,对于250ms淡入、250ms淡出和每秒一个新图像,您需要以下
关键帧
s:

  • 时间0:不透明度0.0
  • 时间0.25秒:不透明度1.0
  • 时间0.75秒:不透明度1.0
  • 时间1.0秒不透明度0.0并加载新图像
你可以用它来做这个

timeline = new Timeline();

KeyValue transparent = new KeyValue(imageView.opacityProperty(), 0.0);
KeyValue opaque = new KeyValue(imageView.opacityProperty(), 1.0);

KeyFrame startFadeIn = new KeyFrame(Duration.ZERO, transparent);
KeyFrame endFadeIn = new KeyFrame(Duration.millis(250), opaque);
KeyFrame startFadeOut = new KeyFrame(Duration.millis(750), opaque);
KeyFrame endFadeOut = new KeyFrame(Duration.millis(1000), e -> {
    if (i < images.size()) {
        imageView.setImage(images.get(i));
        i++ ;
    }
}, transparent);

timeline.getKeyFrames().addAll(startFadeIn, endFadeIn, startFadeOut, endFadeOut);

timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
timeline=newtimeline();
KeyValue transparent=新的KeyValue(imageView.opacityProperty(),0.0);
KeyValue不透明=新的KeyValue(imageView.opacityProperty(),1.0);
关键帧startFadeIn=新关键帧(Duration.ZERO,透明);
KeyFrame endFadeIn=新关键帧(持续时间.毫秒(250),不透明);
关键帧startFadeOut=新关键帧(持续时间为.millis(750),不透明);
关键帧结束淡出=新关键帧(持续时间.毫秒(1000),e->{
if(i
这是上一个答案的变体,它解决了上一个答案评论中提出的问题

以下内容在第一个图像中淡入250毫秒,然后在接下来的500毫秒内以100%不透明度显示,在接下来的250毫秒内淡出。第一个
关键帧
加载初始图像,最后一个
关键帧
删除图像(通过将其设置为null)。动画结束后,将调用
Timeline
OnFinished()
方法,该方法检查是否有更多图像,如果有,则调用
displayNextMedia()
,这将重新启动该过程。请注意,从
Timeline
OnFinished()中调用
displayNextImage()
()
事件处理程序不会导致递归到
displayNextImage()

//应用程序的onStart()方法调用本地方法displayNextImage()
//images是实现getNext()和isEmpty()的集合
void displayNextImage(){
时间线=新时间线();
KeyValue transparent=新的KeyValue(imageView.opacityProperty(),0.0);
KeyValue不透明=新的KeyValue(imageView.opacityProperty(),1.0);
KeyFrame startFadeIn=新关键帧(Duration.ZERO,e->{imageView.setImage(images.getNext());},透明);
KeyFrame endFadeIn=新关键帧(持续时间.毫秒(250),不透明);
关键帧startFadeOut=新关键帧(持续时间为.millis(750),不透明);
KeyFrame endFadeOut=新关键帧(Duration.millis(1000),e->{imageView.setImage(null);},透明);
timeline.getKeyFrames().addAll(startFadeIn、endFadeIn、startFadeOut、endFadeOut);
timeline.setCycleCount(Animation.unfinite);
timeline.setOnFinished(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
如果(!images.isEmpty()){
displayNextImage();
}
}
});
timeline.play();
}
谢谢你
timeline = new Timeline();

KeyValue transparent = new KeyValue(imageView.opacityProperty(), 0.0);
KeyValue opaque = new KeyValue(imageView.opacityProperty(), 1.0);

KeyFrame startFadeIn = new KeyFrame(Duration.ZERO, transparent);
KeyFrame endFadeIn = new KeyFrame(Duration.millis(250), opaque);
KeyFrame startFadeOut = new KeyFrame(Duration.millis(750), opaque);
KeyFrame endFadeOut = new KeyFrame(Duration.millis(1000), e -> {
    if (i < images.size()) {
        imageView.setImage(images.get(i));
        i++ ;
    }
}, transparent);

timeline.getKeyFrames().addAll(startFadeIn, endFadeIn, startFadeOut, endFadeOut);

timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
//The Application's onStart() method calls the local method displayNextImage()
//images is some collection that implements getNext() and isEmpty()

void displayNextImage() {
timeline = new Timeline();

KeyValue transparent  = new KeyValue(imageView.opacityProperty(), 0.0);
KeyValue opaque       = new KeyValue(imageView.opacityProperty(), 1.0);

KeyFrame startFadeIn  = new KeyFrame(Duration.ZERO, e -> {imageView.setImage(images.getNext());}, transparent);
KeyFrame endFadeIn    = new KeyFrame(Duration.millis(250), opaque);
KeyFrame startFadeOut = new KeyFrame(Duration.millis(750), opaque);
KeyFrame endFadeOut   = new KeyFrame(Duration.millis(1000), e -> {imageView.setImage(null);}, transparent);

timeline.getKeyFrames().addAll(startFadeIn, endFadeIn, startFadeOut, endFadeOut);
timeline.setCycleCount(Animation.INDEFINITE);


timeline.setOnFinished(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        if (!images.isEmpty()) {
          displayNextImage();
        }
    }
});

timeline.play();
}