Android unit conversion, android unit
PT-> SP
According to the applyDimension method in TypedValue. java:
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; }
Convert COMPLEX_UNIT_PT to COMPLEX_UNIT_SP:
Value * metrics. xdpi * (1.0f/72)/metrics. scaledDensity = value * metrics. xdpi/(72 * metrics. scaledDensity ))
So 1pt = metrics. xdpi/(72 * metrics. scaledDensity) sp
(Xdpi and metrics. scaledDensity can be obtained from the system. Note that the pt of the ui annotation must be converted to the system's corresponding pt. It depends on the size of the ui annotation)
The value depends on the machine, which is roughly equal to 2.22, and some machines are 2.15.