2016-09-09 34 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()

相关问题