2016-09-09 37 views
1

我需要顯示的圖像的寬度/高度在imegView並將其與原單圖像尺寸,該尺寸在imageView.getImage()。的getWidth()/的getHeight(),並且如果用戶調整其大小形成應用GUI聽變化。如何獲得在javafx imageView中顯示圖像的寬度/高度?

我從imageView.fitWidthProperty()這個大小和高度相似,但我得到在它的ImageView的不是圖像尺寸: enter image description here

我得到藍色的大小是ImageView的,但我需要顯示的圖像的大小(綠色) 。我怎樣才能得到它作爲屬性來監聽用戶調整窗口應用程序的大小(綠色也改變大小,但它似乎有最大和最小大小,所以它停止與最大或最小的imageView調整大小)。

我使用這個屬性來計算原單圖像的大小%以下藍色矩形右下角。

可能嗎?

E:下面是此標籤的FXML:

<BorderPane fx:id="root" fx:controller="..." 
     xmlns:fx="..." xmlns="..." stylesheets="@...css"> 
<center> 
    <StackPane> 
     <ImageView fx:id="..."/> 
     <fx:include source="...fxml" fx:id="..."/> 
    </StackPane> 
</center> 
<bottom> 
    <VBox fx:id="..." minHeight="110" styleClass="small-padding" spacing="5"> 
     <HBox alignment="CENTER_RIGHT" spacing="5"> 
      <fx:include source="...fxml" resources="..." 
         fx:id="..."/> 
     </HBox> 
     <TextArea fx:id="..." promptText="%..." styleClass="-fx-description-text-area"/> 
    </VBox> 
</bottom> 

回答

1

〜> 1)

Image未調整大小,以覆蓋所有的ImageView具有的事實使用preserveRatio方法。如果您想要覆蓋setPreserveRatio(false); 以及的組合

〜> 2)

綠色邊框是原始圖像的大小。

〜> 3)

之前添加ImageImageView你可以這樣做:

Image image = new Image("FILE:..."); 
image.getWidth(); 
image.getHeight(); 

這是圖像的原始大小。

對於圖像的大小時,它會顯示ImageView內:

imageView.getFitWidth(); 
imageView.getFitHeight(); 

對於ImageView裏面的場景大小(你的問題的藍色邊框):

imageView.getWidth(); 
imageView.getHeight(); 

ImageView類具有上述屬性。

+1

imageView.getFitWidth()不起作用。但我發現我需要imageView.boundsInParentProperty()所以問題解決了。 – xav9211

0

我不知道,你可以直接用它來獲得你想要的數字,但你可能會提出這是JavaFX的未來版本的改進所提出的財產。

你現在可以做的是寫一些代碼跟蹤這對顯示圖像的大小產生影響,然後自己計算顯示尺寸的ImageView的所有proerties。這對我來說聽起來不太複雜。

0

ImageView設置爲保留寬高比時,必須手動計算真實尺寸。至少我還沒有找到另一種方式。

double aspectRatio = image.getWidth()/image.getHeight(); 
double realWidth = Math.min(imageView.getFitWidth(), imageView.getFitHeight() * aspectRatio); 
double realHeight = Math.min(imageView.getFitHeight(), imageView.getFitWidth()/aspectRatio); 

解釋這一點:getFitHeight/Width是你的藍色矩形。在保留源圖像的高寬比時,藍色矩形的至少一邊必須與綠色矩形的相應邊具有相同的長度。此外,綠色矩形將始終位於藍色區域內,因此可撥打Math.min()

相關問題