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

后端 未结 4 2019
孤独总比滥情好
孤独总比滥情好 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:23

    You can do it yourself using a simple math operation:

    You need to calculate a relation that draws your text just the same size for any canvas size, so use the actual canvas size, like this:

    double relation = Math.sqrt(canvas.getWidth() * canvas.getHeight());
    

    But that number is just too big, so divide it by one that suits your needs, lets say 250:

    relation = relation / 250;
    

    Now you can set your text size like this:

    paint.setTextSize((float) (myFontSize * relation));
    

    You don't have to necessary divide relation by any number but in that case you will have to use very small font sizes for the variable myFontSize. For me 250 works just fine to use regular font sizes that will adjust to any screen size, since you are already taking in count the pixels dimension from your canvas.

提交回复
热议问题