gpt4 book ai didi

java - JFX 将图像向上和向下缩放到父级

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:47:16 26 4
gpt4 key购买 nike

我正在尝试拥有一个与其父级一起缩放的 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());

我也尝试过注释行,这些方法的问题是当 imageview 绑定(bind)到父级时,父级只能放大而不能缩小。因此,如果增加窗口的大小,图片就会变大。如果您再次减小图片尺寸,它不会减小图片尺寸。在此示例中,imagePane 是 StackPane。我也尝试过 borderFrame。同样,我尝试过使用 Pane 作为父级。这样,图像就成功地按比例放大和缩小了。但是,如果图像与 Pane 的大小不完全匹配,则图像不会居中并且始终位于 Pane 的左上角。

场景树: https://gyazo.com/45e0daebbfd98dfd3446185d21eb91ee

值(value)观:

顶部网格:
https://gyazo.com/f48ebb39f48d876a02d44792a73eaad4
较低级别的网格 Pane :
https://gyazo.com/3399d9f3ab00e8babd36ee3b0e3b27ba
边框 Pane :
https://gyazo.com/51c24f8de50ae3865a299fdddf3a1490
图片 View :
https://gyazo.com/7dfc1071d2b516a83baed301596be2b9

请注意,我在这里尝试使用边框而不是之前使用的边框。但是,无论哪个对象是 ImageView 的父对象,结果都是一样的。

最佳答案

已解决。我一直在搞乱并找到了解决办法。这不是很直观,但对我有用:我将图像留在 borderframe 内,然后在 Java 代码中,我在与图像相同的网格 Pane 部分创建了一个 Pane 。然后我将 imageview 的宽度和高度绑定(bind)到 Pane 。然后,图像的居中和缩放都是正确的。这是我所做的:

    imageView.setPreserveRatio(true);
Pane pane = new Pane();
testGridPane.add(pane, 1, 1);
imageView.fitWidthProperty().bind(pane.widthProperty());
imageView.fitHeightProperty().bind(pane.heightProperty());

关于java - JFX 将图像向上和向下缩放到父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50918108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com