Android: Canvas.drawText() text size on different screen resolutions

后端 未结 4 2033
孤独总比滥情好
孤独总比滥情好 2020-12-13 09:27

For my Android game I have some calls to Canvas.drawText().

For testing, I use the standard font size which seems to be working fine.

However, w

4条回答
  •  自闭症患者
    2020-12-13 10:29

    The easiest way is to define your font sizes in your resources with the units of scale-independent pixels (sp) -- this unit is like density independent pixels (dp or dip) in that it takes into account the screen density but it also takes into account the font-size setting of the user.

    To add a new dimension create a dimens.xml file in your res/values folder and enter the following code to add a new dimension with the name myFontSize:

    
    
        20sp
    
    

    You can then get the text size in your application using

    int scaledSize = getResources().getDimensionPixelSize(R.dimen.myFontSize);
    

    The resulting size will be correctly scaled to take into account the current screen density and font-size setting.

    For more information see the Android Developers page on More Resources.

提交回复
热议问题