简体   繁体   English

如何使JavaFX MediaView拉伸媒体填充父级?

[英]How do I make JavaFX MediaView stretch media to fill parent?

I want make JavaFX MediaView stretch to fill parent container. 我想使JavaFX MediaView拉伸以填充父容器。

I tried some methods from google before,but nothing helpful(maybe i'm not apply it right),and there is a question with same name as this,but solutions in that question maybe not suit me. 我之前尝试过Google的一些方法,但没有帮助(也许我没有正确应用),并且有一个与此名称相同的问题,但是该问题中的解决方案可能不适合我。

first i use the ahchorpane to anchor mediaview but find it can not stretch mediaview,and I don't know why because other controls like button can work. 首先,我使用ahchorpane锚定mediaview,但发现它无法拉伸mediaview,而且我不知道为什么,因为其他控件(如按钮)可以工作。

Then i try bind its width and height to anchorpane(parent of mediaview) 然后我尝试将其宽度和高度绑定到anchorpane(mediaview的父对象)

    Region region=(Region) mediaplayer.getParent();
    mediaplayer.setPreserveRatio(false);
    mediaplayer.fitWidthProperty().bind(region.widthProperty());
    mediaplayer.fitHeightProperty().bind(region.heightProperty());

it's can expand mediaview exactly when i resize window,but can't Shrink down! 调整窗口大小时,它可以完全扩展mediaview,但不能缩小! I guess it's maybe because size of region depend on it childs? 我猜可能是因为区域的大小取决于孩子吗?

finally,i try to bind mediaview'size to stage'size,it's work,but the code looks like ugly,because i need calculate size manully. 最后,我尝试将mediaview的大小绑定到stage的大小,这是可行的,但是代码看起来很难看,因为我需要手动计算大小。

    mediaplayer.setPreserveRatio(false);
    mediaplayer.fitWidthProperty().bind(stage.widthProperty().subtract(200));
    mediaplayer.fitHeightProperty().bind(stage.heightProperty().subtract(135));

are there have any better solutions? 有没有更好的解决方案?

Using the code below you will see that you are able to stretch and shrink the mediaView as much as you like ( depending of the stage dimensions) 使用下面的代码,您会看到您可以随意缩放MediaView(取决于舞台尺寸)

import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class TestApp extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {

        BorderPane pane = new BorderPane();

        FileChooser fc = new FileChooser();
        File mediaFile = fc.showOpenDialog(null);

        MediaView moviePlayer;

        if (mediaFile != null) {
            MediaPlayer player = new MediaPlayer(new Media(mediaFile.toURI().toString()));

            moviePlayer = new MediaView(player);

            moviePlayer.setPreserveRatio(false);
            moviePlayer.fitWidthProperty().bind(pane.widthProperty());
            moviePlayer.fitHeightProperty().bind(pane.heightProperty());

            pane.getChildren().add(moviePlayer);
            player.play();

        }

        Scene scene = new Scene(pane, 300, 300);
        stage.setScene(scene);
        stage.show();
    }
}

This code works with AnchorPane as well so I guess there is something else wrong with your code if you still have the issue or I haven't understand what you need. 该代码也可与AnchorPane一起使用,因此,如果您仍然遇到问题或我不了解您的需求,我想您的代码还有其他问题。 Make a simple runnable program to demonstrate the issue. 制作一个简单的可运行程序来演示该问题。

Try to add this Action-Listener for rescaling the size: 尝试添加此Action-Listener以重新调整大小:

MediaView.getParent().layoutBoundsProperty().addListener(new ChangeListener<Bounds>() {
        @Override
        public void changed(ObservableValue<? extends Bounds> observable, Bounds oldValue, Bounds newValue) {
            mediaView.setFitHeight(newValue.getHeight());
            mediaView.setFitWidth(newValue.getWidth());
        }
    });

This should be called every time the parent-size got changed or maximized and then the view should be resized to parent, too. 每次更改父级大小或最大化父级大小时都应调用此方法,然后也应将视图大小调整为父级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM