資源簡(jiǎn)介
小米華為OPPO/VIVO手機(jī)劉海屏適配
判斷是否有劉海屏,根據(jù)不同的上述4類機(jī)型,進(jìn)行適配。
代碼依據(jù)于各家的開(kāi)發(fā)適配文檔,未來(lái)可能有更新。
判斷是否有劉海屏,根據(jù)不同的上述4類機(jī)型,進(jìn)行適配。
代碼依據(jù)于各家的開(kāi)發(fā)適配文檔,未來(lái)可能有更新。
代碼片段和文件信息
import?android.content.Context;
import?android.util.Log;
import?android.view.Window;
import?android.view.WindowManager;
import?java.lang.reflect.Constructor;
import?java.lang.reflect.Method;
public?class?Utils?{
????/**
?????*?是否劉海
?????*
?????*?@param?context
?????*?@return
?????*/
????public?static?boolean?hasNotchInScreen(Context?context)?{
????????boolean?ret?=?false;
????????try?{
????????????ClassLoader?cl?=?context.getClassLoader();
????????????Class?HwNotchSizeUtil?=?cl.loadClass(“com.huawei.android.util.HwNotchSizeUtil“);
????????????Method?get?=?HwNotchSizeUtil.getMethod(“hasNotchInScreen“);
????????????ret?=?(boolean)?get.invoke(HwNotchSizeUtil);
????????}?catch?(ClassNotFoundException?e)?{
????????????Log.e(“test“?“hasNotchInScreen?ClassNotFoundException“);
????????}?catch?(NoSuchMethodException?e)?{
????????????Log.e(“test“?“hasNotchInScreen?NoSuchMethodException“);
????????}?catch?(Exception?e)?{
????????????Log.e(“test“?“hasNotchInScreen?Exception“);
????????}
????????return?ret;
????}
????/**
?????*?獲取劉海尺寸:width、heightint[0]值為劉海寬度?int[1]值為劉海高度。
?????*
?????*?@param?context
?????*?@return
?????*/
????public?static?int[]?getNotchSize(Context?context)?{
????????int[]?ret?=?new?int[]{0?0};
????????try?{
????????????ClassLoader?cl?=?context.getClassLoader();
????????????Class?HwNotchSizeUtil?=?cl.loadClass(“com.huawei.android.util.HwNotchSizeUtil“);
????????????Method?get?=?HwNotchSizeUtil.getMethod(“getNotchSize“);
????????????ret?=?(int[])?get.invoke(HwNotchSizeUtil);
????????}?catch?(ClassNotFoundException?e)?{
????????????Log.e(“test“?“getNotchSize?ClassNotFoundException“);
????????}?catch?(NoSuchMethodException?e)?{
????????????Log.e(“test“?“getNotchSize?NoSuchMethodException“);
????????}?catch?(Exception?e)?{
????????????Log.e(“test“?“getNotchSize?Exception“);
????????}
????????return?ret;
????}
????/**
?????*?設(shè)置使用劉海區(qū)域
?????*
?????*?@param?window
?????*/
????public?static?void?setFullScreenWindowLayoutInDisplayCutout(Window?window)?{
????????if?(window?==?null)?{
????????????return;
????????}
????????try?{
????????????WindowManager.LayoutParams?layoutParams?=?window.getAttributes();
????????????Class?layoutParamsExCls?=?Class.forName(“com.huawei.android.view.LayoutParamsEx“);
????????????Constructor?con?=?layoutParamsExCls.getConstructor(WindowManager.LayoutParams.class);
????????????object?layoutParamsExObj?=?con.newInstance(layoutParams);
????????????Method?method?=?layoutParamsExCls.getMethod(“addHwFlags“?int.class);
????????????method.invoke(layoutParamsExObj?FLAG_NOTCH_SUPPORT);
????????}?catch?(Exception?e)?{
????????????Log.e(“test“?“other?Exception“);
????????}
評(píng)論
共有 條評(píng)論