我需要在 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()
.
~>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
类具有上述属性。