美文网首页
dp2px与px2dp

dp2px与px2dp

作者: junling2017 | 来源:发表于2017-10-23 14:21 被阅读0次
public class DensityUtil {

float density;

public DensityUtil() {
    density = Resources.getSystem().getDisplayMetrics().density;
}

/**
 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
 */
public static int dp2px(float dpValue) {
    return (int) (0.5f + dpValue * Resources.getSystem().getDisplayMetrics().density);
}

/**
 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
 */
public static float px2dp(float pxValue) {
    return (pxValue / Resources.getSystem().getDisplayMetrics().density);
}

/**
 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
 */
public int dip2px(float dpValue) {
    return (int) (0.5f + dpValue * density);
}

/**
 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
 */
public float px2dip(float pxValue) {
    return (pxValue / density);
}
}

相关文章

网友评论

      本文标题:dp2px与px2dp

      本文链接:https://www.haomeiwen.com/subject/ajhauxtx.html