0
\$\begingroup\$

I am new to the libgdx framework. I don't know how to scale an ImageButton for different screen sizes.

Currently it looks big on desktop, but small on the Android device.

I am using several imagebuttons and using a table to organize them like so:

public class BabyPhone extends Game {

    private Stage stage;
    private Stage stage1;
    private BabyActor bg;
    private BabyActor phone;
    private AnimatedActor squirrel;
    private AnimatedActor cow, hen;
    private ImageButton button1;
    private ImageButton button2,button3,button4,button5,button6,button7,button8,button9;
    private AnimatedActor actor;

    public static final int WIDTH = 480;
    public static final int HEIGHT = 800;

    private Table table;

    public void create () {

        bg = new BabyActor();
        bg.setTexture(new Texture("background-orange.png"));
        bg.setPosition(0,0);
        bg.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

        phone = new BabyActor();
        phone.setTexture(new Texture("blue-phone.png"));
        phone.setSize(Gdx.graphics.getWidth()-50,Gdx.graphics.getHeight()-50);
        phone.setOrigin(phone.getX()/2,phone.getY()/2);



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


        squirrel = new AnimatedActor();
        TextureRegion[] frames = new TextureRegion[14];
        for (int n = 1; n <= 14; n++)
        {
            String fileName = "1 (" + n + ").png";
            Texture tex = new Texture(fileName);
            tex.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
            frames[n-1] = new TextureRegion( tex );
        }
        Array<TextureRegion> framesArray = new Array<TextureRegion>(frames);

        final Animation anim = new Animation(0.1f, framesArray, Animation.PlayMode.LOOP);
        squirrel.setAnimation(anim);
        squirrel.setPosition(phone.getWidth()/5,phone.getHeight()/2);


        cow = new AnimatedActor();
        TextureRegion[] frames1 = new TextureRegion[12];
        for (int n = 1; n <= 12; n++)
        {
            String fileName = "2 (" + n + ").png";
            Texture tex = new Texture(fileName);
            tex.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
            frames1[n-1] = new TextureRegion( tex );
        }
        Array<TextureRegion> framesArray1 = new Array<TextureRegion>(frames1);

        final Animation anim1 = new Animation(0.1f, framesArray1, Animation.PlayMode.LOOP);
        cow.setAnimation(anim1);
        cow.setPosition(phone.getWidth()/5,phone.getHeight()/2);
        cow.setOrigin(cow.getWidth()/2,cow.getHeight()/2);


        hen = new AnimatedActor();
        TextureRegion[] frames2 = new TextureRegion[16];
        for (int n = 1; n <= 16; n++)
        {
            String fileName = "3 (" + n + ").png";
            Texture tex = new Texture(fileName);
            tex.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
            frames2[n-1] = new TextureRegion( tex );
        }
        Array<TextureRegion> framesArray2 = new Array<TextureRegion>(frames2);

        final Animation anim2 = new Animation(0.1f, framesArray2, Animation.PlayMode.LOOP);
        hen.setAnimation(anim2);
        hen.setPosition(phone.getWidth()/5,phone.getHeight()/2);
        hen.setOrigin(hen.getWidth()/2,hen.getHeight()/2);


        // Add actor to the stage
        stage1 = new Stage(new ScreenViewport());
        stage = new Stage(new ScreenViewport());
        stage.addActor(bg);
        stage.addActor(phone);


        Gdx.input.setInputProcessor(stage);

        button1.addListener(new InputListener()
        {
            public boolean touchDown (InputEvent event, float x,
                                      float y, int pointer, int button)
            {
                for (Actor actor : stage1.getActors()){
                    actor.remove();
                }

                stage1.addActor(squirrel);
                return true; }
            public void touchUp (InputEvent event, float x, float
                    y, int pointer, int button)
            {

            }
        });


        button2.addListener(new EventListener() {
            @Override
            public boolean handle(Event event) {
                for (Actor actor : stage1.getActors()){
                    actor.remove();
                }

                stage1.addActor(cow);
                return false;
            }
        });

        button3.addListener(new EventListener() {
            @Override
            public boolean handle(Event event) {
                for (Actor actor : stage1.getActors()){
                    actor.remove();
                }
                stage1.addActor(hen);
                return false;
            }
        });

        table = new Table();
        table.setPosition(phone.getWidth()/2,phone.getHeight()/4);
        table.add(button1);
        table.add(button2);
        table.add(button3);
        table.row();
        table.add(button4);
        table.add(button5);
        table.add(button6);
        table.row();
        table.add(button7).size(button1.getWidth()/2,button1.getHeight()/2);
        table.add(button8).size(button1.getWidth()/2,button1.getHeight()/2);
        table.add(button9).size(button1.getWidth()/2,button1.getHeight()/2);
        stage.addActor(table);


    }
    public void render(){
        stage.act(Gdx.graphics.getDeltaTime());
        stage1.act(Gdx.graphics.getDeltaTime());
        stage.draw();
        stage1.draw();
    }

    @Override
    public void resize(int width, int height) {

    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.