java - Scale libgdx UI for mobile? -


at moment desktop version of application fine, buttons scaled quite nice when deploy android they're tiny , barely usable.

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

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

core code ..

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

desktop ..

desktop

android ..

android

by default stage have scalingviewport set scaling.stretch virtual viewport size of gdx.graphics.getwidth() x gdx.graphics.getheight (see here).

on desktop start size of 800x480, because that's told launcher. on android, dynamic , depends on device. on device might 1920x1080.

since not change button sizes, have same size on both devices in terms of pixels. because of totally different screen density though, on android buttons appear smaller.

the easiest solution both same level use viewport fixed virtual size. example new fitviewport(800, 480). can supply viewport stage via new stage(viewport).

however, scaling or down depending on screens size keep aspect ratio , virtual resolution not idea uis. might better use screenviewport instead , set actors' sizes relative each other. can use value.percentwidth(0.5f, roottable) example, set width of widget 50% of root table, takes whole screen (via setfillparent(true)).


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -