What does DisplayMetrics.scaledDensity actually return in Android?

后端 未结 2 786
野的像风
野的像风 2021-02-06 12:32

I\'m trying to understand the sp unit in Android, but I can\'t really figure out how it works. The documentation say:

sp

Scale-independent Pi

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-06 12:59

    Scaled density is a unit of measurement not a value of font-size. Let me try to give you an example, feel free to follow up with a comment if you would like more explanation.

    Say you have a font size 18sp. This needs to get converted to pixels in order to display on the screen. The following calculation is made: pixels = fontSize * scaledDensity * scale. Now, when you change your phone's settings, you are changing the scale. This results in the same calculation being made, but let's say with a scale of 1.5 now instead of 1. The unit of scaledDensity is not changed because your screen's pixel density has not changed.

    I hope that helps!

提交回复
热议问题