简体   繁体   English

Libgdx纹理大小Android

[英]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.. 我在缩放图像时遇到问题,每个设备的屏幕分辨率都不同,我无法弄清楚如何在Libgdx平台中缩小图像,例如,今天我为android制作了一个小型游戏,而我面临的问题是.. 在此处输入图片说明

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 我们可以按比例缩小它们,但是这不是正确的方法,因为如果有人拥有平板电脑,则Butttons将非常适合,并且据我所知,在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;
    }
    });

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. 相反,您可以使用具有更有意义单位的视口 Eg you could use the actual size of the screen (eg 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. 您可能还希望/需要调用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM