简体   繁体   English

libgdx如何缩放BitmapFont以更改屏幕尺寸?

[英]libgdx How do I scale a BitmapFont to changing screen sizes?

What I want to have is the Bitmap Font to change in size accordingly when changing screen sizes. 我想要的是在更改屏幕大小时相应更改大小的位图字体。 What I mean is on my computer, the font appears rather large, but on my phone, it is a little font that is harder to read. 我的意思是在计算机上,字体看起来很大,但是在手机上,它是一种较难阅读的小字体。 I could change the size, but I want it to look similar on all screens, instead of having it large on one screen and smaller on another. 我可以更改大小,但是我希望它在所有屏幕上看起来都相似,而不是在一个屏幕上变大而在另一个屏幕上变小。 Here is my code to see what I have to work with: 这是我的代码,以查看必须使用的代码:

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);
}

Think of it this way, you always want to have the same ratio. 这样想,您总是想拥有相同的比率。

For example: 例如:

500/3 = 450/x 500/3 = 450 / x

x is the new size of your text. x是您的文本的新大小。 So you have to do some cross multiplying. 因此,您必须进行一些交叉乘法。

500x = 1350 500x = 1350

1350÷500 = x 1350÷500 = x

So now to do this programmatically. 因此,现在以编程方式执行此操作。

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
}

Also you said that it needs to change depending on of the size is over a certain amount. 您还说过,需要根据尺寸是否超过一定量来进行更改。

So here's a little thing I've devised 所以这是我设计的一件事

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()缩放

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM