位置视频里面VideoView [英] Position Video Inside a VideoView

查看:130
本文介绍了位置视频里面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的屏幕大小),我用了如下因素code给予保留比率测量到的观点:

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代替。我不知道为什么,但如果我用一个线性或相对在我的code也不会中锋,但框时。这是适合我的视频画面,preserving的比例,并围绕它的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 Frame 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">
</VideoView>
</FrameLayout>

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

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