将像素转换为 sp [英] Convert pixels to sp
本文介绍了将像素转换为 sp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要 sp
单位中的 TextView
的当前 TextSize
.
I need the current TextSize
of the TextView
in sp
units.
但 getTextSize()
以 pixels
为单位返回大小.那么有没有办法将像素转换为sp
?
But getTextSize()
returns the size in pixels
. So is there a way to convert pixels to sp
?
推荐答案
参见DisplayMetrics 类,它有 densityDpi
和 scaledDensity
的字段.
See the DisplayMetrics class, it has fields for densityDpi
and scaledDensity
.
示例用法:
float sp = px / getResources().getDisplayMetrics().scaledDensity;
这篇关于将像素转换为 sp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文