如何使JavaFX MediaView拉伸和收缩媒体以填充/适合父容器? [英] How do I make JavaFX MediaView stretch and shrink media to fill/fit parent container?

查看:165
本文介绍了如何使JavaFX MediaView拉伸和收缩媒体以填充/适合父容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使JavaFX MediaView拉伸并收缩以填充/适合父容器?

How do I make JavaFX MediaView stretch and shrink media to fill/fit parent container?

这里已经有一个问题:

我如何制作JavaFX MediaView拉伸媒体可以填充父容器?

以及此代码的解决方案:

and the solution with this code:

<Pane fx:id="mediaViewPane">
  <children>
     <MediaView fx:id="videoView" fitHeight="${mediaViewPane.height}" fitWidth="${mediaViewPane.width}" layoutX="1.0" />
  </children>
</Pane>

可以使视频变大以填充容器,但是不允许容器小于视频的大小,因此,它不会收缩,只是在控件落入容器外时才开始隐藏控件场景.

works for making the video bigger to fill the container, but it doesn't allow the container to be smaller than the size of the video, so, it doesn't shrink, it just starts hiding controls as they fall outside the scene.

有什么想法可以伸展但也可以缩小MediaView以使其合适吗?

Any ideas how to stretch but also shrink the MediaView to fit?

推荐答案

我已经做到了这一点,尽管我对这是最好的方法并不十分自信.首先,正如fabian指出的那样,我需要将MediaView设置为不受管理,以便可以将父Pane的大小调整为任意大小,而不会受到MediaView的限制:

I have achieved this, although I'm not super confident about this being the best approach. First, as fabian pointed out, I need to set the MediaView as not managed so that the parent Pane can be resized to any size without the MediaView constraining it:

<Pane fx:id="mediaViewPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0">
    <MediaView fx:id="mediaView" StackPane.alignment="CENTER" managed="false"/>
</Pane>

然后,在一次rezise事件中,我将MediaView调整为适合父级的大小,然后获取视频的实际大小并将其偏移到中心:

Then, on a rezise event, I resize the MediaView to fit in the parent, then get the actual size of the video and offset it to center:

InvalidationListener resizeMediaView = observable -> {
    mediaView.setFitWidth(mediaViewPane.getWidth());
    mediaView.setFitHeight(mediaViewPane.getHeight());

    // After setting a big fit width and height, the layout bounds match the video size. Not sure why and this feels fragile.
    Bounds actualVideoSize = mediaView.getLayoutBounds();
    mediaView.setX((mediaView.getWidth() - actualVideoSize.getWidth()) / 2);
    mediaView.setY((mediaView.getHeight() - actualVideoSize.getHeight()) / 2);
};
mediaViewPane.heightProperty().addListener(resizeMediaView);
mediaViewPane.widthProperty().addListener(resizeMediaView);

这篇关于如何使JavaFX MediaView拉伸和收缩媒体以填充/适合父容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆