我正在尝试让一个ImageView随其父容器进行缩放。我已经搜索了所有StackOverflow上与此相关的帖子,并尝试了以下方法:
taggedImage.setPreserveRatio(true);
//taggedImage.fitWidthProperty().bind(
// Bindings.min(imagePane.widthProperty(), imagePane.widthProperty()));
//taggedImage.fitHeightProperty().bind(
// Bindings.min(imagePane.heightProperty(), imagePane.heightProperty()));
taggedImage.fitWidthProperty().bind(imagePane.widthProperty());
taggedImage.fitHeightProperty().bind(imagePane.heightProperty());
我也尝试了注释掉的代码行,但这些方法的问题在于当图像视图绑定到父级时,父级只能放大而不能缩小。因此,如果您增加窗口的大小,图片会变得更大。如果您再次减小图片的大小,则不会减小图片的大小。在此示例中,imagePane 是 StackPane。我还尝试过 borderFrame。同样,我尝试使用 Pane 作为父级。这样,图像可以成功缩放。然而,如果图像的大小与面板的大小不完全匹配,则图像不居中且始终位于面板的左上角。
场景树: https://gyazo.com/45e0daebbfd98dfd3446185d21eb91ee 数值:
顶级网格面板:
https://gyazo.com/f48ebb39f48d876a02d44792a73eaad4
下一级网格面板:
https://gyazo.com/3399d9f3ab00e8babd36ee3b0e3b27ba
边界面板:
https://gyazo.com/51c24f8de50ae3865a299fdddf3a1490
图像视图:
https://gyazo.com/7dfc1071d2b516a83baed301596be2b9
imagePane
没有收缩。你需要告诉我们整个场景图树,以及它们的最小/首选/最大尺寸。一般来说,imagePane
绝不能将自己的大小调整为其子级,否则会导致循环依赖。 - JaiBorderPane
(或StackPane
)设置hgrow
和vgrow
为ALWAYS
。外部的GridPane
不应将最小宽度/高度设置为USE_PREF_SIZE
,否则它将永远不会缩小。 - Jai