Android dp,sp 与 px 转换
Android 大约 855 字方法1
public float dp2px(float dp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());
}
public float sp2px(float sp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, Resources.getSystem().getDisplayMetrics());
}
方法2
public class DensityUtil {
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
* 想要在代码中设置数值单位为dp的,用这个方法
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
阅读 4764 · 发布于 2019-04-14
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
JMeter 登录后进行压测以及压测结束后退出阅读 823
-
JMeter 使用 Critical Section Controller 按顺序执行接口阅读 799
-
Windows 命令行中打开文件夹阅读 699
-
Golang 时间加减、计算方法耗、毫秒转 Time阅读 8043
-
PHP 判断函数、类、方法、属性、数组中的 key 是否存在阅读 2578
-
Android MediaMetadataRetriever 获取多媒体文件信息阅读 3006
-
IDEA 破解后打不开解决方法阅读 12868
-
Spring 事务原理阅读 349
-
Linux CentOS6 安装 Docker阅读 3178
-
MySQL 查看数据储存引擎阅读 740