如何在javafx imageView中获取显示图像的宽度/高度?

xav*_*211 6 image javafx-2

我需要在 imegView 中获取显示图像的宽度/高度,并将其与 imageView.getImage().getWidth()/getHeight() 中的原始图像大小进行比较,并在用户从应用程序 GUI 中调整其大小时监听更改。

我从imageView.fitWidthProperty()高度上得到了这个大小,但我得到了 imageView 的大小而不是其中的图像: 在此处输入图片说明

我得到的是 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>
Run Code Online (Sandbox Code Playgroud)

小智 5

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

稍微解释一下: getFitHeight/Width 是你的蓝色矩形。保留源图像的长宽比时,蓝色矩形的至少一侧必须与绿色矩形的相应侧具有相同的长度。此外,绿色矩形将始终位于蓝色矩形内部,因此调用Math.min().


GOX*_*LUS 3

~>1)

事实上,Image没有调整大小以覆盖所有ImageView与 keepRatio 方法有关。如果您想setPreserveRatio(false); 用以下组合来覆盖setSmooth( true );

〜>2)

绿色边框是原始图像的大小。

~>3)

在将 添加Image到 之前,您可以执行以下ImageView操作:

Image image = new Image("FILE:...");
image.getWidth( );
image.getHeight( );
Run Code Online (Sandbox Code Playgroud)

这就是图像的原始尺寸。

至于图像显示在里面时的大小ImageView

imageView.getFitWidth();
imageView.getFitHeight();
Run Code Online (Sandbox Code Playgroud)

ImageView对于场景内部的大小(问题中的蓝色边框):

imageView.getWidth();
imageView.getHeight();
Run Code Online (Sandbox Code Playgroud)

ImageView类具有上述属性。

  • imageView.getFitWidth() 不起作用。但我在 imageView.boundsInParentProperty() 中找到了我需要的东西,所以问题解决了。 (6认同)
  • 同样在这里,您需要使用:`listView.boundsInParentProperty().get().getWidth()` (4认同)