2012-09-27 59 views
26

如何在ImageView中獲取圖像以自動調整大小,使其始終適合父節點?調整圖像大小以適應父節點

這裏是一個小的代碼示例:

@Override 
public void start(Stage stage) throws Exception { 
    BorderPane pane = new BorderPane(); 
    ImageView img = new ImageView("http://..."); 

    //didn't work for me: 
    //img.fitWidthProperty().bind(new SimpleDoubleProperty(stage.getWidth())); 

    pane.setCenter(img); 

    Scene scene = new Scene(pane); 
    stage.setScene(scene); 
    stage.show(); 
} 
+1

相關問題:([如何調整JavaFX中的窗口時,調整圖像大小] http://stackoverflow.com/questions/22993550/how-to-resize-an-image -when-resizing-window-in-javafx/22997981?noredirect = 1#36027997) – jewelsea

回答

40
@Override 
public void start(Stage stage) throws Exception { 
    BorderPane pane = new BorderPane(); 
    ImageView img = new ImageView("http://..."); 

    img.fitWidthProperty().bind(stage.widthProperty()); 

    pane.setCenter(img); 

    Scene scene = new Scene(pane); 
    stage.setScene(scene); 
    stage.show(); 
} 
+0

thx,太容易了;) – rbs

+2

JavaFX2很簡單,但並不總是不言自明的。一旦你習慣了這些概念,它是一件輕而易舉的事情。 –

+7

\ * facepalm \ *真的嗎?佈局系統本身無法做到這一點? –

1

使用滾動窗格或簡單地窗格來克服這個問題: 示例:

img_view1.fitWidthProperty().bind(scrollpane_imageview1.widthProperty()); 
img_view1.fitHeightProperty().bind(scrollpane_imageview1.heightProperty()); 
+6

如何在FXML中設置它? – Tomi

+0

不起作用。如果我調整窗口的大小,窗格的寬度和高度保持不變,因此imageview也是如此。 – T3rm1

6

這比結合寬度的更好的解決方案屬性(更好,因爲經常在將一個孩子綁定到其容器時,可能無法縮小容器的尺寸,在其他情況下容器甚至可能自動開始增長)。

下面的解決方案依賴重寫ImageView,以便我們可以讓它表現爲「可調整大小」,然後提供最小,首選和最大寬度/高度的實現。實際執行resize()調用也很重要。

class WrappedImageView extends ImageView 
{ 
    WrappedImageView() 
    { 
     setPreserveRatio(false); 
    } 

    @Override 
    public double minWidth(double height) 
    { 
     return 40; 
    } 

    @Override 
    public double prefWidth(double height) 
    { 
     Image I=getImage(); 
     if (I==null) return minWidth(height); 
     return I.getWidth(); 
    } 

    @Override 
    public double maxWidth(double height) 
    { 
     return 16384; 
    } 

    @Override 
    public double minHeight(double width) 
    { 
     return 40; 
    } 

    @Override 
    public double prefHeight(double width) 
    { 
     Image I=getImage(); 
     if (I==null) return minHeight(width); 
     return I.getHeight(); 
    } 

    @Override 
    public double maxHeight(double width) 
    { 
     return 16384; 
    } 

    @Override 
    public boolean isResizable() 
    { 
     return true; 
    } 

    @Override 
    public void resize(double width, double height) 
    { 
     setFitWidth(width); 
     setFitHeight(height); 
    } 
} 
+0

謝謝,這非常有幫助 – sixtstorm1

+0

非常好的工作解決方案。謝謝 –

+0

對於像我這樣的人誰也想要它中心,看看這裏https://stackoverflow.com/questions/32781362/centering-an-image-in-an-imageview;而不是setX setY使用setTranslateX setTranslateY,把中心調用放在resize方法中。 –

1

如果你想的ImageView以適合窗口框架中,使用此行代碼: imageView.fitWidthProperty()綁定(scene.widthProperty())。

請注意,我使用場景的widthProperty而不是舞臺。 實施例:

import java.io.FileNotFoundException; 
import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.image.ImageView; 
import javafx.stage.Stage; 

public class MapViewer extends Application { 

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

    @Override 
    public void start(Stage primaryStage) throws FileNotFoundException { 
     String strTitle = "Titulo de la Ventana"; 
     int w_width = 800; 
     int w_height = 412; 
     primaryStage.setTitle(strTitle); 
     primaryStage.setWidth(w_width); 
     primaryStage.setHeight(w_height); 

     Group root = new Group(); 
     Scene scene = new Scene(root); 

     final ImageView imv = new ImageView("file:C:/Users/utp/Documents/1.2008.png"); 
     imv.fitWidthProperty().bind(scene.widthProperty()); 
     imv.setPreserveRatio(true); 

     root.getChildren().add(imv); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

    } 

} 

階段(primaryStage)的縱橫無線電應該類似於圖像(1.2008.png)

0

這是一個計算寬度的方法,其去除滾動條的寬度的。

第一:
myImageView.setPreserveRatio(true);

顯示器滾動條寬度的變化:

scrollPane.widthProperty().addListener((observable, oldValue, newValue) -> { 
      myImageView.setFitWidth(newValue.doubleValue() - (oldValue.doubleValue() - scrollPane.getViewportBounds().getWidth())); 
     }); 

滾動條的寬度:
oldValue.doubleValue() - scrollPane.getViewportBounds().getWidth()

如何設置初始化寬度是多少?
primaryStage.show();

myImageView.setFitWidth(scrollPane.getViewportBounds().getWidth());

相關問題