JavaFX - MediaView 的 isPreserveRatio() 方法



在 JavaFX 中,'MediaView' 类中的 **isPreserveRatio()** 方法用于检索在将媒体内容缩放以适应 MediaView 时是否保留媒体内容的纵横比。

如果 preserveRatioProperty 设置为 'true',则媒体将以保持其宽高比不变的方式进行缩放。

语法

以下是 'MediaView' 类的 'isPreserveRatio()' 方法的语法:

public final boolean isPreserveRatio()

参数

此方法不接受任何参数。

返回值

此方法返回一个布尔值,指示是否保留媒体内容的纵横比 (true) 或不保留 (false)。

示例 1

以下是一个演示 'MediaView' 类的 **isPreserveRatio()** 方法的基本示例:

在此示例中,我们将 'preserveRatio' 设置为 true 以确保在 MediaView 中显示媒体时保留其纵横比。我们使用 isPreserveRatio() 方法检查是否保留了纵横比。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import java.io.File;
public class PreserveRatioExample extends Application {
   @Override
   public void start(Stage stage) {
      File mediaPath = new File("./audio_video/sampleTP.mp4");
      Media media = new Media(mediaPath.toURI().toString());
      MediaPlayer mediaPlayer = new MediaPlayer(media);

      mediaPlayer.play();
      // Create the MediaView and set preserve ratio
      MediaView mediaView = new MediaView(mediaPlayer);
      // This will preserve the aspect ratio of the media
      mediaView.setPreserveRatio(true); 

      boolean isRatioPreserved = mediaView.isPreserveRatio();
      System.out.println("Is aspect ratio preserved? "+ isRatioPreserved);

      // Add MediaView to the scene
      Group root = new Group(mediaView);
      Scene scene = new Scene(root, 550, 270);
      stage.setScene(scene);
      stage.setTitle("Preserve Ratio Example");
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

输出

以下是代码的输出:

Is preservation
Is aspect ratio preserved? true

示例 2

在此示例中,我们检索 VBox 中 isPreserveRatio() 方法的值。我们使用条件语句检查该值是否为 true。如果是 true,则显示 "if" 块中的内容。否则,执行 "else" 块。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.File;

public class PreserveRatioExample extends Application {
   @Override
   public void start(Stage stage) {
      File mediaPath = new File("./audio_video/sampleTP.mp4");
      Media media = new Media(mediaPath.toURI().toString());
      MediaPlayer mediaPlayer = new MediaPlayer(media);

      mediaPlayer.play();
      
      // Create the MediaView and set preserve ratio
      MediaView mediaView = new MediaView(mediaPlayer);
      mediaView.setFitHeight(240);
      mediaView.setFitWidth(480);
      // This will preserve the aspect ratio of the media
      mediaView.setPreserveRatio(true); 

      boolean isRatioPreserved = mediaView.isPreserveRatio();

      // Create a VBox to hold the label and MediaView
      VBox root = new VBox();      
      // Use String.valueOf to convert boolean to String
      Label preserveLabel = new Label();
      if(isRatioPreserved) {
         preserveLabel.setText("Aspect ratio preserved");
      } else {
         preserveLabel.setText("Aspect ratio is not preserved");
      }
      root.getChildren().addAll(mediaView, preserveLabel);

      Scene scene = new Scene(root, 550, 270);
      stage.setScene(scene);
      stage.setTitle("Preserve Ratio Example");
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

输出

以下是代码的输出:

ispreserveratio1
广告
© . All rights reserved.