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
廣告