android - Libgdx如何获取文本的总宽度? (位图字体)

标签 android fonts libgdx

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

font.draw(batch, text ,Gdx.graphics.getWidth() / 2 - /* text width */ /2,450);

最佳答案

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

GlyphLayout layout = new GlyphLayout(font, text);
float textWidth = layout.width;

不过,不要将其放入 render 方法中。最好在 createshow 中计算并存储该值一次,然后在 render 中使用该值。

关于android - Libgdx如何获取文本的总宽度? (位图字体),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32289925/

相关文章:

java - Libgdx:SpriteBatch.draw spriteVertecies 奇怪的 UV 坐标行为

java - 在 libGDX 中作为动画运行时,不同大小的纹理区域会反弹

java - 尝试从 Player 类中抽取时 Player 不会移动

android - TextView 下划线电话号码和超链接

android - 如何将数组放入复选框并根据数组 id 单击它以 react native ?

android - 尝试获取 CurrentPosition 时在 MediaPlayer 中获取 IllegalStateException

html - 如何根据 css 属性 "font-weight"计算文本粗细?

android - TextView编程中如何设置撇号

android - 当我使用 Roboto-Italic.ttf 字体时,为什么 TextView.setTypeface() 夸大地增加了我的 TextView 高度?

c++ - C++ 对话框中的多种字体