Android 全面屏适配及判断是否为全面屏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zuo_er_lyf/article/details/82259239

一,全面屏的适配

全面屏出现后,如果不做适配,屏幕上会出现上下黑边,影响视觉效果。

针对此问题,Android官方提供了适配方案,即提高App所支持的最大屏幕纵横比,实现起来也比较简单,在AndroidManifest.xml中做如下配置即可,在AndroidManifet里的<application>下声明:

<meta-data android:name="android.max_aspect"  
           android:value="ratio_float"/>

将ratio_float设置为2.1即可适配一众全面屏手机,即:

 <meta-data
            android:name="android.max_aspect"
            android:value="2.1" />

二,判断是否为全面屏

很多的手机是有虚拟导航栏的,特别是华为手机,有人提议通过判断是否含有虚拟导航栏,不就可以判断是否为全面屏了吗?

/**
     * 判断设备是否存在NavigationBar(虚拟导航栏)
     *
     * @return true 存在, false 不存在
     */
    public static boolean deviceHasNavigationBar() {
        boolean haveNav = false;
        try {
            //1.通过WindowManagerGlobal获取windowManagerService
            // 反射方法:IWindowManager windowManagerService = WindowManagerGlobal.getWindowManagerService();
            Class<?> windowManagerGlobalClass = Class.forName("android.view.WindowManagerGlobal");
            Method getWmServiceMethod = windowManagerGlobalClass.getDeclaredMethod("getWindowManagerService");
            getWmServiceMethod.setAccessible(true);
            //getWindowManagerService是静态方法,所以invoke null
            Object iWindowManager = getWmServiceMethod.invoke(null);

            //2.获取windowMangerService的hasNavigationBar方法返回值
            // 反射方法:haveNav = windowManagerService.hasNavigationBar();
            Class<?> iWindowManagerClass = iWindowManager.getClass();
            Method hasNavBarMethod = iWindowManagerClass.getDeclaredMethod("hasNavigationBar");
            hasNavBarMethod.setAccessible(true);
            haveNav = (Boolean) hasNavBarMethod.invoke(iWindowManager);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return haveNav;
    }

通过检验发现,此方法并不能判断是否为全面屏,因为全面屏的手机通过以上方法,判断的值为:true。

因此,需要从其他方面进行判断,全面屏与传统屏的区别在于,屏幕的纵横比,所以,可以从纵横比方面做出判断,详细代码如下:

 /**
     * 判断是否是全面屏
     */
    private volatile static boolean mHasCheckAllScreen;
    private volatile static boolean mIsAllScreenDevice;

    public static boolean isAllScreenDevice(Context context) {
        if (mHasCheckAllScreen) {
            return mIsAllScreenDevice;
        }
        mHasCheckAllScreen = true;
        mIsAllScreenDevice = false;
        // 低于 API 21的,都不会是全面屏。。。
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            return false;
        }
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (windowManager != null) {
            Display display = windowManager.getDefaultDisplay();
            Point point = new Point();
            display.getRealSize(point);
            float width, height;
            if (point.x < point.y) {
                width = point.x;
                height = point.y;
            } else {
                width = point.y;
                height = point.x;
            }
            if (height / width >= 1.97f) {
                mIsAllScreenDevice = true;
            }
        }
        return mIsAllScreenDevice;
    }

例如:此判断在PopupWindow兼容适配有虚拟导航栏手机和全面屏的显示时,底部被虚拟导航栏遮盖,或者全面屏手机下方有间隙。

猜你喜欢

转载自blog.csdn.net/zuo_er_lyf/article/details/82259239