对于使用android中的videoview的某些设备,视频不能在横向模式下全屏显示

时间:2017-06-06 16:19:41

标签: android xml android-linearlayout android-videoview

我的视频在纵向模式下工作正常,但在横向上它不适合全屏(我不想使用布局 - 土地)。我试图以编程方式更改视图,但这不能按预期工作。我尝试使用以下代码获得设备的分辨率:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
WindowManager wm =(WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
height = metrics.heightPixels 
width =  metrics.widthPixels

在分辨率为1080 * 1920的设备上进行测试,我得到的高度和宽度值分别为1080和1776。 (对于某些设备,我得到正确的值,videoview在两个方向都可以正常工作)

我遇到了以下链接中提出的同样问题 -

video not taking full screen in landscape mode using videoview android

1 个答案:

答案 0 :(得分:0)

使用此代码解决。

 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT
            );
    private void enterFullScreen(){
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            mVideoView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            mVideoView.setLayoutParams(layoutParams);
        }

    private void exitFullScreen(){
              this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                mVideoView.setSystemUiVisibility(0);
                isFullScreen = false;
                mFullScreen.setVisibility(View.INVISIBLE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                    layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
                    layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                }else {
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,0);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,0);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,0);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
                }
                mFullScreen.setVisibility(View.VISIBLE);
        enter code here`enter code here`
                mVideoView.setLayoutParams(layoutParams);
            }