android VideoView屏幕旋转为竖屏固定高度,旋转为横屏全屏播放实现


发一张效果图打消你们的迟疑。

Java代码:

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,dip2px(this,235f));
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            videoView.setLayoutParams(params);
        } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            videoView.setLayoutParams(new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
        }
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

把代码中的235改成你想要在竖屏情况下展示VideoView的高度即可。

这个activity要在清单文件里加上:

android:configChanges="orientation|screenSize|keyboardHidden"

xml布局中VideoView的设置:

<VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="235dp"
        android:layout_centerInParent="true"
        />

有疑问请留言,博主会尽快回复。

欢迎浏览博主的其他文章。



猜你喜欢

转载自blog.csdn.net/yonghuming_jesse/article/details/80027487