libgdx如何将BitmapFont缩放到更改屏幕大小?

我想要的是在更改屏幕尺寸时相应地改变尺寸的位图字体。 我的意思是在我的电脑上,字体看起来相当大,但在我的手机上,它是一个难以阅读的小字体。 我可以改变大小,但我希望它在所有屏幕上都看起来相似,而不是在一个屏幕上大而在另一个屏幕上小一些。 这是我的代码,看看我有什么用处:

public void render() { //score system scoreFont.setColor(1.0f, 1.0f, 1.0f, 1.0f); scoreBatch.begin(); scoreFont.draw(scoreBatch, Long.toString(getScore()), 10, Gdx.graphics.getHeight() - 10); scoreFont.setScale(3, 3); scoreBatch.end(); } public void resize(int width, int height) { camera.viewportWidth = 450; camera.viewportHeight = 250; camera.update(); stage.setViewport(450, 250, true); stage.getCamera().translate(-stage.getGutterWidth(), -stage.getGutterHeight(), 0); } 

可以这样想,你总是希望拥有相同的比例。

例如:

500/3 = 450 / x

x是文本的新大小。 所以你必须做一些交叉乘法。

500x = 1350

1350÷500 = x

所以现在以编程方式执行此操作。

 public void resizeText(float width, float currentSize, float currentWidth){ //currentWidth/currentSize = width/x a = width * currentSize;//450 * 3 in example above b = a/currentWidth; return b;//returns the x or the new size that your text should be } 

另外你说它需要根据大小的变化超过一定量。

所以这是我设计的一件小事

 ApplicationType appType = Gdx.app.getType(); if (appType == ApplicationType.Android || appType == ApplicationType.iOS) { screenFont.setScale(your number) } else { screen font.setScale(otherNum)} //if its a desktop 

Gdx.graphics.getDensity()缩放