Android dp,sp与px转换
Android 大约 854 字方法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);
}
}
阅读 2994 · 发布于 2019-04-14
————        END        ————
扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
PostgreSQL使用\copy命令时报character with byte sequence 0xc3 0xa5 in encoding "UTF8" has no equivalent in encoding "GBK"阅读 3534
-
OpenResty判断是否为空阅读 1070
-
PostgreSQL查询表中的自增序列名称阅读 246
-
Linux命令之vim显示行号阅读 782
-
Android去除SDK危险权限阅读 744
-
设计模式之外观模式阅读 369
-
Linux 两个文件取并集、交集、差集阅读 1518
-
软考-系统架构设计师:并发控制阅读 420
-
设计模式之访问者模式阅读 504
-
走进Rust:引用的生命周期阅读 401