我正在设置一个媒体视图以根据容器(锚窗格)进行拉伸,同时通过将媒体视图的宽度和高度与容器的宽度和高度绑定来保持媒体的纵横比。我发现仅调整容器的宽度时,媒体视图会调整大小,但媒体始终位于媒体视图的左侧,或者在调整高度时,媒体始终位于顶部。但我希望它以 X 布局和 Y 布局为中心,同时仍调整大小。
我尝试将媒体视图的 xtranslate 与锚窗格宽度的减法和媒体视图宽度除以 2 相结合,但它不起作用,因为媒体视图宽度与容器宽度绑定,它们之间的减法最终将始终产生 0 xtranslate 总是取 0。
mainMediaView.setPreserveRatio(true);
// binding both media view width and height to anchor pane width and height
mainMediaView.fitWidthProperty().bind(centerAnchorPane.widthProperty());
mainMediaView.fitHeightProperty().bind(centerAnchorPane.heightProperty());
// should make media view move to center according to X coordinates
mainMediaView.translateXProperty().bind(centerAnchorPane.widthProperty()
.subtract(mainMediaView.fitWidthProperty()).divide(2));
// should make media view move to center according to Y coordinates
mainMediaView.translateYProperty().bind(centerAnchorPane.heightProperty()
.subtract(mainMediaView.fitHeightProperty()).divide(2));