Android 切换全屏,取消全屏

切换全屏,取消全屏

/**
 * 切换全屏,取消全屏
 *
 * @param isChecked
 */
private void switchFullScreen(boolean isChecked) {
    if (isChecked) {
        //切换到全屏模式
        //添加一个全屏的标记
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //请求横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        //设置视频播放控件的布局的高度是match_parent
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mVideoRootView.getLayoutParams();
        //将默认的高度缓存下来
        mVideoHeight = layoutParams.height;
        layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT;
        mVideoRootView.setLayoutParams(layoutParams);
    } else {
        //切换到默认模式
        //清除全屏标记
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //请求纵屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //设置视频播放控件的布局的高度是200
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mVideoRootView.getLayoutParams();
        layoutParams.height = mVideoHeight;  //这里的单位是px
        mVideoRootView.setLayoutParams(layoutParams);
    }
}

横竖屏切换时的生命周期总结:

1、不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次

2、设置Activity的android:configChanges=”orientation”时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次

3、设置Activity的android:configChanges=”orientation|keyboardHidden”时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方

你可能感兴趣的:(Android)