android 程序中像素跟单位dp(dip)之间的转换

来源:互联网 发布:谷嫂淘宝同款排除王下载 编辑:程序博客网 时间:2024/05/16 01:47
  1. public class UnitTransformUtil {
  2.         /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */
  3.         public static int dip2px(Context context, float dpValue) {
  4.                 final float scale = context.getResources().getDisplayMetrics().density;
  5.                 return (int) (dpValue * scale + 0.5f);
  6.         }

  7.         /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */
  8.         public static int px2dip(Context context, float pxValue) {
  9.                 final float scale = context.getResources().getDisplayMetrics().density;
  10.                 return (int) (pxValue / scale + 0.5f);
  11.         }
  12. }
0 0