Convert pixels to sp

前端 未结 3 1310
野趣味
野趣味 2020-11-27 15:18

I need the current TextSize of the TextView in sp units.

But getTextSize() returns the size in pixels

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 16:03

    See the DisplayMetrics class, it has fields for densityDpi and scaledDensity.

    Example usage:

    float sp = px / getResources().getDisplayMetrics().scaledDensity;
    

提交回复
热议问题