java 的typed_Java TypedValue.COMPLEX_UNIT_MM属性代码示例

/**

* @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();

}

}

}