如何保持 Android VideoView 的纵横比仅与其高度成比例?

How can I preserve my Android VideoView's aspect ratio in proportion to its height only?

我想设置一个充满屏幕并保持其纵横比的视频。但是,我不希望视频与 phone.

的纵横比匹配

如果我的屏幕宽高比为 18.9:9,而视频的宽高比为 16:9,那么当我将视频放入继承 fill_parent 的 VideoView 时,视频会被拉伸。如果我不设置 fill_parent,则视频会保留其宽高比,但只占屏幕的不到 1/3,因为宽高比是由其宽度而非高度决定的。这是不可取的。

我想用视频垂直填充屏幕,无论是否剪掉了部分宽度。

当前代码:

window.setFormat(PixelFormat.TRANSLUCENT)
val videoView = findViewById < VideoView > (videoView)
val video = Uri.parse("android.resource://" + packageName + "/" +
    R.raw.bgvideo)
videoView.setVideoURI(video)
videoView.setOnPreparedListener {
    mp: MediaPlayer - >
        mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING)
    mp.isLooping = true
    mp.setScreenOnWhilePlaying(false)
}
videoView.start()

XML:

<VideoView
    android:id="@+id/videoView"
    android:layout_width="2000dp"
    android:layout_height="match_parent" />

<!--- Sample Foreground Content !--->
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="250dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="24dp"
    android:contentDescription="@string/logo"
    android:src="@drawable/logo" />

当前结果:

期望的结果:

以编程方式调整 VideoView 的尺寸。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!--parent needs to be matching the screen height-->
    <!--VideoView layout_width doesn't matter-->

    <VideoView
        android:id="@+id/video_player"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>
// at onCreate or anywhere

        // post, because view dimension is not initialized yet in here
        videoView.post(new Runnable() {
            @Override
            public void run() {
                // do resizing here
                // AR = aspect ratio

                float videoARWidth = 16.f;
                float videoARHeight = 9.f;

                // Phone screen aspect ratio height
                float screenARHeight = 9.f;

                // scale to screen AR height
                float videoScale = screenARHeight / videoARHeight;

                float videoARRatio = videoARWidth / videoARHeight;

                // scale the ratio to screen
                float videoScaledARRatio = videoARRatio * videoScale;

                ViewGroup.LayoutParams layoutParams = videoView.getLayoutParams();

                // make sure the VideoView matches the screen height
                layoutParams.width = (int)(videoView.getHeight() * videoScaledARRatio);
                videoView.setLayoutParams(layoutParams);
            }
        });

此代码不关心屏幕宽度。

宽度将始终缩放到屏幕高度。实际上,它总是会缩放到它的 (VideoView) 自己的高度。但是正如我对代码的评论,使用 layout_height = "match_parent" 来匹配屏幕。

如果屏幕宽度较大,我不确定您是否希望宽度适合。

如果屏幕宽度较大,视频宽度将不会填满屏幕。