JavaFX 在 ImageView 中播放 gif 图像



我看到这个问题问了很多,但我还没有看到一个非常普遍的方法。我正在使用JavaFX在Java Eclipse Oxygen中开发桌面应用程序,并且我有一个加载器,我想在通过http加载图像时显示它。当用户单击不同的行时,必须通过http加载图像。所以我把我的加载器放在图像的顶部,我只是使用; productPreviewLoader.setVisible(true)显示或隐藏它。fxml 中的加载程序 ImageView 在 GridPane 中如下所示。

<GridPane>
   <columnConstraints>
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints minHeight="10.0" valignment="TOP" vgrow="ALWAYS" />
      <RowConstraints vgrow="SOMETIMES" />
      <RowConstraints vgrow="SOMETIMES" />
   </rowConstraints>
   <children>
      <ImageView fx:id="productPreview" fitHeight="282.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" />
      <!-- This is the loader-->
      <ImageView fx:id="productPreviewLoader" fitHeight="112.0" fitWidth="136.0" pickOnBounds="true" preserveRatio="true" /> 
      <TextFlow fx:id="productPreviewTextFlow" GridPane.rowIndex="1">
         <GridPane.margin>
            <Insets right="10.0" />
         </GridPane.margin>
         <padding>
            <Insets bottom="30.0" left="10.0" right="10.0" top="30.0" />
         </padding></TextFlow>

设置加载器映像的代码如下所示;

//Set up the product preview image loader
BufferedImage bufferedImage;
File file = new File("resources\images\loaders\img-loader-icon.gif");
try {
bufferedImage = ImageIO.read(file);
   productPreviewLoader.setImage(SwingFXUtils.toFXImage(bufferedImage, null));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
productPreviewLoader.setVisible(false);
图像

无法在图像视图中播放。如果不可能,请告诉我另一种方法。此外,我在让图像视图在网格窗格行中水平居中图像视图时遇到了问题。每当调整窗口大小时,GridPane 都会调整大小以匹配其父级的高度。我知道这是 2 个问题,但我最终可能会创建 2 个问题。

好吧,

您还没有提供一个完整的示例,我可以使用该示例来展示如何将gif图像加载到ImageView的用例,因此我将为您提供一个完整的示例:

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class GifViewerTest extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage stage) throws Exception {
        VBox box = new VBox();
        ImageView imageView = new ImageView();
        box.getChildren().add(imageView);
        Button localLoadButton = new Button("Local load");
        Button externalLoadButton = new Button("URL Load");
        localLoadButton.setOnAction(e -> {
            imageView.setImage(new Image(this.getClass().getResource("java.gif").toExternalForm()));
        });
        externalLoadButton.setOnAction(e -> {
            String imageSource = "https://cdn.dribbble.com/users/550761/screenshots/1773241/untitled-5.gif";
            try {
                imageView.setImage(createImage(imageSource));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        });
        FlowPane buttonPane = new FlowPane();
        buttonPane.setAlignment(Pos.CENTER);
        buttonPane.getChildren().addAll(localLoadButton, externalLoadButton);
        box.getChildren().add(buttonPane);
        stage.setScene(new Scene(box, 500, 400));
        stage.show();
    }
    private Image createImage(String url) throws IOException {
        // You have to set an User-Agent in case you get HTTP Error 403
        // respond while you trying to get the Image from URL.
        URLConnection conn = new URL(url).openConnection();
        conn.setRequestProperty("User-Agent", "Wget/1.13.4 (linux-gnu)");
        try (InputStream stream = conn.getInputStream()) {
            return new Image(stream);
        }
    }
}

如果您的所有图像都是通过URL加载的,那么为什么不直接使用WebViewer并加载URL,而不是使用ImageView呢?如果 gif 大小不小,这将导致 GUI 滞后,直到从 InputStream 加载 gif 图像,在这种情况下,您应该创建加载过程到后台线程,但正如我所说,如果您只使用 WebViewer,您可以避免所有这些。

最新更新