欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

android中px和dp,px和sp之间的转换方法

时间:2021-09-15 10:43:37|栏目:Android代码|点击:

在Android开发中dp和px,sp和px之间的转换时必不可少的,网上流传的方法

public class DisplayUtils {
  /**
   * convert px to its equivalent dp
   * 将px转换为与之相等的dp
   */
  public static int px2dp(Context context, float pxValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5f);
  }
  /**
   * convert dp to its equivalent px
   * 将dp转换为与之相等的px
   */
  public static int dp2px(Context context, float dipValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dipValue * scale + 0.5f);
  }
  /**
   * convert px to its equivalent sp 
   * 将px转换为sp
   */
  public static int px2sp(Context context, float pxValue) {
    final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
    return (int) (pxValue / fontScale + 0.5f);
  }
  /**
   * convert sp to its equivalent px
   * 将sp转换为px
   */
  public static int sp2px(Context context, float spValue) {
    final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
    return (int) (spValue * fontScale + 0.5f);
  }
}

关于转换公式中,通过类比一元一次函数,怎么看都是有问题的,这么明显的问题,为什么没人纠正,后来发现是自己并没有理解,原因是float类型在强转为int类型是,用的是去尾法,精度上有较大差异,所以通过+0.5f的方式,将去尾法转变成四舍五入法,提高精度。

调用TypedValue类实现转换

/**
* convert dp to its equivalent px
*/
protected int dp2px(int dp){
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,getResources().getDisplayMetrics());
}
/**
* convert sp to its equivalent px
*/
protected int sp2px(int sp){
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,getResources().getDisplayMetrics());
}
public class TypedValue {
  ...
  /** {@link #TYPE_DIMENSION} complex unit: Value is raw pixels. */
  public static final int COMPLEX_UNIT_PX = 0;
  /** {@link #TYPE_DIMENSION} complex unit: Value is Device Independent
   * Pixels. */
  public static final int COMPLEX_UNIT_DIP = 1;
  /** {@link #TYPE_DIMENSION} complex unit: Value is a scaled pixel. */
  public static final int COMPLEX_UNIT_SP = 2;
  /** {@link #TYPE_DIMENSION} complex unit: Value is in points. */
  public static final int COMPLEX_UNIT_PT = 3;
  /** {@link #TYPE_DIMENSION} complex unit: Value is in inches. */
  public static final int COMPLEX_UNIT_IN = 4;
  /** {@link #TYPE_DIMENSION} complex unit: Value is in millimeters. */
  public static final int COMPLEX_UNIT_MM = 5;
  /**
   * 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 #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;
  }
  ...
}

对比两种方式的差异,其实就在+0.5f上,去尾法和四舍五入法的区别,即精度问题。

总结

上一篇:详解Flutter混排瀑布流解决方案

栏    目:Android代码

下一篇:Android如何使用Bmob后端云实现失物招领功能

本文标题:android中px和dp,px和sp之间的转换方法

本文地址:http://www.codeinn.net/misctech/175130.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有