VideoView 匹配父高度并保持纵横比 [英] VideoView to match parent height and keep aspect ratio

查看:92
本文介绍了VideoView 匹配父高度并保持纵横比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置如下的 VideoView:

I have a VideoView which is set up like this:

<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 与父级的高度相匹配,同时保持纵横比不变,视频将在两侧被剪裁.

But the VideoView matches the width of the parent container, and then the height is set according to the aspect ratio of the loaded movie.
I would like to do just the opposite, I want the VideoView to match the height of the parent while keeping the aspect ratio intact, the video will be clipped on the sides.

我设法拉伸 VideoView 以填充父级,但随后未保留纵横比.

I managed to stretch the VideoView to fill the parent but then the aspect ratio is not kept.

另一件事是,我将 MediaController 添加到 VideoView 中,如下所示:

Another thing is, I'm adding MediaController to the VideoView like this:

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();
    }
});

这很好用,并且控制器始终保持开启状态,但在计算放置视频的位置时没有考虑控制器的高度(因为它垂直居中).

This works great, and the controllers always stay on, but the height of the controllers is not being taken into account when calculating where to place the video (since it's vertically centered).

我的两个问题是:

  1. 如何使 VideoView 与父级的高度匹配,同时保持纵横比?
  2. 如何让 VideoView 考虑到其控制器的高度?

谢谢.

推荐答案

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

You should extends from the built-in video view.

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

Call setVideoSize before video view is shown, you can get video size from thumbnail extracted from video.

这样,当视频视图的 onMeasure 被调用时,mVideoWidth &mVideoHeight > 0.

So that, when video view's onMeasure is called, both mVideoWidth & mVideoHeight are > 0.

如果要计算控制器的高度,可以在onMeasure方法中自行计算.

If you want to account the height of controllers, you can do it yourself in the onMeasure method.

希望会有所帮助.

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);
        }
}

这篇关于VideoView 匹配父高度并保持纵横比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆