Java 如何使用BitmapFont.TextBounds在屏幕上居中显示一行文本?

Java 如何使用BitmapFont.TextBounds在屏幕上居中显示一行文本?,java,bitmap,libgdx,Java,Bitmap,Libgdx,我用来将这两行文字居中的代码不起作用。是我屏幕上的链接 这是我用来做这件事的代码,我希望两个文本在屏幕上水平居中 BitmapFont.TextBounds titleBounds1 = font.getBounds("Pizza Delivery"); BitmapFont.TextBounds titleBounds2 = font.getBounds("MADNESS"); font.draw(batch,"Pizza Delivery", (Gdx.graphi

我用来将这两行文字居中的代码不起作用。是我屏幕上的链接

这是我用来做这件事的代码,我希望两个文本在屏幕上水平居中

    BitmapFont.TextBounds titleBounds1 = font.getBounds("Pizza Delivery");
    BitmapFont.TextBounds titleBounds2 = font.getBounds("MADNESS");

    font.draw(batch,"Pizza Delivery", (Gdx.graphics.getWidth() - titleBounds1.width)  / 2,500);
    font.draw(batch,"MADNESS",(Gdx.graphics.getWidth() - titleBounds2.width)  / 2, 450);

您拥有的代码很好,因此它一定是代码中您尚未发布的其他内容

据我所知,唯一能引起你得到的效果的是,如果批量应用了刻度

如果是这样,那么有两种可能的解决方案

  • 计算中心位置时比例中的系数
  • 如果缩放仅针对文本,则不要缩放批次,而是使用font.setScale(),并在获取边界之前进行缩放

  • 您可以使用
    表格
    ,并将文本添加为
    标签
    s。