Android 获取dip数据的三种途径
发布日期:2021-06-30 22:37:07 浏览次数:2 分类:技术文章

本文共 503 字,大约阅读时间需要 1 分钟。

1.第一种就是写个工具类UITools,获取手机的密度,然后根据公式转换

代码:

public static int px2dip(Context context, float px) {        float density = context.getResources().getDisplayMetrics().density;        return (int) (px * density + 0.5f);}

2.第二种使用TypedValue里的applyDimension转换

代码:

int width= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,                  width, getResources().getDisplayMetrics());

3.第三种就是写配置文件dimens.xml

定义

16dp

代码:

int width = context.getResources().getDimension(R.dimen.width)

转载地址:https://lovoo.blog.csdn.net/article/details/51462563 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:OC 结构体的应用
下一篇:Android 模仿flabby bird游戏开发

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月24日 10时05分13秒