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

新手上路,请多包涵

我有一个 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 考虑到它的控制器的高度?

谢谢。

原文由 Nitzan Tomer 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
1 个回答

您应该从内置视频视图扩展。

在显示视频视图之前调用 setVideoSize ,您可以从视频中提取的缩略图获取视频大小。

因此,当调用视频视图的 onMeasure 时, mVideoWidthmVideoHeight 都> 0。

如果你想计算控制器的高度,你可以在 onMeasure 方法中自己做。

希望会有所帮助。

 public class MyVideoView extends VideoView {

        private int mVideoWidth;
        private int mVideoHeight;

        public MyVideoView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public MyVideoView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        public MyVideoView(Context context) {
            super(context);
        }

        public void setVideoSize(int width, int height) {
            mVideoWidth = width;
            mVideoHeight = height;
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // Log.i("@@@", "onMeasure");
            int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
            int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
            if (mVideoWidth > 0 && mVideoHeight > 0) {
                if (mVideoWidth * height > width * mVideoHeight) {
                    // Log.i("@@@", "image too tall, correcting");
                    height = width * mVideoHeight / mVideoWidth;
                } else if (mVideoWidth * height < width * mVideoHeight) {
                    // Log.i("@@@", "image too wide, correcting");
                    width = height * mVideoWidth / mVideoHeight;
                } else {
                    // Log.i("@@@", "aspect ratio is correct: " +
                    // width+"/"+height+"="+
                    // mVideoWidth+"/"+mVideoHeight);
                }
            }
            // Log.i("@@@", "setting size: " + width + 'x' + height);
            setMeasuredDimension(width, height);
        }
}

原文由 ax003d 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏