[代码片段]Android像素转换工具类

Android2023年3月14日 pm6:35发布1年前 (2023)更新 3XCN.COM站长
1 0 0
广告也精彩
目录

前言

移动项目需要根据设备进行适配,这个还是很有用的,摘抄于此,方便自己查阅。

正文

public class DensityUtil {
    public static int dip2px(float dpValue) {
        return (int) (dpValue * Resources.getSystem().getDisplayMetrics().density + 0.5f * (float) (dpValue >= 0.0f ? 1 : -1));
    }

    public static int dimenDip2px(int dimenId) {
        return dip2px(Resources.getSystem().getDimension(dimenId) / Resources.getSystem().getDisplayMetrics().density);
    }

    public static int px2dip(float pxValue) {
        return (int) (pxValue / Resources.getSystem().getDisplayMetrics().density + 0.5f * (float) (pxValue >= 0.0f ? 1 : -1));
    }

    public static int px2sp(float pxValue) {
        return (int) (pxValue / Resources.getSystem().getDisplayMetrics().scaledDensity + 0.5f * (float) (pxValue >= 0.0f ? 1 : -1));
    }

    public static int sp2px(float spValue) {
        return (int) (spValue * Resources.getSystem().getDisplayMetrics().scaledDensity + 0.5f * (float) (spValue >= 0.0f ? 1 : -1));
    }
}

参考文章

  1. Android像素转换工具类
  2. 安卓工具类1——像素PX和dp互相转换的方法(获取手机屏幕尺寸PX)

 历史上的今天

版权声明 1、 本站名称: 91易搜
2、 本站网址: 91es.com3xcn.com
3、 本站文章: 部分来源于网络,仅供站长学习和参考,若侵权请留言
广告也精彩

相关文章

广告也精彩

暂无评论

评论审核已启用。您的评论可能需要一段时间后才能被显示。

暂无评论...

网站升级中

公告

近期网站升级中,可能存在一些bug。欢迎反馈 https://www.91es.com/we.html

本站域名

本站域名 : 91es.com3xcn.com。本站邮箱 : 站长邮箱 i@oorr.cn,通知邮箱we@oorr.cn ,如有更新,请看公告 。