具体通过使用
/** * Converts an unpacked complex data value holding a dimension to its final floating * point value.
The two parameters unit and value * are as in {
@link #TYPE_DIMENSION}. * *
@param unit The unit to convert from. *
@param value The value to apply the unit to. *
@param metrics Current display metrics to use in the conversion -- * supplies display density and scaling information. * *
@return The complex floating point value multiplied by the appropriate * metrics depending on its unit. */
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; }
方法,可以在代码中设置x dip的效果。