【问题标题】:JavaFX down-size ImageViewJavaFX 缩小 ImageView
【发布时间】:2018-02-26 22:38:37
【问题描述】:

我有一个包含一个矩形和顶部图像的组。我希望矩形可以重新调整大小,并且图像应该具有固定大小,除非矩形小于图像。然后图像应该与矩形一起缩小。

图像也应该始终居中并有一些填充。

我已经完成了大部分这些部分,除了图像的缩小部分。我不知道为什么,但图像根本不会缩小。这就是我所拥有的。

    Group group = new Group()
    GeometryNode<Rectangle> rectangle = new GeometryNode<>();
    rectangle.setGeometry(new Rectangle(0, 0, 60, 60));

    ImageView imageView = new ImageView(image);
    imageView.setPreserveRatio(true);

    ImageViewPane imagePane = new ImageViewPane(imageView);
    imagePane.setMinSize(0, 0);
    imagePane.setMaxSize(50, 50);

    StackPane stackPane = new StackPane();

    stackPane.getChildren().add(rectangle);
    stackPane.getChildren().add(imagePane);
    group.getChildren().add(stackPane);

【问题讨论】:

    标签: java image javafx imageview resize


    【解决方案1】:

    如果StackPane 的尺寸发生变化,您希望ImageViewfitWidthfitHeight 属性发生变化。所以你可以做

    double padding = ... ;
    
    imageView.setPreserveRatio(true);
    imageView.fitWidthProperty().bind(
        Bindings.min(stackPane.widthProperty().subtract(padding), image.widthProperty()));
    imageView.fitHeightProperty().bind(
        Bindings.min(stackPane.heightProperty().subtract(padding), image.heightProperty()));
    

    【讨论】:

      猜你喜欢
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      相关资源
      最近更新 更多