COMPLEX_UNIT_MM源码实例Demo

android.util.TypedValue#COMPLEX_UNIT_MM源码实例Demo

下面列出了android.util.TypedValue#COMPLEX_UNIT_MM 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: AcgClub   文件: SizeUtils.java
/**
 * 各种单位转换
 * <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;
}
 
源代码2 项目: Common   文件: DimenUtils.java
/**
 * 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;
}
 
源代码3 项目: Android-UtilCode   文件: SizeUtils.java
/**
 * 各种单位转换
 * <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;
}
 
源代码4 项目: Ticket-Analysis   文件: SizeUtils.java
/**
 * 各种单位转换
 * <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;
}
 
源代码5 项目: XFrame   文件: XDensityUtils.java
/**
 * 各种单位转换
 * <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;
}
 
源代码6 项目: Matisse   文件: SizeUtils.java
/**
 * 各种单位转换
 * <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;
}
 
源代码7 项目: AndroidUtilCode   文件: SizeUtils.java
/**
 * 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;
}
 
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;
    }
 
源代码9 项目: screenAdaptation   文件: ScreenAdaperHelper.java
/**
 * @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();
        }
    }
}
 
源代码10 项目: external-resources   文件: DimensionResource.java
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;
  }
}
 
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;
    }
 
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;
    }