Android 8.0刘海屏适配方案 小米和华为根据官方api即可获取隐藏显示转态,oppo和Vivo没有公开API

/**
 * @return  判断Android O的刘海屏
 */
private boolean hasNotchInScreen() {
    boolean ret_all = false;
    ret_huawei = false;
    ret_oppo = false;
    ret_vivo = false;
    ret_xiaomi = false;
    // ---------------------------------------------------------------
    // --------------------------              -----------------------
    //                              华为
    // --------------------------              -----------------------
    // ---------------------------------------------------------------
    try {
        ClassLoader cl = content.getClassLoader();
        Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
        Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
        ret_huawei = (boolean) get.invoke(HwNotchSizeUtil);


    } catch (Exception e) {
    }
    // ---------------------------------------------------------------
    // --------------------------              -----------------------
    //                                 oppo
    // --------------------------              -----------------------
    // ---------------------------------------------------------------
    try {
        ret_oppo = getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
    } catch (Exception e) {
    }
    // ---------------------------------------------------------------
    // --------------------------              -----------------------
    //                                vivo
    // --------------------------              -----------------------
    // ---------------------------------------------------------------
    try {
        int NOTCH_IN_SCREEN_VOIO = 0x00000020;//是否有凹槽
        ClassLoader cl = content.getClassLoader();
        Class FtFeature = cl.loadClass("android.util.FtFeature");
        Method get = FtFeature.getMethod("isFeatureSupport", int.class);
        ret_vivo = (boolean) get.invoke(FtFeature, NOTCH_IN_SCREEN_VOIO);
    } catch (Exception e) {
    }
    // ---------------------------------------------------------------
    // --------------------------              -----------------------
    //                              小米
    // --------------------------              -----------------------
    // ---------------------------------------------------------------
    try {
        String key = "ro.miui.notch";
        ClassLoader cl = content.getClassLoader();
        Class SystemProperties = cl.loadClass("android.os.SystemProperties");
        //参数类型
        Class[] paramTypes = new Class[2];
        paramTypes[0] = String.class;
        paramTypes[1] = int.class;
        Method getInt = SystemProperties.getMethod("getInt", paramTypes);
        //参数
        Object[] params = new Object[2];
        params[0] = new String(key);
        params[1] = new Integer(0);
        ret_xiaomi = (Integer) getInt.invoke(SystemProperties, params) == 1;
    } catch (Exception e) {
    }

    System.out.println("--ret_huawei--:" + ret_huawei);
    System.out.println("--ret_oppo--:" + ret_oppo);
    System.out.println("--ret_vivo--:" + ret_vivo);
    System.out.println("--ret_xiaomi--:" + ret_xiaomi);
    Log.i("zhangyb", "hasNotchInScreen: " + ret_oppo);

    ret_all = ret_huawei | ret_oppo | ret_vivo | ret_xiaomi;
    return ret_all;
}
/**
 * 隐藏虚拟导航栏
 */
private void dealSystemUI() {
    View view = getWindow().getDecorView();
    view.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);
}

 

/**
 * 在onResum中调用
 *获取参数
 * @param view 充满屏幕的view
 */
private void getViewHigh(final View view) {
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            if (ret_oppo || ret_vivo) {

                changeNotchHight(getNotchStatus(view));

            }
        }
    });
}

 

/**
 * vivo和oppo的控制
 *
 * @param view
 */
private int getNotchStatus(View view) {
    int notchHight = 0;
    int viewH = view.getHeight();
    Point point = new Point();
    getWindowManager().getDefaultDisplay().getRealSize(point);
    int maxH = point.y;
    if (viewH == maxH) {


    } else {

        notchHight = maxH - viewH;


    }
    return notchHight;


}

 

/**
 * @param hight oppo和vivo处理适配逻辑
 */
public void changeNotchHight(int hight) {

}

华为

注册刘海显示


   String DISPLAY_NOTCH_STATUS = "display_notch_status";
                    int mIsNotchSwitchOpen = Settings.Secure.getInt(getContentResolver(),DISPLAY_NOTCH_STATUS, 0);
                    Toast.makeText(content, "华为手机状态值=" + (mIsNotchSwitchOpen==0?"显示区域":"覆盖耳朵区域"), Toast.LENGTH_LONG).show();


小米

注册刘海屏


获取是否以开启刘海设置
 Settings.Global.getInt(content.getContentResolver(), "force_black", 0) == 1//覆盖区域耳朵"

获取状态兰高度 高度比刘海大一点
  public int getstatus(Context content){
        int result=0;
        int resourceId = content.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = content.getResources().getDimensionPixelSize(resourceId);
        }

        return result;
        }

你可能感兴趣的:(适配)