Set maximum size for JavaFX ImageView

一世执手 提交于 2019-12-10 23:43:13

问题


I resize the image view according to the scene but image keep resizing even if the scene is bigger than image original size.

I have set ImageView.maxWidth(originalImageWidth) but it did not work.

I resize the ImageView by binding it's size to the the scene size.


回答1:


This would 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);



回答2:


May not be too relevant anymore but i found a solution that works for me at least:

  • set setPreserveRatio to true,
  • set fitHeight to the wanted maximum width * ratio
  • bind the fitWidthProperty to the screnes widthProperty

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



来源:https://stackoverflow.com/questions/21501090/set-maximum-size-for-javafx-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!