Let's look at an example:
int size = (int) typedvalue.applydimension (Typedvalue.complex_unit_dip, 3, Context.getresources (). Getdisplaymetrics ( ));
This line of code converts the value of dip 3 to the PX value in the current screen resolution state
Here is an introduction to the API:
Public static float applydimension (int unit, float value, displaymetrics metrics) SINCE:API Level 1
Converts an unpacked complex data value holding a dimension to it final floating point value. The parameters and is as in unit value TYPE_DIMENSION .
Parameters
| Unit |
The unit to convert from. |
| Value |
The value to apply the unit to. |
| Metrics |
Current display metrics to use the conversion--supplies display density and scaling information. |
Returns
- The complex floating point value multiplied is the appropriate metrics depending on its unit.
View its source code in the framework
Public Static floatApplydimension (intUnitfloatvalue, Displaymetrics metrics) { Switch(unit) { Casecomplex_unit_px:returnvalue; CaseComplex_unit_dip:returnValue *metrics.density; Casecomplex_unit_sp:returnValue *metrics.scaleddensity; Casecomplex_unit_pt:returnValue * metrics.xdpi * (1.0f/72); Casecomplex_unit_in:returnValue *metrics.xdpi; Casecomplex_unit_mm:returnValue * metrics.xdpi * (1.0f/25.4f); } return0; }
If the incoming UNIT is compliex_unit_px, the return value is consistent with the value passed in, and the inverse is converted to PX and returned according to a certain calculation.
Android.util.TypedValue.applyDimension