Android 4.4新特性之开启全屏沉浸模式

Android 4.4 KitKat系统全屏沉浸模式实现与扩展
本文详细介绍了Android 4.4 KitKat系统的新特性全屏沉浸模式的实现方法,包括普通全屏、沉浸模式和黏性沉浸模式的区别与应用。同时提供了代码示例来演示如何在应用中启用这些模式,以提升用户体验。

作为Android4.4 KitKat系统的新特性之一“Full-screen Immersive Mode(全屏沉浸模式)”。当启用该模式,应用程序的界面将占据整个屏幕,系统自动将隐藏系统的状态栏和导航栏,让应用程序内容可以在最大显示范围呈现,增加大屏体验,而当需要查看通知的时候只需要从顶部向下滑动就能呼出通知栏。

普通全屏模式(Fullscreen)沉浸模式 (Immersive)黏性沉浸模式 (Sticky Immersive)

说到全屏,我们先看看如何实现普通的全屏模式。

参考资料:

http://libufan.sinaapp.com/?p=55

http://blog.sina.com.cn/s/blog_9f3cecc50101gw7j.html

http://blog.youkuaiyun.com/heng615975867/article/details/9031931

 

接下来我们一起看看如何实现Android4.4系统带来的全屏沉浸模式。

参考资料:

https://developer.android.com/intl/zh-cn/training/system-ui/immersive.html#nonsticky

http://www.cnblogs.com/zhengxt/p/3508485.html

http://www.tuicool.com/articles/RVRneq

 

官方文档上说全屏沉浸模式有两种选择,一种是

 

View.SYSTEM_UI_FLAG_IMMERSIVE

 

这个选择通过组合

View.SYSTEM_UI_FLAG_FULLSCREEN
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

将这个组合通过

setSystemUiVisibility

设置以后可以实现全屏沉浸模式,并且隐藏顶部状态栏和底部的虚拟导航键。但是这个选择通过顶部下滑就恢复原状,同时自动清除这三个标志。

 

另一种是

View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY

同样设置以后可以实现全屏沉浸模式,并且隐藏顶部状态栏和底部的虚拟导航键。与第一种不同的是此时通过顶部下滑会显示出透明的状态栏和虚拟导航栏,在一段时间以后自动回复成全屏沉浸模式,需要手动清除这三个标志回复原状。

 

最后做一下扩展

// This snippet hides the system bars.
private void hideSystemUI() {
    // Set the IMMERSIVE flag.
    // Set the content to appear under the system bars so that the content
    // doesn't resize when the system bars hide and show.
    mDecorView.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 // hide nav bar
            | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
            | View.SYSTEM_UI_FLAG_IMMERSIVE);
}

// This snippet shows the system bars. It does this by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
    mDecorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

 

@Override
public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        decorView.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);}
}

 

注意标志位的版本判断:

    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);
        boolean isImmersiveModeEnabled =
         ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE) == 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;
            newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE;
        }

        getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
        //END_INCLUDE (set_ui_flags)
    }

 

转载于:https://www.cnblogs.com/8dull/p/5372165.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值