Set max size for ImageView

I resize the image to fit the scene, but the image retains its size even if the scene is larger than the original image.

I installed ImageView.maxWidth(originalImageWidth)

but it didn't work.

I am resizing the ImageView by binding it to the size of the scene.

+2


source to share


2 answers


This will work



Pane root = new VBox(); //or other Pane sub-class
ImageView bg = new ImageView(...);
bg.setPreserveRatio(false);
bg.fitWidthProperty().bind(root.widthProperty());
bg.fitHeightProperty().bind(root.heightProperty());
root.getChildren().addAll(bg);

      

+3


source


May not be too topical, but I found a solution that works for me at least:

  • set the setPreserveRatio parameter to true,
  • set fitHeight to desired max width * ratio
  • bind the fitWidthProperty property to screnes widthProperty


In other words: you can snap one dimension and have a maximum fit to another at the same time.

0


source







All Articles