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

Ulm*_*Ulm 5 java javafx lazy-loading image

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);
    }
Run Code Online (Sandbox Code Playgroud)

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

有谁知道如何实现这样的目标

mip*_*ipa 8

这是异步加载图像的示例。您可以选择是要使用本地文件还是远程 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);
    }
}
Run Code Online (Sandbox Code Playgroud)