为移动设备缩放 libgdx UI?

bob*_*e01 3 java libgdx

目前desktop应用程序版本很好,按钮缩放得很好,但是当我部署到android它们时,它们很小并且几乎无法使用。

DesktopLauncher ..

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        config.title = "Color Catchin";
        config.width = 800;
        config.height = 480;
        new LwjglApplication(new ColorCatch(), config);
    }
}
Run Code Online (Sandbox Code Playgroud)

AndroidLauncher ..

public class AndroidLauncher extends AndroidApplication {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        config.useAccelerometer = false;
        config.useCompass = false;
        initialize(new ColorCatch(), config);
    }
}
Run Code Online (Sandbox Code Playgroud)

Core 代码 ..

public class MainMenu implements Screen {

    Skin skin = new Skin(Gdx.files.internal("ui/uiskin.json"));
    Stage stage = new Stage();

    final private ColorCatch game;

    public MainMenu(final ColorCatch gam) {

        game = gam;

        Gdx.input.setInputProcessor(stage);

        Table table = new Table();
        table.setFillParent(true);
        stage.addActor(table);

        final TextButton play = new TextButton("Play", skin);
        final TextButton quit = new TextButton("Quit", skin);
        table.add(play).pad(10);
        table.row();
        table.add(quit).pad(10);

        play.addListener(new ChangeListener() {
            public void changed(ChangeEvent event, Actor actor) {
                game.setScreen(new GameScreen(game));
                dispose();
            }
        });
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act(delta);
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
        stage.getViewport().update(width, height, true);
    }
}
Run Code Online (Sandbox Code Playgroud)

桌面 ..

桌面

安卓 ..

安卓

noo*_*one 6

默认情况下StageScalingViewport设置为xScaling.stretch的虚拟视口大小(请参阅此处)。Gdx.graphics.getWidth()Gdx.graphics.getHeight

在桌面上,您将从 800x480 的大小开始,因为这是您告诉启动器的大小。在 Android 上,这是动态的,取决于设备。在您的设备上,它可能是 1920x1080。

由于您不更改按钮大小,因此它们在两个设备上的像素大小相同。由于屏幕密度完全不同,Android 上的按钮看起来要小得多。

使两者达到同一级别的最简单解决方案是使用Viewport具有固定虚拟大小的 。例如一个new FitViewport(800, 480). 您可以通过 将该视口提供给舞台new Stage(viewport)

但是,根据屏幕尺寸放大或缩小以保持纵横比和虚拟分辨率对于 UI 通常不是一个好主意。最好使用 aScreenViewport代替并设置您的演员相对于彼此的尺寸。Value.percentWidth(0.5f, rootTable)例如,您可以使用将小部件的宽度设置为根表的 50%,这将占据整个屏幕(通过 setFillParent(true))。