java - Android Libgdx 纹理大小

标签 java android libgdx

我在缩放图像时遇到问题,每个设备都有不同的屏幕分辨率,我不知道如何在 Libgdx 平台中缩小它们, 例如今天我为Android制作了一个小游戏,我面临的问题是.. enter image description here

offcurse 我们可以缩小它们,但这不是正确的方法,因为如果有人有平板电脑,按钮将非常适合,并且据我所知,在 libgdx 中我无法使用布局 sw600dp、large、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;
    }
    });

最佳答案

看起来您依赖于资源大小并使用虚构的像素单位。看看this post为什么你不应该这样做。相反,您可以使用 a viewport具有更有意义的单位。例如。您可以为视口(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

关于java - Android Libgdx 纹理大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30821895/

相关文章:

android - 使用本地服务器套接字转换音频

Android:在调试器断点时,由于 ANR,dalvik 杀死应用程序

java - Libgdx-在指针后面画一条线

java - Spring Boot应用程序oneToMany关系中的assertTrue异常

java - Jtable 在同一行左右对齐文本

Android NFC Intents 没有开始我的 Activity

java - 动态地将游戏从纵向更改为横向

java - LibGdx 双击

java - 跨 Android 应用程序使用的自定义 View 和属性

java - <c :foreach> jSTL tag doesn't print anything