简体   繁体   中英

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

x is the new size of your text. So you have to do some cross multiplying.

500x = 1350

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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