将视频放置在 VideoView 内 [英] Position Video Inside a VideoView

查看:47
本文介绍了将视频放置在 VideoView 内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我扩展了 VideoView 的 onMeasure 来放大视频以适应全屏视图.

So I have extended VideoView's onMeasure to scale up the video to fit inside a fullscreen view.

方法如下:

public void setVideoAspect(int w,int h){
    wVideo=w;
    hVideo=h;
    onMeasure(w, h);
}
 @Override
 protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
 {
     super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     if(wVideo!=0 && hVideo!=0)
     setMeasuredDimension(wVideo,hVideo);
 }

我使用屏幕的显示指标(宽度、高度)调用 setVideoAspect().问题是这种方法会拉伸视频以适应屏幕.我希望能够保持纵横比.(我有 4:3 视频和 3:2 屏幕尺寸.)我使用以下代码为视图提供保留比率测量:

I call setVideoAspect() with the display metrics (width, hight) of the screen. The problem is that this method stretches the video to fit inside the screen. I want to be able to keep the aspect ratio. (I have 4:3 video and 3:2 screen size.) I used the folowing code to give the retained ratio measurements to the view:

int height =  (int) (metrics.widthPixels*3/(float)4);
                        int width=  metrics.widthPixels;   
                        mVideoView.setVideoAspect(width,height);

所以这可以完成工作,但有一个问题:它给了我一个 4:3 的视频,具有屏幕的宽度并正确缩放高度,但它没有使视频居中.(它只是裁剪视频的底部而不是顶部和底部.)我有一个包含 VideoView 的相对布局,其中 VideoView 的重力设置为中心.

So this does the job but there is an issue: it gives me a 4:3 video with the width of the screen and scales the height correctly, but it doesn't center the video. (It just crops the bottom part of the video instead of the top and the bottom equally.) I have a relative layout containing the VideoView with the gravity of the VideoView set to center.

推荐答案

尝试改用 FrameLayout.我不确定为什么,但是如果我在代码中使用 LinearRelative ,它不会居中,但 FrameLayout 会.这是使我的视频适合屏幕的 XML,保留比例并将其居中:

Try using a FrameLayout instead. I'm not sure why, but if I use a Linear or Relative in my code it won't center, but FrameLayout does. Here is the XML that fit my video to the screen, preserving the ratio and centering it:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg">
    <!-- Video player -->
    <VideoView
        android:id="@+id/surface_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"/>
</FrameLayout>

这篇关于将视频放置在 VideoView 内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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