需要有关Libgdx Scaling的建议

时间:2013-10-14 02:07:07

标签: android resize screen libgdx scaling

这可能是一个在某处解决的简单问题,但我找不到它。我希望有人能带领我走上正确的道路。我的应用程序的设计分辨率为800x480。为了在具有更高分辨率的设备上保持正确的宽高比,我跟着this post并设法在更大的屏幕(nexus 7)两侧获得“黑条”(我用蓝色进行测试)。但是,似乎舞台没有缩放以覆盖屏幕。请看下面的屏幕截图,蓝色是Gdx.gl.glClearColor(0.5f,1,1,1);黑色矩形(800x480)是实际的精灵。

Link to image

我不确定我哪里出错了。任何帮助深表感谢。代码如下:

private SpriteBatch batch;
private Texture splashTexture;
private Sprite splashSp;
TextureRegion splashTr;
Stage stage;

@Override
public void create() {

    stage = new Stage();

    batch = new SpriteBatch();

    splashTexture = new Texture(Gdx.files.internal("img/splashTexture.png"));
    splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    TextureRegion splashTr = new TextureRegion(splashTexture, 0, 0, 800, 480);

    splashSp = new Sprite(splashTr);

    Gdx.app.log("myapp", "Creating game");
}

@Override
public void render() {      
    Gdx.gl.glClearColor(0.5f, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    stage.draw();

    batch.begin();
    batch.draw(splashSp, 0, 0);

    batch.end();
}

@Override
public void resize(int width, int height) {

    Gdx.app.log("myapp", "width:" + width + " height:" + height);
    Vector2 size = Scaling.fit.apply(800, 480, width, height);
    int viewportX = (int)(width - size.x) / 2;
    int viewportY = (int)(height - size.y) / 2;
    int viewportWidth = (int)size.x;
    int viewportHeight = (int)size.y;
    Gdx.app.log("myapp", "viewportWidth:" + viewportWidth + " viewportHeight:" + viewportHeight);
    Gdx.app.log("myapp", "viewportX:" + viewportX + " viewportY:" + viewportY);
    Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
    stage.setViewport(800, 480, true);

    Gdx.app.log("myapp", "Resizing game");
}

1 个答案:

答案 0 :(得分:1)

您需要设置相机和舞台。

首先声明像这样的相机变量

OrthographicCamera camera;

然后在create方法中执行此操作

camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
camera.update();

mystage = new Stage(800, 480, false);

并在渲染方法中更新相机

camera.update();

对我来说很好..

相关问题