【问题标题】:How can I preserve my Android VideoView's aspect ratio in proportion to its height only?如何保持我的 Android VideoView 纵横比仅与其高度成比例?
【发布时间】:2019-10-31 14:56:03
【问题描述】:

我想设置一个充满屏幕并保留其纵横比的视频。但是,我不希望视频与手机的纵横比相匹配。

如果我的屏幕纵横比是 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" />

当前结果:

期望的结果:

【问题讨论】:

  • 这个问题没有解决方案,它是在 6 年前发布的。我们可能应该继续我的问题。
  • 如果您有视频的分辨率,为什么不直接以编程方式调整VideoView 的尺寸。
  • @IsraeldelaCruz 视频的纵横比是 16:9,但屏幕的纵横比是 18.9:9。如果我使用 fill_parent,视频会显得拉伸。我希望视频填满屏幕,即使部分高度会被裁剪掉。

标签: android kotlin android-videoview


【解决方案1】:

以编程方式调整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" 来匹配屏幕。

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

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

【讨论】:

  • 感谢您的详细回答。现在才经历。什么是AR高度?您还可以提供您的 XML 吗?除非我遗漏了什么,否则您的代码似乎没有什么不同。我手机的宽高比是 19.5:9。我希望高度填满屏幕,但宽度不受屏幕限制(它可能会溢出)。
  • 我已经提供了 xml。 AR 是纵横比。
  • 我说得太早了。这确实达到了我想要的效果。只需确保父布局是 FrameLayout。您可能还想在此处发布您的解决方案:stackoverflow.com/questions/13603553/…,因为我相信尚未选择任何答案,而且没有一个对我有用。
猜你喜欢
  • 2012-11-16
  • 2012-06-02
  • 2016-12-17
  • 2015-06-13
  • 2015-12-02
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 2014-12-13
相关资源
最近更新 更多