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

时间:2018-06-18 22:07:48

标签: java user-interface javafx javafx-8

我正在尝试使用与其父级缩放的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绑定到父级时,父级只能向上扩展而不能向下扩展。因此,如果增加窗口的大小,图片会变大。如果再次缩小图片尺寸,则不会减小图片尺寸。在此示例中,imagePane是StackPane。我也尝试过borderFrame。同样,我尝试使用Pane作为父母。这样,图像可以成功地向上和向下缩放。但是,如果图像与窗格的大小完全不匹配,则图像不会居中,并始终位于窗格的左上角。

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

值:

Top gridpane:
https://gyazo.com/f48ebb39f48d876a02d44792a73eaad4
较低级别的网格窗格:
https://gyazo.com/3399d9f3ab00e8babd36ee3b0e3b27ba
BorderPane:
https://gyazo.com/51c24f8de50ae3865a299fdddf3a1490
ImageView的:
https://gyazo.com/7dfc1071d2b516a83baed301596be2b9

注意,我在这里尝试使用borderpane而不是之前使用的。但是,无论哪个对象是imageview的父对象,结果都是相同的。

1 个答案:

答案 0 :(得分:1)

解决。我一直在乱搞并找到了解决办法。这并不完全直观,但它对我有用:我将图像留在边框内,并且在java代码中,我在网格窗格的相同部分创建了一个窗格作为图像。然后,我将imageview的宽度和高度绑定到窗格。然后,图像的居中以及缩放是正确的。 这是我做的:

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