kkafkas
kkafkas

Reputation: 342

Libgdx Texture Size Android

I'm having problem with scaling images ,every device has different screen resolution and i can't figure out how scale them down in the Libgdx Platform, for example today i've made a small game for android and the problem that i am facing is.. enter image description here

offcurse we can scale them down ,but this is not the right way, because if someone has a tablet the Butttons will fit perfect,and in libgdx as far as i know i cant use the layouts sw600dp,large,xlarge etc ...how we can scale them depend of the screen width and height

stage = new Stage();
    Gdx.input.setInputProcessor(stage);
    font = new BitmapFont();
    skin = new Skin();
    buttonAtlas = new TextureAtlas(Gdx.files.internal("data/Spritesheetbuttons.pack"));
    skin.addRegions(buttonAtlas);
    Table table = new Table();
    table.setBounds(0,0,buttonAtlas.getRegions().get(0).getRegionWidth()*2 , buttonAtlas.getRegions().get(0).getRegionHeight());

    textButtonStyle = new TextButtonStyle();
    textButtonStyle.font = font;
    textButtonStyle.up = skin.getDrawable("left");
    textButtonStyle.down = skin.getDrawable("left");
    textButtonStyle.checked = skin.getDrawable("left");
    buttonLeft = new TextButton("Button1", textButtonStyle);
    table.add(buttonLeft);

    textButtonStyle = new TextButtonStyle();
    textButtonStyle.font = font;
    textButtonStyle.up = skin.getDrawable("right");
    textButtonStyle.down = skin.getDrawable("right");
    textButtonStyle.checked = skin.getDrawable("right");
    buttonRight = new TextButton("Button1", textButtonStyle);
    table.add(buttonRight);
    stage.addActor(table);



    table = new Table();
    table.setBounds(Game.SCREEN_WIDTH-buttonAtlas.getRegions().get(0).getRegionWidth(),0,buttonAtlas.getRegions().get(0).getRegionWidth() , buttonAtlas.getRegions().get(0).getRegionHeight()*2);

    textButtonStyle = new TextButtonStyle();
    textButtonStyle.font = font;
    textButtonStyle.up = skin.getDrawable("up");
    textButtonStyle.down = skin.getDrawable("up");
    textButtonStyle.checked = skin.getDrawable("up");
    buttonUp = new TextButton("Button1", textButtonStyle);
    table.add(buttonUp);
    table.row();

    textButtonStyle = new TextButtonStyle();
    textButtonStyle.font = font;
    textButtonStyle.up = skin.getDrawable("down");
    textButtonStyle.down = skin.getDrawable("down");
    textButtonStyle.checked = skin.getDrawable("down");
    buttonDown = new TextButton("Button1", textButtonStyle);
    table.add(buttonDown);
    stage.addActor(table);


    buttonRight.addListener(new EventListener() {

        @Override
        public boolean handle(Event event) {

        }
    });
    buttonLeft.addListener(new EventListener() {

        @Override
        public boolean handle(Event event) {

        }
    });
    buttonDown.addListener(new EventListener() {

    @Override
    public boolean handle(Event event) {
        // TODO Auto-generated method stub
        return false;
    }
    });
    buttonUp.addListener(new EventListener() {

    @Override
    public boolean handle(Event event) {
        // TODO Auto-generated method stub
        return false;
    }
    });

Upvotes: 1

Views: 854

Answers (1)

Xoppa
Xoppa

Reputation: 8113

It looks like you're relying on asset size and using imaginary pixel units. Have a look at this post why you shouldn't do that. Instead you could use a viewport with a more meaningful units. E.g. you could use the actual size of the screen (e.g. in inches or centimeters) for your viewport and then specify the size of the button (again in inches or centimeters) that you want them to be. You might also want/need to call the Drawable#setMinWidth(), depending on your skin.

float widthInches = (float)Gdx.graphics.getWidth() * Gdx.graphics.getPpiX();
float heightInches = (float)Gdx.graphics.getHeight() * Gdx.graphics.getPpiY();
stage = new Stage(new StretchViewport(widthInches, heightInches));
...
// e.g. if you want your buttons to be 1 by 1 inch 
button.setSize(1f, 1f);
button.getStyle().up.setMinWidth(1f);
button.getStyle().up.setMinHeight(1f);
... etc. for the other drawables your button uses

Upvotes: 3

Related Questions