Android中关于全屏显示和返回原始状态(视频等方面在横竖屏切换用到)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lixiang_Y/article/details/79787276
注意点:
1.在改变全屏与否状态栏的显示或隐藏时,关于屏幕参数的设置,竖屏切换到横屏时需要提前设置状态栏隐藏;而横屏切换到竖屏时需要现用现设,以上设置规则为必要条件。
2.横竖屏切换时,我们最好不要让Activity重新创建(重新走onCreate方法),而是调用onConfigurationChanged方法,这样就不需要重新走onCreate方法。

使用方法:
1.配置清单文件(主要是配置Activity的configChanges参数)
<activity android:name=".activity.ControlActivity"
          android:configChanges="orientation|keyboardHidden|screenSize"
          android:exported="false"
          android:windowSoftInputMode="adjustResize">
</activity>
2.在onConfigurationChanged方法中对横竖屏进行操作
@Override
public void onConfigurationChanged(Configuration newConfig) {

    if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // 当前为横屏
        //动态改变布局
        setOprateVisible(View.VISIBLE);
        
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        surfaceView.setLayoutParams(layoutParams);
    } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        // 当前为竖屏

        //动态改变布局
        setOprateVisible(View.GONE);

        //切换为竖屏显示(提前设置无效,只有现用现设置)
        int flagBack = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //获得当前窗体对象
        Window windowBack = this.getWindow();
        //设置当前窗体为全屏显示
        windowBack.clearFlags(flagBack);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(RelativeLayout.BELOW, R.id.operate_ll);
        surfaceView.setLayoutParams(layoutParams);
    }
    super.onConfigurationChanged(newConfig);

}
3.在要改变横竖屏状态的地方添加代码
@Override
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.max_img:

    //横屏提前设置参数充满整个屏幕(只有提前设置在横竖屏切换时才会生效)
    //定义全屏参数
    int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
    //获得当前窗体对象
    Window window = this.getWindow();
    //设置当前窗体为全屏显示
    window.setFlags(flag, flag);

    //改变屏幕方向(设置为横屏)
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    break;
  case R.id.back1_img:

    //切换竖屏(横屏头部返回键)
  this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

  break;
  }
}
4.设置当按返回键对应的操作(按back键,横屏切换到竖屏,此时是不能销毁activity的;而竖屏状态下则会finish掉这个Activity)

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //按返回键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 检测屏幕的方向:纵向或横向

            if (this.getResources().getConfiguration().orientation

                    == Configuration.ORIENTATION_LANDSCAPE) {

                //当前为横屏,切换至竖屏

              this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


            } else if (this.getResources().getConfiguration().orientation

                    == Configuration.ORIENTATION_PORTRAIT) {

                //当前为竖屏,按退出键后就结束当前activity
                finish();
            }

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
5.在横屏全屏状态下锁屏,之后重新点亮,此时会出现全屏的参数设置失效,所以我们应该在onResume方法中重新设置全屏参数

@Override
protected void onResume() {
        super.onResume();
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 当前为横屏

            //切换为竖屏显示(提前设置无效,只有现用现设置)
            int flagBack = WindowManager.LayoutParams.FLAG_FULLSCREEN;
            //获得当前窗体对象
            Window windowBack = this.getWindow();
            //设置当前窗体为全屏显示
            windowBack.setFlags(flagBack, flagBack);
        }

}

6.实现效果:





展开阅读全文

没有更多推荐了,返回首页