Android刘海屏、水滴屏全面屏适配方案,2021年Android社招面试题

  1. 对Application生效,意味着该应用的所有页面,系统都不会做竖屏场景的特殊下移或者是横屏场景的右移特殊处理:








  1. 对Activity生效,意味着可以针对单个页面进行刘海屏适配,设置了该属性的Activity系统将不会做特殊处理:




方案二

对Application生效,意味着该应用的所有页面,系统都不会做竖屏场景的特殊下移或者是横屏场景的右移特殊处理

我的NotchScreenTool中使用的就是方案二,如果需要针对Activity,建议自行修改。

  1. 设置应用窗口在华为刘海屏手机使用刘海区

/刘海屏全屏显示FLAG/
public static final int FLAG_NOTCH_SUPPORT=0x00010000
;
/**

  • 设置应用窗口在华为刘海屏手机使用刘海区
  • @param window 应用页面window对象
    */
    public static void setFullScreenWindowLayoutInDisplayCutout(Window window) {
    if (window == null) {
    return;
    }
    WindowManager.LayoutParams layoutParams = window.getAttributes();
    try {
    Class layoutParamsExCls = Class.forName(“com.huawei.android.view.LayoutParamsEx”);
    Constructor con=layoutParamsExCls.getConstructor(LayoutParams.class);
    Object layoutParamsExObj=con.newInstance(layoutParams);
    Method method=layoutParamsExCls.getMethod(“addHwFlags”, int.class);
    method.invoke(layoutParamsExObj, FLAG_NOTCH_SUPPORT);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |InstantiationException
    | InvocationTargetException e) {
    Log.e(“test”, “hw add notch screen flag api error”);
    } catch (Exception e) {
    Log.e(“test”, “other Exception”);
    }
    }
  1. 清除添加的华为刘海屏Flag,恢复应用不使用刘海区显示

/**

  • 设置应用窗口在华为刘海屏手机使用刘海区
  • @param window 应用页面window对象
    */
    public static void setNotFullScreenWindowLayoutInDisplayCutout (Window window) {
    if (window == null) {
    return;
    }
    WindowManager.LayoutParams layoutParams = window.getAttributes();
    try {
    Class layoutParamsExCls = Class.forName(“com.huawei.android.view.LayoutParamsEx”);
    Constructor con=layoutParamsExCls.getConstructor(LayoutParams.class);
    Object layoutParamsExObj=con.newInstance(layoutParams);
    Method method=layoutParamsExCls.getMethod(“clearHwFlags”, int.class);
    method.invoke(layoutParamsExObj, FLAG_NOTCH_SUPPORT);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |InstantiationException
    | InvocationTargetException e) {
    Log.e(“test”, “hw clear notch screen flag api error”);
    } catch (Exception e) {
    Log.e(“test”, “other Exception”);
    }
    }
适配小米Android O设备
  1. 判断是否是刘海屏

private static boolean isNotch() {
try {
Method getInt = Class.forName(“android.os.SystemProperties”).getMethod(“getInt”, String.class, int.class);
int notch = (int) getInt.invoke(null, “ro.miui.notch”, 0);
return notch == 1;
} catch (Throwable ignore) {
}
return false;
}

  1. 设置显示到刘海区域

@Override
public void setDisplayInNotch(Activity activity) {
int flag = 0x00000100 | 0x00000200 | 0x00000400;
try {
Method method = Window.class.getMethod(“addExtraFlags”,
int.class);
method.invoke(activity.getWindow(), flag);
} catch (Exception ignore) {
}
}

  1. 获取刘海宽高

public static int getNotchHeight(Context context) {
int resourceId = context.getResources().getIdentifier(“notch_height”, “dimen”, “android”);
if (resourceId > 0) {
return context.getResources().getDimensionPixelSize(resourceId);
}
return 0;
}

public static int getNotchWidth(Context context) {
int resourceId = context.getResources().getIdentifier(“notch_width”, “dimen”, “android”);
if (resourceId > 0) {
return context.getResources().getDimensionPixelSize(resourceId);
}
return 0;
}

适配oppoAndroid O设备
  1. 判断是否是刘海屏

@Override
public boolean hasNotch(Activity activity) {
boolean ret = false;
try {
ret = activity.getPackageManager().hasSystemFeature(“com.oppo.feature.screen.heteromorphism”);
} catch (Throwable ignore) {
}
return ret;
}

  1. 获取刘海的左上角和右下角的坐标

/**

  • 获取刘海的坐标
  • 属性形如:[ro.oppo.screen.heteromorphism]: [378,0:702,80]
  • 获取到的值为378,0:702,80
  • (378,0)是刘海区域左上角的坐标
  • (702,80)是刘海区域右下角的坐标
    */
    private static String getScreenValue() {
    String value = “”;
    Class<?> cls;
    try {
    cls = Class.forName(“android.os.SystemProperties”);
    Method get = cls.getMethod(“get”, String.class);
    Object object = cls.newInstance();
    value = (String) get.invoke(object, “ro.oppo.screen.heteromorphism”);
    } catch (Throwable ignore) {
    }
    return value;
    }

Oppo Android O机型不需要设置显示到刘海区域,只要设置了应用全屏就会默认显示。

因此Oppo机型必须适配。

适配总结

根据上述功能,我将其整理成了一个依赖库:NotchScreenTool

使用起来很简单:

// 支持显示到刘海区域
NotchScreenManager.getInstance().setDisplayInNotch(this);
// 获取刘海屏信息
NotchScreenManager.getInstance().getNotchInfo(this, new INotchScreen.NotchScreenCallback() {
@Override
public void onResult(INotchScreen.NotchScreenInfo notchScreenInfo) {
Log.i(TAG, "Is this screen notch? " + notchScreenInfo.hasNotch);
if (notchScreenInfo.hasNotch) {
for (Rect rect : notchScreenInfo.notchRects) {
Log.i(TAG, "notch screen Rect = " + rect.toShortString());
}
}
}
});

o notchScreenInfo) {
Log.i(TAG, "Is this screen notch? " + notchScreenInfo.hasNotch);
if (notchScreenInfo.hasNotch) {
for (Rect rect : notchScreenInfo.notchRects) {
Log.i(TAG, "notch screen Rect = " + rect.toShortString());
}
}
}
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值