我在AnchorPane中有一个ImageView,希望当AnchorPane大小改变时,以相同的比例调整图片大小。
请问有人能够解释一下如何实现吗?
非常感谢。
您可以将图像视图的fitWidth
和fitHeight
属性绑定到锚点面板的宽度和高度上:
imageView.fitWidthProperty().bind(anchorPane.widthProperty());
imageView.fitHeightProperty().bind(anchorPane.heightProperty());
imageView.setPreserveRatio(true);
对我有效的唯一方法。 在视图上,监听 AnchorPane 的更改并设置 ImageView 大小。
init {
myAnchorPane.widthProperty().addListener { observable, oldValue, newValue ->
myImageView.fitWidth = myAnchorPane.width
myImageView.fitHeight = 0.0
}
}
imageView
的managed
属性设置为false
,以防止其影响AnchorPane
的min
和pref
大小。 - fabianimageView.fitHeightProperty().bind(anchorPane.heightProperty().subtract(30))
?(将30
替换为您需要的任何值)。或者只需在另一个面板中包装图像视图 - 比如一个StackPane
,并将StackPane
放置在AnchorPane
中(然后绑定到堆栈面板的宽度和高度)。 - James_D