I am new to the libgdx framework trying to make a game but the problem here is I don't know how to scale the ImageButton
for different screen sizes it looks big on desktop but looking small on the android devices. I am using several ImageButtons
of different sizes and using table to organize them. Can anyone please make some code changes so I could understand how to scale the images here with the viewports?
public class BabyPhone extends Game {
public static final int WIDTH = 480;
public static final int HEIGHT = 800;
public void create () {
bg = new BabyActor();
bg.setTexture(new Texture("background-orange.png"));
bg.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
phone = new BabyActor();
phone.setTexture(new Texture("blue-phone.png"));
phone.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
TextureRegion btLeft = new TextureRegion(new Texture("10OFF.png"));
Drawable drawableLeft = new TextureRegionDrawable(new TextureRegion(btLeft));
buttonLeft = new ImageButton(drawableLeft);
TextureRegion btRight = new TextureRegion(new Texture("12OFF.png"));
Drawable drawableRight = new TextureRegionDrawable(new TextureRegion(btRight));
buttonRight = new ImageButton(drawableRight);
TextureRegion btCenter = new TextureRegion(new Texture("11OFF.png"));
Drawable drawableCenter = new TextureRegionDrawable(new TextureRegion(btCenter));
buttonCenter = new ImageButton(drawableCenter);
TextureRegion bt1 = new TextureRegion(new Texture("1OFF.png"));
Drawable drawable = new TextureRegionDrawable(new TextureRegion(bt1));
button1 = new ImageButton(drawable);
TextureRegion bt2 = new TextureRegion(new Texture("2OFF.png"));
Drawable drawable1 = new TextureRegionDrawable(new TextureRegion(bt2));
button2 = new ImageButton(drawable1);
TextureRegion bt3 = new TextureRegion(new Texture("3OFF.png"));
Drawable drawable2 = new TextureRegionDrawable(new TextureRegion(bt3));
button3 = new ImageButton(drawable2);
TextureRegion bt4 = new TextureRegion(new Texture("4OFF.png"));
Drawable drawable3 = new TextureRegionDrawable(new TextureRegion(bt4));
button4 = new ImageButton(drawable3);
TextureRegion bt5 = new TextureRegion(new Texture("5OFF.png"));
Drawable drawable4 = new TextureRegionDrawable(new TextureRegion(bt5));
button5 = new ImageButton(drawable4);
TextureRegion bt6 = new TextureRegion(new Texture("6OFF.png"));
Drawable drawable5 = new TextureRegionDrawable(new TextureRegion(bt6));
button6 = new ImageButton(drawable5);
TextureRegion bt7 = new TextureRegion(new Texture("7OFF.png"));
Drawable drawable6 = new TextureRegionDrawable(new TextureRegion(bt7));
button7 = new ImageButton(drawable6);
TextureRegion bt8 = new TextureRegion(new Texture("8OFF.png"));
Drawable drawable7 = new TextureRegionDrawable(new TextureRegion(bt8));
button8 = new ImageButton(drawable7);
TextureRegion bt9 = new TextureRegion(new Texture("9OFF.png"));
Drawable drawable8 = new TextureRegionDrawable(new TextureRegion(bt9));
button9 = new ImageButton(drawable8);
gamecam = new OrthographicCamera(WIDTH,HEIGHT);
gamecam.position.set(WIDTH/2,HEIGHT/2,0);
gamePort = new ScreenViewport(gamecam);
backStage = new Stage(gamePort);
fitPort = new FitViewport(gamecam.viewportWidth,gamecam.viewportHeight,gamecam);
frontStage = new Stage(fitPort);
backStage.addActor(bg);
frontStage.addActor(phone);
Table table = new Table();
table.padLeft(40);
table.setPosition(phone.getWidth()/2,phone.getHeight()/2*0.6f);
table.row().size(95,95);
table.add(buttonLeft);
table.add(buttonCenter);
table.add(buttonRight);
table.row().size(95,95);
table.add(button1);
table.add(button2);
table.add(button3);
table.row().size(95,95);
table.add(button4);
table.add(button5);
table.add(button6);
table.row().size(95,95);
table.add(button7);
table.add(button8);
table.add(button9);
frontStage.addActor(table);
}
public void render(){
backStage.act(Gdx.graphics.getDeltaTime());
frontStage.act(Gdx.graphics.getDeltaTime());
backStage.draw();
frontStage.draw();
}
@Override
public void resize(int width, int height) {
gamePort.update(width, height);
frontStage.getViewport().update(width, height);
}