【问题标题】:Libgdx Texture Size AndroidLibgdx 纹理大小 Android
【发布时间】:2015-06-13 18:03:52
【问题描述】:

我在缩放图像时遇到问题,每个设备都有不同的屏幕分辨率,我无法弄清楚如何在 Libgdx 平台中缩小它们, 例如,今天我为 android 制作了一个小游戏,我面临的问题是..

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

【问题讨论】:

    标签: java android libgdx


    【解决方案1】:

    您似乎依赖于资产大小并使用虚构的像素单位。看看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
    

    【讨论】:

    • 感谢您的回答,这非常有帮助,但我有一个问题..我有屏幕宽度、高度 100 米和 VIEWPORT_WIDTH、VIEWPORT_HEIGHT 30 米,我想不管屏幕尺寸的设备是什么让用户只看到那个特定的视口而不是更多而不是更少.. 到目前为止我们很好但是在缩放之后所有的 OrthogonalTiledMapRenderer 都会变形。
    • 听起来你想使用另一个视口。在这个答案中,我使用实际的屏幕尺寸,所以它会有正确的纵横比。如果您需要不同的纵横比,那么您需要决定如何处理这种差异。这个伸展,而其他人使用各种技术来保持纵横比。检查答案中的链接。
    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 2014-10-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多