首页 > 解决方案 > JavaFX 一张一张显示多个gif

问题描述

我想一个接一个地显示不同的 gif。
我的想法是使用时间轴动态更改包装到图像中的显示 gif。
但我得到一个错误:

从 lambda 表达式引用的局部变量必须是 final 或有效 final

有什么解决办法吗?
我的代码:

public class Task10 extends Application {
    @Override
    public void start(Stage primaryStage) {
        HBox hbox5 = new HBox();

        VBox VBoxAll = new VBox();


        Image gifs[] = new Image[3];
        gifs[0] = new Image(this.getClass().getResource("/img/L1.gif").toExternalForm());
        gifs[1] = new Image(this.getClass().getResource("/img/L2.gif").toExternalForm());
        gifs[2] = new Image(this.getClass().getResource("/img/L1.gif").toExternalForm());

        ImageView currentGif = new ImageView();


        Button localLoadButton = new Button("Start!");
        localLoadButton.setOnAction(e -> {
            show(currentGif, gifs);
        });

        hbox5.getChildren().addAll(currentGif, localLoadButton);

        VBoxAll.getChildren().addAll(hbox5);
        VBoxAll.setSpacing(15);

        Pane pane = new Pane();
        pane.getChildren().add(VBoxAll);

        Scene scene = new Scene(pane, 500, 350);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void show(ImageView currentGif, Image[] gifs) {
        for (int i = 0; i<gifs.length; i++) {
            Timeline timeline = new Timeline(
                new KeyFrame(Duration.ZERO, e -> { currentGif.setImage(gifs[i]); }),
                new KeyFrame(Duration.seconds(2), e -> { currentGif.setImage(null); })
            );
            timeline.play();
        }

    }


}

标签: javajavafxjavafx-8gifanimated-gif

解决方案


在 Java 中,在 lambda 或匿名类之外声明但在所述 lambda 或匿名类中使用的局部变量必须是“有效最终的”。这意味着永远不会修改局部变量(即重新分配)。如果局部变量可以将final关键字添加到声明中而不会导致编译错误,则该变量实际上是最终的。

在您的代码中,您具有以下内容:

public void show(ImageView currentGif, Image[] gifs) {
    for (int i = 0; i<gifs.length; i++) {
        Timeline timeline = new Timeline(
            new KeyFrame(Duration.ZERO, e -> { currentGif.setImage(gifs[i]); }),
            new KeyFrame(Duration.seconds(2), e -> { currentGif.setImage(null); })
        );
        timeline.play();
    }

}

您正在引用ilambda 内的局部变量( first KeyFrame)。问题是for循环修改了i(例如i++),这意味着变量不是有效的最终变量。一个可能的解决方法是在循环中声明另一个变量,为其分配 的值i,然后在 lambda 中使用那个新的(不变的!)变量。然而,这不是唯一的问题。

Timeline您的代码正在为数组中的每个元素创建一个单独的元素gifs——换句话说,每个循环一个元素。您还可以在每次迭代结束时调用play每个。Timeline这将导致Timeline同时播放多个 s。正如您可能想象的那样,这不会做您想要的。相反,您应该创建一个单一 Timeline的并让每个更改都Image成为它自己的KeyFrame. 类似于以下内容:

public void show(ImageView view, Image[] gifs, Duration displayDuration) {
    Timeline timeline = new Timeline();

    Duration totalDelay = Duration.ZERO;
    for (Image gif : gifs) {
        KeyFrame frame = new KeyFrame(totalDelay, e -> view.setImage(gif));
        timeline.getFrames().add(frame);
        totalDelay = totalDelay.add(displayDuration);
    }
    timeline.getFrames().add(new KeyFrame(totalDelay, e -> view.setImage(null));

   timeline.play();
}

这将每隔一段时间将数组中Image的下一个更改为下一个。ImagedisplayDuration

如果你想知道为什么gif允许在 lambda 中使用——尽管显然被修改了——这是因为我使用了“增强的 for 循环”。增强 for 循环的变量的处理方式与常规for循环不同,因为它在每次迭代时都被初始化。有关更多信息,请参阅增强的“for”循环和 lambda 表达式


推荐阅读