TypedValue.applyDimension是一个将各种单位的值转换为像素的方法
用法TypedValue.applyDimension(int unit, float value,DisplayMetrics metrics)
源码分析:
public static float applyDimension(int unit, float value, DisplayMetrics metrics) { switch (unit) { case COMPLEX_UNIT_PX: return value; case COMPLEX_UNIT_DIP: return value * metrics.density; case COMPLEX_UNIT_SP: return value * metrics.scaledDensity; case COMPLEX_UNIT_PT: return value * metrics.xdpi * (1.0f/72); case COMPLEX_UNIT_IN: return value * metrics.xdpi; case COMPLEX_UNIT_MM: return value * metrics.xdpi * (1.0f/25.4f); } return 0; }
使用:
//其他sp,dp转px public int value2px(int typeValue,int dpVal) { return (int) TypedValue.applyDimension(typeValue, dpVal, getResources().getDisplayMetrics()); }
时间: 2024-10-08 22:00:24