在写播放器的时候,遇到了关于全屏模式的坑。翻阅了网上一些资料和google 官方的demo后,终于把坑填完了。
全屏主要的干扰就是 顶部状态栏stateBar和部分手机的虚拟按键NavigationBar。
这是google demo的源码:
/** * Detects and toggles immersive mode (also known as "hidey bar" mode). */ public void toggleHideyBar() { // BEGIN_INCLUDE (get_current_ui_flags) // The UI options currently enabled are rePResented by a bitfield. // getSystemUiVisibility() gives us that bitfield. int uiOptions = getActivity().getWindow().getDecorView().getSystemUiVisibility(); int newUiOptions = uiOptions; // END_INCLUDE (get_current_ui_flags) // BEGIN_INCLUDE (toggle_ui_flags) boolean isImmersiveModeEnabled = ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions); if (isImmersiveModeEnabled) { Log.i(TAG, "Turning immersive mode mode off. "); } else { Log.i(TAG, "Turning immersive mode mode on."); } // Navigation bar hiding: Backwards compatible to ICS. if (Build.VERSION.SDK_INT >= 14) { newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; } // Status bar hiding: Backwards compatible to Jellybean if (Build.VERSION.SDK_INT >= 16) { newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN; } // Immersive mode: Backward compatible to KitKat. // Note that this flag doesn't do anything by itself, it only augments the behavior // of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample // all three flags are being toggled together. // Note that there are two immersive mode UI flags, one of which is referred to as "sticky". // Sticky immersive mode differs in that it makes the navigation and status bars // semi-transparent, and the UI flag does not get cleared when the user interacts with // the screen. if (Build.VERSION.SDK_INT >= 18) { newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; } getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions); //END_INCLUDE (set_ui_flags) }这个demo比较简单,不做详细的解释。就简单的介绍下Flag等意思。
getActivity().getWindow().getDecorView() //获取当前页面的跟布局。View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;//控制NavigationBar的隐藏于显示。View.SYSTEM_UI_FLAG_FULLSCREEN;//控制stateBar的隐藏与显示。View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;这个标签比较有趣。其实这个标签有两个:View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY 和 View.SYSTEM_UI_FLAG_IMMERSIVE。这两个标签都必须和View.SYSTEM_UI_FLAG_HIDE_NAVIGATION、View.SYSTEM_UI_FLAG_FULLSCREEN 一起使用。下面分为3中情况:
1、只使用View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 和 View.SYSTEM_UI_FLAG_FULLSCREEN 。
这种情况下,在进入全屏模式后,用户有任何操作,SYSTEM_UI_FLAG_HIDE_NAVIGATION和SYSTEM_UI_FLAG_FULLSCREEN 就会被清除。状态栏和虚拟按键会一直可见。除非再次设置SYSTEM_UI_FLAG_HIDE_NAVIGATION和SYSTEM_UI_FLAG_FULLSCREEN。在状态栏和虚拟按键显示变化时会调用View.OnSystemUiVisibilityChangeListener。
2、SYSTEM_UI_FLAG_IMMERSIVE 配合View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 和 View.SYSTEM_UI_FLAG_FULLSCREEN使用。
用户操作不会清除SYSTEM_UI_FLAG_HIDE_NAVIGATION和SYSTEM_UI_FLAG_FULLSCREEN。会一直保持全屏模式。显示切换时也会触发View.OnSystemUiVisibilityChangeListener。还有一个区别就是 ,全屏模式时,从原本状态栏或者虚拟按键的位置 响屏幕内部滑动,会清除SYSTEM_UI_FLAG_HIDE_NAVIGATION和SYSTEM_UI_FLAG_FULLSCREEN,保持可见状态,并且也会触发View.OnSystemUiVisibilityChangeListener监听。3.SYSTEM_UI_FLAG_IMMERSIVE_STICKY配合View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 和 View.SYSTEM_UI_FLAG_FULLSCREEN使用。
用户操作不会清除SYSTEM_UI_FLAG_HIDE_NAVIGATION和SYSTEM_UI_FLAG_FULLSCREEN。会一直保持全屏模式。显示切换时也会触发View.OnSystemUiVisibilityChangeListener。,全屏模式时,,从原本状态栏或者虚拟按键的位置 响屏幕内部滑动, 状态栏 和 虚拟按键栏会暂时可见,一段时间后自动隐藏。与SYSTEM_UI_FLAG_IMMERSIVE 不同的是,因为是临时的显示,所以不会触发View.OnSystemUiVisibilityChangeListener。可能说的有点混乱,可以参考下下面的表格。个人觉得使用toggleHideyBar()来控制有点不顺手,所以拆分成了showBar()和hideBar()。
public void showBar(){ int uiOptions = getWindow().getDecorView().getSystemUiVisibility(); int newUiOptions = uiOptions; boolean isImmersiveModeEnabled = ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions); if (isImmersiveModeEnabled) { Log.i(TAG, "Turning immersive mode mode off. "); //先取 非 后再 与, 把对应位置的1 置成0,原本为0的还是0 if (Build.VERSION.SDK_INT >= 14) { newUiOptions &= ~View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; } if (Build.VERSION.SDK_INT >= 16) { newUiOptions &= ~View.SYSTEM_UI_FLAG_FULLSCREEN; } if (Build.VERSION.SDK_INT >= 18) { newUiOptions &= ~View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; } getWindow().getDecorView().setSystemUiVisibility(newUiOptions); } } public void hideBar() { // The UI options currently enabled are represented by a bitfield. // getSystemUiVisibility() gives us that bitfield. int uiOptions = getWindow().getDecorView().getSystemUiVisibility(); int newUiOptions = uiOptions; boolean isImmersiveModeEnabled = ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions); if (!isImmersiveModeEnabled) { Log.i(TAG, "Turning immersive mode mode on. "); if (Build.VERSION.SDK_INT >= 14) { newUiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; } if (Build.VERSION.SDK_INT >= 16) { newUiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN; } if (Build.VERSION.SDK_INT >= 18) { newUiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; } getWindow().getDecorView().setSystemUiVisibility(newUiOptions); } }暂时就写这么多吧。
新闻热点
疑难解答