本文共 1111 字,大约阅读时间需要 3 分钟。
/**
* @param unit {@link TypedValue} Not recommended COMPLEX_UNIT_PX、COMPLEX_UNIT_DIP、COMPLEX_UNIT_SP
* recommend COMPLEX_UNIT_PT
*/
private void setDensity(Context context, int unit) {
if (context != null) {
try {
Resources res = context.getResources();
if ((res.getClass().getSimpleName().contains("MiuiResources") || res.getClass().getSimpleName().contains("XResources")) && !enableOtherResources)
return;
float xdpi = 0f;
DisplayMetrics dm = getMetrics(context.getResources());
if (dm == null) return;
switch (unit) {
case TypedValue.COMPLEX_UNIT_PT:
xdpi = dm.widthPixels / DESIGN_WIDTH * 72;
break;
case TypedValue.COMPLEX_UNIT_IN:
xdpi = dm.widthPixels / DESIGN_WIDTH;
break;
case TypedValue.COMPLEX_UNIT_MM:
xdpi = dm.widthPixels / DESIGN_WIDTH * 25.4f;
break;
case TypedValue.COMPLEX_UNIT_DIP:
xdpi = dm.widthPixels / DESIGN_WIDTH;
break;
}
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putFloat("xdpi", xdpi);
editor.apply();
if (unit == TypedValue.COMPLEX_UNIT_DIP) {
if (enableDp)
dm.density = xdpi;
} else {
dm.xdpi = xdpi;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
转载地址:https://blog.csdn.net/weixin_34237700/article/details/114898924 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!