6

在我的程序中,我试图全屏播放视频但不破坏播放视频的宽高比,

在android默认播放器中,当您按下它时,它在左上角有一个按钮,它将视频切换到完整视图,而不会导致视频失真,并保持纵横比

如下图:

在此处输入图像描述

我尝试使用以下代码获取全屏视频:

 <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 
     <VideoView android:id="@+id/myvideoview" 
         android:layout_width="fill_parent" 
         android:layout_alignParentRight="true"
          android:layout_alignParentLeft="true" 
         android:layout_alignParentTop="true"
          android:layout_alignParentBottom="true" 
         android:layout_height="fill_parent"> 
      </VideoView> 
   </RelativeLayout> 

上面的代码导致全屏视频没有保持 Aspest Ratio

请任何人建议我如何在不破坏纵横比的情况下获得全屏视频。

我的视频存储在 App 资源文件夹的 raw 文件夹中。

请提供完整的工作代码

我在这里和谷歌搜索了很多,但我找不到我的目标,

在 stackoverflow 中找到的另一篇文章具有相同的问题,但在此链接中没有正确答案:

全屏 videoview 无需拉伸视频

我使用 videoview 在我的应用程序中显示我的视频,如下所示:

  public class Video extends Activity {   
    private VideoView videoview;    
      @Override  
         public void onCreate(Bundle icicle) {  
              super.onCreate(icicle);      
               setContentView(R.layout.main);   
   videoview = (VideoView) findViewById(R.id.count_videoview);  
   videoview.setVideoURI(Uri.parse("android.resource://" + getPackageName()
          +"/"+R.raw.first_video));    
   videoview.setMediaController(new MediaController(this));    
   videoview.requestFocus();    
   videoview.start();   
                 }
          } 

提前致谢 。

4

1 回答 1

0

获取原始视频的高度和宽度。
获取屏幕的高度和宽度(在当前方向)。

float ratioWidth = screenWidth / videoWidth;
float ratioHeight = screenHeight / videoHeight;
float fullWidth;
float fullHeight;
if (ratioWidth < ratioHeight ) {  
    fullWidth = videoWidth * ratioWidth;
    fullHeight = videoHeight * ratioWidth;
} else {
    fullWidth = videoWidth * ratioHeight;
    fullHeight = videoHeight * ratioHeight;
}

设置fullWidthfullHeightVideoView

于 2012-09-30T18:09:51.277 回答