Java Code Examples for android.util.TypedValue#COMPLEX_UNIT_IN

The following examples show how to use android.util.TypedValue#COMPLEX_UNIT_IN . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SizeUtils.java    From AcgClub with MIT License 6 votes vote down vote up
/**
 * 各种单位转换
 * <p>该方法存在于TypedValue</p>
 *
 * @param unit 单位
 * @param value 值
 * @param metrics DisplayMetrics
 * @return 转换结果
 */
public static float applyDimension(int unit, float value, DisplayMetrics metrics) {
  switch (unit) {
    case TypedValue.COMPLEX_UNIT_PX:
      return value;
    case TypedValue.COMPLEX_UNIT_DIP:
      return value * metrics.density;
    case TypedValue.COMPLEX_UNIT_SP:
      return value * metrics.scaledDensity;
    case TypedValue.COMPLEX_UNIT_PT:
      return value * metrics.xdpi * (1.0f / 72);
    case TypedValue.COMPLEX_UNIT_IN:
      return value * metrics.xdpi;
    case TypedValue.COMPLEX_UNIT_MM:
      return value * metrics.xdpi * (1.0f / 25.4f);
  }
  return 0;
}
 
Example 2
Source File: DimenUtils.java    From Common with Apache License 2.0 6 votes vote down vote up
/**
 * Converts an unpacked complex data value holding a dimension to its final floating
 * point value. The two parameters <var>unit</var> and <var>value</var>
 * are as in {@link TypedValue#TYPE_DIMENSION}.
 *
 * @param value The value to apply the unit to.
 * @param unit  The unit to convert from.
 * @return The complex floating point value multiplied by the appropriate
 * metrics depending on its unit.
 */
public static float applyDimension(@NonNull final Context context, final float value, final int unit) {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    switch (unit) {
        case TypedValue.COMPLEX_UNIT_PX:
            return value;
        case TypedValue.COMPLEX_UNIT_DIP:
            return value * metrics.density;
        case TypedValue.COMPLEX_UNIT_SP:
            return value * metrics.scaledDensity;
        case TypedValue.COMPLEX_UNIT_PT:
            return value * metrics.xdpi * (1.0f / 72);
        case TypedValue.COMPLEX_UNIT_IN:
            return value * metrics.xdpi;
        case TypedValue.COMPLEX_UNIT_MM:
            return value * metrics.xdpi * (1.0f / 25.4f);
    }
    return 0;
}
 
Example 3
Source File: SizeUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * 各种单位转换
 * <p>该方法存在于TypedValue</p>
 *
 * @param unit    单位
 * @param value   值
 * @param metrics DisplayMetrics
 * @return 转换结果
 */
public static float applyDimension(int unit, float value, DisplayMetrics metrics) {
    switch (unit) {
        case TypedValue.COMPLEX_UNIT_PX:
            return value;
        case TypedValue.COMPLEX_UNIT_DIP:
            return value * metrics.density;
        case TypedValue.COMPLEX_UNIT_SP:
            return value * metrics.scaledDensity;
        case TypedValue.COMPLEX_UNIT_PT:
            return value * metrics.xdpi * (1.0f / 72);
        case TypedValue.COMPLEX_UNIT_IN:
            return value * metrics.xdpi;
        case TypedValue.COMPLEX_UNIT_MM:
            return value * metrics.xdpi * (1.0f / 25.4f);
    }
    return 0;
}
 
Example 4
Source File: SizeUtils.java    From Ticket-Analysis with MIT License 6 votes vote down vote up
/**
 * 各种单位转换
 * <p>该方法存在于TypedValue
 */
public static float applyDimension(int unit, float value, DisplayMetrics metrics) {
    switch (unit) {
        case TypedValue.COMPLEX_UNIT_PX:
            return value;
        case TypedValue.COMPLEX_UNIT_DIP:
            return value * metrics.density;
        case TypedValue.COMPLEX_UNIT_SP:
            return value * metrics.scaledDensity;
        case TypedValue.COMPLEX_UNIT_PT:
            return value * metrics.xdpi * (1.0f / 72);
        case TypedValue.COMPLEX_UNIT_IN:
            return value * metrics.xdpi;
        case TypedValue.COMPLEX_UNIT_MM:
            return value * metrics.xdpi * (1.0f / 25.4f);
    }
    return 0;
}
 
Example 5
Source File: XDensityUtils.java    From XFrame with Apache License 2.0 6 votes vote down vote up
/**
 * 各种单位转换
 * <p>该方法存在于TypedValue</p>
 *
 * @param unit    单位
 * @param value   值
 * @param metrics DisplayMetrics
 * @return 转换结果
 */
public static float applyDimension(int unit, float value, DisplayMetrics metrics) {
    switch (unit) {
        case TypedValue.COMPLEX_UNIT_PX:
            return value;
        case TypedValue.COMPLEX_UNIT_DIP:
            return value * metrics.density;
        case TypedValue.COMPLEX_UNIT_SP:
            return value * metrics.scaledDensity;
        case TypedValue.COMPLEX_UNIT_PT:
            return value * metrics.xdpi * (1.0f / 72);
        case TypedValue.COMPLEX_UNIT_IN:
            return value * metrics.xdpi;
        case TypedValue.COMPLEX_UNIT_MM:
            return value * metrics.xdpi * (1.0f / 25.4f);
    }
    return 0;
}
 
Example 6
Source File: SizeUtils.java    From Matisse with Apache License 2.0 6 votes vote down vote up
/**
 * 各种单位转换
 * <p>该方法存在于TypedValue</p>
 * @param unit    单位
 * @param value   值
 * @param metrics DisplayMetrics
 * @return 转换结果
 */
public static float applyDimension(int unit, float value, DisplayMetrics metrics) {
    switch (unit) {
        case TypedValue.COMPLEX_UNIT_PX:
            return value;
        case TypedValue.COMPLEX_UNIT_DIP:
            return value * metrics.density;
        case TypedValue.COMPLEX_UNIT_SP:
            return value * metrics.scaledDensity;
        case TypedValue.COMPLEX_UNIT_PT:
            return value * metrics.xdpi * (1.0f / 72);
        case TypedValue.COMPLEX_UNIT_IN:
            return value * metrics.xdpi;
        case TypedValue.COMPLEX_UNIT_MM:
            return value * metrics.xdpi * (1.0f / 25.4f);
    }
    return 0;
}
 
Example 7
Source File: SizeUtils.java    From AndroidUtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * Converts an unpacked complex data value holding a dimension to its final floating
 * point value. The two parameters <var>unit</var> and <var>value</var>
 * are as in {@link TypedValue#TYPE_DIMENSION}.
 *
 * @param value The value to apply the unit to.
 * @param unit  The unit to convert from.
 * @return The complex floating point value multiplied by the appropriate
 * metrics depending on its unit.
 */
public static float applyDimension(final float value, final int unit) {
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    switch (unit) {
        case TypedValue.COMPLEX_UNIT_PX:
            return value;
        case TypedValue.COMPLEX_UNIT_DIP:
            return value * metrics.density;
        case TypedValue.COMPLEX_UNIT_SP:
            return value * metrics.scaledDensity;
        case TypedValue.COMPLEX_UNIT_PT:
            return value * metrics.xdpi * (1.0f / 72);
        case TypedValue.COMPLEX_UNIT_IN:
            return value * metrics.xdpi;
        case TypedValue.COMPLEX_UNIT_MM:
            return value * metrics.xdpi * (1.0f / 25.4f);
    }
    return 0;
}
 
Example 8
Source File: DimensionParser.java    From android-cassowary-layout with Apache License 2.0 6 votes vote down vote up
private static int getUnitFromString(String unitString) {

        int unit = TypedValue.COMPLEX_UNIT_DIP;
        if ("px".equals(unitString)) {
            unit = TypedValue.COMPLEX_UNIT_PX;
        } else if ("dp".equals(unitString)) {
            unit = TypedValue.COMPLEX_UNIT_DIP;
        } else if ("sp".equals(unitString)) {
            unit = TypedValue.COMPLEX_UNIT_SP;
        } else if ("pt".equals(unitString)) {
            unit = TypedValue.COMPLEX_UNIT_PT;
        } else if ("in".equals(unitString)) {
            unit = TypedValue.COMPLEX_UNIT_IN;
        } else if ("mm".equals(unitString)) {
            unit = TypedValue.COMPLEX_UNIT_MM;
        }
        return unit;
    }
 
Example 9
Source File: ScreenAdaperHelper.java    From screenAdaptation with Apache License 2.0 5 votes vote down vote up
/**
 * @param unit {@link TypedValue} Not recommended COMPLEX_UNIT_PX、COMPLEX_UNIT_DIP、COMPLEX_UNIT_SP
 *             recommend COMPLEX_UNIT_PT
 */
private void setDensity(Resources res, int unit) {
    if (res != null) {
        try {
            if ((res.getClass().getSimpleName().contains("MiuiResources") || res.getClass().getSimpleName().contains("XResources")) && !enableOtherResources)
                return;
            float xdpi = 0f;
            DisplayMetrics dm = getMetrics(res);
            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();
        }
    }
}
 
Example 10
Source File: DimensionResource.java    From external-resources with Apache License 2.0 5 votes vote down vote up
protected static int getTypeFromString(@NonNull String str) {
  String[] split = str.trim().split("[0-9]");

  if (split.length > 0) {
    final String typeStr = split[split.length - 1];

    switch (typeStr) {
      case TYPE_DP:
      case TYPE_DIP:
        return TypedValue.COMPLEX_UNIT_DIP;
      case TYPE_SP:
        return TypedValue.COMPLEX_UNIT_SP;
      case TYPE_PT:
        return TypedValue.COMPLEX_UNIT_PT;
      case TYPE_IN:
        return TypedValue.COMPLEX_UNIT_IN;
      case TYPE_MM:
        return TypedValue.COMPLEX_UNIT_MM;
      case TYPE_PX:
        return TypedValue.COMPLEX_UNIT_PX;
      default:
        throw new IllegalArgumentException(typeStr + " is not a valid type dimension format.");
    }
  } else {
    return TypedValue.COMPLEX_UNIT_PX;
  }
}
 
Example 11
Source File: XmlToClassAttribHandler.java    From MVPAndroidBootstrap with Apache License 2.0 5 votes vote down vote up
public int gettextSizeUnit() {

        String value=mAttributeSet.getAttributeValue(namespace, KEY_TEXT_SIZE );

        if (value==null)
            return TypedValue.COMPLEX_UNIT_SP;

        try{
            String type=value.substring(value.length()-2, value.length());

            if (type.equals("dp"))
                return TypedValue.COMPLEX_UNIT_DIP;
            else if (type.equals("sp"))
                return TypedValue.COMPLEX_UNIT_SP;
            else if (type.equals("pt"))
                return TypedValue.COMPLEX_UNIT_PT;
            else if (type.equals("mm"))
                return TypedValue.COMPLEX_UNIT_MM;
            else if (type.equals("in"))
                return TypedValue.COMPLEX_UNIT_IN;
            else if (type.equals("px"))
                return TypedValue.COMPLEX_UNIT_PX;
        }
        catch(Exception e){
            return -1;
        }

        return -1;
    }
 
Example 12
Source File: XmlToClassAttribHandler.java    From RxAndroidBootstrap with Apache License 2.0 5 votes vote down vote up
public int gettextSizeUnit() {

        String value=mAttributeSet.getAttributeValue(namespace, KEY_TEXT_SIZE );

        if (value==null)
            return TypedValue.COMPLEX_UNIT_SP;

        try{
            String type=value.substring(value.length()-2, value.length());

            if (type.equals("dp"))
                return TypedValue.COMPLEX_UNIT_DIP;
            else if (type.equals("sp"))
                return TypedValue.COMPLEX_UNIT_SP;
            else if (type.equals("pt"))
                return TypedValue.COMPLEX_UNIT_PT;
            else if (type.equals("mm"))
                return TypedValue.COMPLEX_UNIT_MM;
            else if (type.equals("in"))
                return TypedValue.COMPLEX_UNIT_IN;
            else if (type.equals("px"))
                return TypedValue.COMPLEX_UNIT_PX;
        }
        catch(Exception e){
            return -1;
        }

        return -1;
    }