2016-07-25 86 views
3
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如何懶惰加載圖片而不是等待它完成Java下載?

有沒有人有任何想法如何實現像this

+0

[說不定的InputStream的圖像?](https://docs.oracle.com/javafx/2/api/javafx/scene/image/Image.html#Image(java.io.InputStream中)) – Jeeter

回答

2

以下是異步加載圖像的示例。您可以選擇是否要使用本地文件或遠程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); 
    } 
}