Libgdx,文本大小与桌面不同 android

Libgdx, text size differs from android to desktop

我是 libgdx 的新手,我正在尝试做一些事情,但我遇到了一个奇怪的问题。文本在桌面上非常小,而在 android 上是正常的。这是屏幕截图:

桌面:

Android:

这是代码:

    public MainMenu(RTSGame game){
    this.game = game;

    stage = new Stage( new FitViewport( 1920, 1080 ));

    Table rootTable = new Table();
    rootTable.setFillParent(true);
    rootTable.setDebug(true, true);

    Table menuTable = new Table();
    rootTable.add(menuTable).expand().center().right();

    Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));

    TextButton singlePlayer = new TextButton("Single Player",skin);
    TextButton multiPlayer = new TextButton("Multi Player", skin);
    TextButton options = new TextButton("Options", skin);
    TextButton exit = new TextButton("Exit", skin);

    int x = 1080;
    int unit = x / 8;

    float scale = unit / singlePlayer.getHeight();

    float width = singlePlayer.getWidth() * scale;

    Label.LabelStyle style = new Label.LabelStyle();

    style.font = game.fontMenu;

    singlePlayer.getLabel().setStyle(style);
    multiPlayer.getLabel().setStyle(style);
    options.getLabel().setStyle(style);
    exit.getLabel().setStyle(style);

    float fontScale = 1f;

    if( singlePlayer.getLabel().getHeight() < unit )
        fontScale = (singlePlayer.getLabel().getHeight() / unit) * ( 1080 / stage.getHeight() );

    Gdx.app.log( "label", singlePlayer.getLabel().getHeight() + "" );
    Gdx.app.log( "unit", unit + "" );
    Gdx.app.log( "scale", fontScale + "");

    singlePlayer.getLabel().setFontScale(fontScale);
    multiPlayer.getLabel().setFontScale(fontScale);
    options.getLabel().setFontScale(fontScale);
    exit.getLabel().setFontScale(fontScale);

    menuTable.add( singlePlayer ).size( width,unit).right().row();
    menuTable.add( multiPlayer ).size( width,unit).right().row();
    menuTable.add( options ).size( width,unit).right().row();
    menuTable.add( exit ).size( width,unit).right().row();

    stage.addActor(rootTable);
}

首先我认为我应该使用#getPrefHeight() 而不是#getHeight()。使用高度进行缩放也是一种不好的做法。最好使用宽度比率。其次,我用以下代码纠正了错误的宽高比:

    if( width > singlePlayer.getLabel().getPrefWidth() && fontScale < 1f )
        fontScale = 1 / fontScale;

    if( width < singlePlayer.getLabel().getPrefWidth() && fontScale > 1f )
        fontScale = 1 / fontScale;

这是完整的代码片段:

    public MainMenu(RTSGame game){
    this.game = game;

    stage = new Stage( new FitViewport( 1920, 1080 ));

    Table rootTable = new Table();
    rootTable.setFillParent(true);
    rootTable.setDebug(true, true);

    Table menuTable = new Table();
    rootTable.add(menuTable).expand().center().right();

    Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));

    TextButton singlePlayer = new TextButton("Single Player",skin);
    TextButton multiPlayer = new TextButton("Multi Player", skin);
    TextButton options = new TextButton("Options", skin);
    TextButton exit = new TextButton("Exit", skin);

    int y = 1080;
    int unit = y / 8;

    float scale = unit / singlePlayer.getHeight();

    float width = singlePlayer.getWidth() * scale;

    Label.LabelStyle style = new Label.LabelStyle();

    style.font = game.fontMenu;

    singlePlayer.getLabel().setStyle(style);
    multiPlayer.getLabel().setStyle(style);
    options.getLabel().setStyle(style);
    exit.getLabel().setStyle(style);

    float fontScale = ( singlePlayer.getLabel().getPrefWidth() + 20 ) / width;

    Gdx.app.log( "label-height", singlePlayer.getLabel().getHeight() + "" );
    Gdx.app.log( "label-prefheight", singlePlayer.getLabel().getPrefHeight() + "" );
    Gdx.app.log( "unit", unit + "" );
    Gdx.app.log( "scale", fontScale + "");

    if( width > singlePlayer.getLabel().getPrefWidth() && fontScale < 1f )
        fontScale = 1 / fontScale;

    if( width < singlePlayer.getLabel().getPrefWidth() && fontScale > 1f )
        fontScale = 1 / fontScale;

    singlePlayer.getLabel().setFontScale(fontScale);
    multiPlayer.getLabel().setFontScale(fontScale);
    options.getLabel().setFontScale(fontScale);
    exit.getLabel().setFontScale(fontScale);

    Gdx.app.log( "label-scaled-height", singlePlayer.getLabel().getHeight() + "" );
    Gdx.app.log( "label-scaled-prefheight", singlePlayer.getLabel().getPrefHeight() + "" );

    menuTable.add( singlePlayer ).size( width,unit).right().row();
    menuTable.add( multiPlayer ).size( width,unit).right().row();
    menuTable.add( options ).size( width,unit).right().row();
    menuTable.add( exit ).size( width,unit).right().row();

    exit.addListener( new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y){
            Gdx.app.exit();
        }
    });

    Gdx.input.setInputProcessor( stage );

    stage.addActor(rootTable);
}