上一段时间有同事提了一个bug,说他手机用我们APP的时候图标全部都变型了,被强制拉长,现在分享一下这台手机OPPO R11s,分辨率高达2160*1080,这奇葩分辨率,一般的分辨率只是1920*1080,所以怪不得图标会拉长,现在教大家一个完美处理这个问题的方法。废话不多说,直接上代码
public void init(Context context) { getMetaData(context); int[] screenSize = ScreenUtils.getScreenSize(context, useDeviceSize); mScreenWidth = screenSize[0]; mScreenHeight = screenSize[1]; L.e(" screenWidth =" + mScreenWidth + " ,screenHeight = " + mScreenHeight); } private void getMetaData(Context context) { PackageManager packageManager = context.getPackageManager(); ApplicationInfo applicationInfo; try { applicationInfo = packageManager.getApplicationInfo(context .getPackageName(), PackageManager.GET_META_DATA); if (applicationInfo != null && applicationInfo.metaData != null) { mDesignWidth = applicationInfo.metaData.getInt(KEY_DESIGN_WIDTH); mDesignHeight = applicationInfo.metaData.getInt(KEY_DESIGN_HEIGHT); } } catch (PackageManager.NameNotFoundException e) { throw new RuntimeException( "you must set " + KEY_DESIGN_WIDTH + " and " + KEY_DESIGN_HEIGHT + " in your manifest file.", e); } L.e(" designWidth =" + mDesignWidth + " , designHeight = " + mDesignHeight); }
这个是原来AutoLayout初始化的代码,可以看出他是根据我们设置的宽高来按比例显示,所以当出现分辨率不是16:9的时候会很恶心,所以这时候我们要做一个根据手机分辨率来调整参数的举动
private void getMetaData(Context context) { PackageManager packageManager = context.getPackageManager(); ApplicationInfo applicationInfo; try { applicationInfo = packageManager.getApplicationInfo(context .getPackageName(), PackageManager.GET_META_DATA); if (applicationInfo != null && applicationInfo.metaData != null) { mDesignWidth = applicationInfo.metaData.getInt(KEY_DESIGN_WIDTH); mDesignHeight = applicationInfo.metaData.getInt(KEY_DESIGN_HEIGHT); /*if(PhoneUtil.getPhoneModel().equals("OPPO R11s")) mDesignHeight = 1500;*/ /** * 针对有虚拟按键的手机重新计算数值 */ if(PhoneUtil.isHaveNavigationBar(context)){ mDesignHeight = (PhoneUtil.getScreenHeight(context)+PhoneUtil.getNavigationBarHeight(context))*750/PhoneUtil.getScreenWidth(context); } } } catch (PackageManager.NameNotFoundException e) { throw new RuntimeException( "you must set " + KEY_DESIGN_WIDTH + " and " + KEY_DESIGN_HEIGHT + " in your manifest file.", e); } L.e(" designWidth =" + mDesignWidth + " , designHeight = " + mDesignHeight); }
首先要判断有没有虚拟按键,有的话要计算出来因为获取屏幕高度是不包括按钮的,而产商给出的屏幕的高度是虚拟按钮的高度再加系统获取的ScreenHeight。之后看代码就知道我重新计算了mDeisignHeight,750是我配置的屏幕宽度,这样就能完美解决奇葩分辨率的问题啦。