简体   繁体   中英

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

I want make JavaFX MediaView stretch to fill parent container.

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.

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.

Then i try bind its width and height to anchorpane(parent of 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! 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.

    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)

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. Make a simple runnable program to demonstrate the issue.

Try to add this Action-Listener for rescaling the size:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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