[英]JavaFX play gif image in ImageView
我看到這個問題問了很多,但我還沒有看到一個非常通用的方法。 我正在使用 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);
圖像不在 ImageView 中播放。 如果不可能,請告訴我另一種方法。 此外,我在讓 ImageView 在 GridPane 行中水平居中 ImageView 時遇到問題。 每當調整窗口大小時,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,則可以避免所有這些。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.