java - 如何延迟加载图片而不是等待它在 Java 中完成下载?

标签 java image javafx lazy-loading

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;


public class Testwebimage extends Application {

    public void start(Stage primaryStage) {

        Image image = new Image("http://i.imgur.com/hbjCOgg.jpg");
        ImageView imageView = new ImageView();
        imageView.setImage(image);

        StackPane root = new StackPane();
        root.getChildren().add(imageView);
        Scene scene = new Scene(root);

        primaryStage.setScene(scene);
        primaryStage.setMaximized(true);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }

我正在尝试创建一个直接从 url 显示图像的程序,但我遇到的问题是它等待图像完全加载然后显示它,这意味着如果图像尺寸很大, 显示图像会花费大量时间,这会很烦人。但如果它显示图像加载打开,有点像 this .

有谁知道如何实现类似 this 的东西吗? ?

最佳答案

这是一个异步加载图像的例子。您可以选择是要使用本地文件还是远程 URL。

package jfxfeatures.graphics.image.loading.async;

import java.io.File;
import java.net.MalformedURLException;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class AsyncImageDemo extends Application {

    @Override
    public void start(Stage stage) {
        String imgURL = null;
        try {
            final String remoteURL = "http://farm5.staticflickr.com/4129/4960490401_71a3d05d28_o_d.jpg";
            final String remoteURL2 = "http://www.spacetelescope.org/static/archives/posters/large/earth02.jpg";
            final String localURL = new File("data/earth02.jpg").toURI().toURL().toExternalForm();
            final String localFile = "/earth02.jpg";

            //===========================
            // Select local or remote image source.
            imgURL = localFile;
            //===========================
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }

        StackPane root = new StackPane();
        Scene scene = new Scene(root, 800, 800);
        scene.setFill(Color.BLACK);

        ImageView iv = new ImageView();
        iv.setPreserveRatio(true);
        iv.fitHeightProperty().bind(root.heightProperty());
        iv.fitWidthProperty().bind(root.widthProperty());
        root.getChildren().add(iv);

        stage.setTitle(getClass().getSimpleName());
        stage.setScene(scene);
        stage.show();

        if (imgURL != null) {
            Image image = new Image(imgURL, true);
            image.progressProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                    System.out.println("Progress: " + Math.rint(newValue.doubleValue() * 100) + "%");
                }
            });
            iv.setImage(image);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

关于java - 如何延迟加载图片而不是等待它在 Java 中完成下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38579075/

相关文章:

android加载SD卡中图像的缩略图

java - 有没有办法检查 ComboBox 中是否有 JavaFX 中的任何项目?

java - 在 Android 上安装数据库期间,将 AsyncTask 与 Sqlite 一起使用会导致应用程序崩溃吗?

java - messagebodyreader Jersey

html - 从 Google Images 在 R 中抓取网页

java - 使用 Java 的 ConvolveOP 的 Sobel 滤波器对图像进行边缘检测的问题

css - 如何用CSS去掉折线图中的轴线?

java - 为什么 SceneBuilder 对自定义控件如此讲究?

java - 如何在 Java 中转义命令行参数(文件路径)中的斜杠?

java - 使用 BitmapFactory 在 Canvas 上 onDraw 时出错