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);
}
}
阅读 5845 · 发布于 2019-04-14
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
Linux 之校准服务器时间阅读 2902
-
Java11 -Xms -Xmx 设置堆内存大小时的注意事项阅读 2240
-
Spring Boot logback 导致 /tmp 磁盘爆满阅读 4211
-
Ubuntu 系统升级 MySQL 版本阅读 2368
-
Spring @EventListener 加载流程阅读 1807
-
Kubernetes 查看容器进程使用内存情况阅读 890
-
Redis删除数据后内存不释放解决方法阅读 3852
-
Keycloak OAuth2 授权码模式开启 PKCE 授权方式阅读 59
-
使用 awk 提取 JSON 字符串中的字段阅读 9124
-
Alpine Linux 查看发行版本阅读 347