Libgdx如何获取文本的总宽度?(BitmapFont)

Dyl*_*uwe 2 fonts android libgdx

如何获取BitmapFont文本的总宽度?

font.draw(batch, text ,Gdx.graphics.getWidth() / 2 - /* text width */ /2,450);
Run Code Online (Sandbox Code Playgroud)

dan*_*rod 5

使用a GlyphLayout来使用特定字体计算某些字符串的宽度:

GlyphLayout layout = new GlyphLayout(font, text);
float textWidth = layout.width;
Run Code Online (Sandbox Code Playgroud)

不过,不要把它放在render方法中.它是计算和存储一次该值是一个好主意createshow,然后只用值在你的render.