Libgdx纹理大小Android

时间:2015-06-13 18:03:53

标签: java android libgdx

我在缩放图像方面遇到问题,每台设备都有不同的屏幕分辨率,我无法弄清楚如何在Libgdx平台中缩小它们的尺寸, 例如,今天我为Android做了一个小游戏,我面临的问题是...... enter image description here

我们可以缩小它们,但这不是正确的方法,因为如果有人有平板电脑Butttons将完美,并在libgdx据我所知,我不能使用布局sw600dp,大,xlarge等。 ..我们可以根据屏幕宽度和高度来缩放它们

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

1 个答案:

答案 0 :(得分:3)

看起来您依赖资产规模并使用虚构像素单元。看看this post为什么你不应该这样做。相反,您可以使用a viewport更有意义的单位。例如。您可以使用屏幕的实际大小(例如,以英寸或厘米为单位),然后指定您希望它们的按钮大小(再次以英寸或厘米为单位)。您可能还需要/需要调用Drawable#setMinWidth(),具体取决于您的皮肤。

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