VideoView将匹配父级高度并保持纵横比

38

我有一个VideoView,设置如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/player" 
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

    <VideoView
        android:id="@+id/video"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true" />

    <ProgressBar
        android:id="@+id/loader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

然而,VideoView会自适应父容器的宽度,并根据载入视频的纵横比例设置高度。
我希望做的恰好相反,也就是让VideoView与父容器的高度相同,同时保持纵横比例不变,因此视频将被剪裁在两侧。

我尝试将VideoView拉伸以填充父容器,但这样会破坏纵横比例。

另外一件事是,我要像这样将MediaController添加到VideoView中:

MediaController controllers = new MediaController(this) {
    @Override
    public void hide() {
        if (state != State.Hidden) {
            this.show();
        }
        else {
            super.hide();
        }
    }
};
controllers.setAnchorView(videoView);
videoView.setMediaController(controllers);

videoView.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        controllers.show();
    }
});

这个方法很好用,控制器总是保持开启状态,但在计算视频位置时没有考虑到控制器的高度(因为它垂直居中)。

那么我的两个问题是:

  1. 如何使VideoView适应父容器的高度但保持纵横比?
  2. 如何让VideoView考虑其控制器的高度?

谢谢。


你是在说 WebView 还是 VideoView?你指的是中途更改吗? - George Baker
第一次一个问题回答了我的问题,而不是答案! 我的问题是在全屏模式下视频下方有一个空白区域。我将layout_height设置为match_parent。解决方法是将其设置为wrap_content并给父元素设置黑色背景。此外,还要使VideoView在其父元素中垂直居中。 - Abdelalim Hassouna
保持你的VideoView宽度和高度为match_parent,并且设置android:scaleType="fitXY",这样你就可以按照你的要求得到结果。 - Prince Dholakiya
13个回答

0

我一直在寻找在VideoView中以aspect fill方式显示视频的方法,但是尝试了许多解决方案后,似乎没有一个能够正常工作。

因此,我采用了以下方法,并且它对我有效:

代码:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    // getting screen size
    ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int width = displayMetrics.widthPixels;
    int height = displayMetrics.heightPixels;

    double videoSizeRatio = (double) mVideoHeight / mVideoWidth;
    double screenSizeRatio = (double) height / width;

    if (mVideoWidth > 0 && mVideoHeight > 0) {
        if (videoSizeRatio > screenSizeRatio) { // screen is wider than video width
            height = (int) (videoSizeRatio * width);
        } else if (videoSizeRatio < screenSizeRatio) {
            width = (int) (height / videoSizeRatio);
        }

    }
    setMeasuredDimension(width, height);
}

布局:

 <YourCustomizedVideoView
    android:id="@+id/videoView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    />

1
mVideoWidth / mVideoHeight 是如何计算的? - user12880465
mVideoWidth / mVideoHeight 如其名称所示,它们是视频的宽度和高度。 - eppe
代码没问题,但我们只需要知道你在哪里计算它们了? displayMetrics的声明在哪里? - Mahmoud Mabrok

-1

只需将您的VideoView放置在RelativeLayout中,并为该相对布局设置所需的大小。就像下面的代码一样,

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="300dp">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" />

</RelativeLayout>

它会运作。


-3

你只需要将Videoview小部件放入RelativeLayout中(仅更改xml文件)

这里是参考答案link


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接