国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android中px和dp,px和sp之間的轉換方法

2019-10-21 21:49:57
字體:
來源:轉載
供稿:網友

在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上,去尾法和四舍五入法的區別,即精度問題。

總結

以上所述是小編給大家介紹的android中px和dp,px和sp之間的轉換方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳江市| 阜城县| 禄丰县| 界首市| 宜君县| 甘泉县| 赞皇县| 深水埗区| 百色市| 洛隆县| 吕梁市| 蓝田县| 和田市| 阜阳市| 牡丹江市| 武陟县| 攀枝花市| 郑州市| 贡嘎县| 米泉市| 巴马| 布拖县| 齐河县| 鄂托克旗| 惠东县| 涞水县| 巴中市| 宿迁市| 浑源县| 南昌市| 黄梅县| 平塘县| 九龙坡区| 崇文区| 五大连池市| 固原市| 云梦县| 大关县| 永登县| 麻栗坡县| 林口县|