Libgdx在android上调整窗口大小以填充屏幕

时间:2013-01-17 01:52:59

标签: java android libgdx

我正在尝试填充Libgdx附带的调整大小功能以适应所有的Android屏幕。

到目前为止,我看到的最佳解决方案是本教程:http://www.java-gaming.org/index.php?topic=25685.0但是当我在Galaxy S3上运行时,按钮非常小,整个游戏总体上很小,在桌面版本上它很大以及我希望它的方式。我现在调整大小的代码是:

@Override
public void resize(int width, int height) {
    float aspectRatio = (float)width/(float)height;
    float scale = 2f;
    Vector2 crop = new Vector2(0f, 0f); 
    if(aspectRatio > ASPECT_RATIO)
    {
        scale = (float)height/(float)VIRTUAL_HEIGHT;
        crop.x = (width - VIRTUAL_WIDTH*scale)/2f;
    }
    else if(aspectRatio < ASPECT_RATIO)
    {
        scale = (float)width/(float)VIRTUAL_WIDTH;
        crop.y = (height - VIRTUAL_HEIGHT*scale)/2f;
    }
    else
    {
        scale = (float)width/(float)VIRTUAL_WIDTH;
    }

    float w = (float)VIRTUAL_WIDTH*scale;
    float h = (float)VIRTUAL_HEIGHT*scale;
    viewport = new Rectangle(crop.x, crop.y, w, h);
}

我的渲染代码是:

@Override
public void render() {
    // update camera
    camera.update();

    // set viewport
    Gdx.gl.glViewport((int) viewport.x, (int) viewport.y,
                      (int) viewport.width, (int) viewport.height);

    // clear previous frame
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    ScreenManager.updateScreen();

    sb.begin();
    ScreenManager.renderScreen(sb);
    sb.end();
}

我的变数是:

private static final int VIRTUAL_WIDTH = 600;
private static final int VIRTUAL_HEIGHT = 800;
private static final float ASPECT_RATIO = (float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;

public static BitmapFont FONT;

private OrthographicCamera camera;
private Rectangle viewport;
private SpriteBatch sb;

就像我说的那样,当我运行桌面版时它工作正常但是当我在我的Galaxy S3上运行它(屏幕昏暗:1,280 x 720)时,游戏会缩小很多,它会突出顶部和侧面这个游戏根本不是我想要的。

0 个答案:

没有答案