Android中的LibGDX背景大小错误

时间:2014-07-19 17:37:47

标签: android libgdx

我从一本关于LibGDX的书中获取了一些代码,并将其改编为我自己的项目,不幸的是,我有点陷入困境。

我有一个名为MenuScreen的课程(见下文)当然会显示菜单屏幕。它与原始代码基本相同,只适用于我正在使用的图像。在本书中的项目中,用于菜单的背景图像正确显示,在桌面上填充800x480窗口,并填写我的Nexus 7屏幕。

使用我的项目,图像在桌面上显示正常,但在Nexus 7上显示的更小。我有一个针对此项目的最新版本,我知道有关Viewport的一些更改。我假设它现在以屏幕上的实际尺寸显示,但实际上它现在比应该是的要小得多......我有一个2012 N7,分辨率为1280 x 800,但显示在约550 x 300,即使图像本身是800 x 480.

有人可以指出我正确的方向来正确显示这个图像吗?

public class MenuScreen extends AbstractGameScreen {
private static final String TAG = MenuScreen.class.getName();
private Stage stage;
private Skin demoSkin;
private Skin skinLibGdx;

private Viewport viewport;
private Image imgBackground;
// options
private Window winOptions;
private TextButton btnWinOptSave;
private TextButton btnWinOptCancel;
private TextButton optionsButton;
private TextButton startButton;
private Slider numBlocks;
private CheckBox chkShowFpsCounter;


public MenuScreen (Game game)
{
    super(game);
}

@Override
public void render(float deltaTime) {
    Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act(deltaTime);
    stage.draw();
    Table.drawDebug(stage);

}

@Override
public void resize(int width, int height) {
    stage.getViewport().update((int)Constants.VIEWPORT_GUI_WIDTH, (int)Constants.VIEWPORT_GUI_HEIGHT, false);
}

@Override
public void show() {
    stage = new Stage();
    rebuildStage();
}

private void rebuildStage() {
    demoSkin = new Skin(Gdx.files.internal(Constants.SKIN_PHYSICSDEMO_UI), new TextureAtlas(Constants.TEXTURE_ATLAS_UI));
    skinLibGdx = new Skin(Gdx.files.internal(Constants.SKIN_LIBGDX_UI), new TextureAtlas(Constants.TEXTURE_ATLAS_LIBGDX_UI));
    // build all layers
    Table layerBackground = buildBackgroundLayer();
    Table layerControls = buildControlsLayer();
    Table layerOptionsWindow = buildOptionsWindowLayer();

    // assemble stage for menu screen
    stage.clear();
    Stack stack = new Stack();
    stage.addActor(stack);

    stack.setSize(Constants.VIEWPORT_GUI_WIDTH, Constants.VIEWPORT_GUI_HEIGHT);

    stack.add(layerBackground);
    stack.add(layerControls);
    //stage.addActor(layerOptionsWindow);

}

private Table buildOptionsWindowLayer() {
    winOptions = new Window("Options", skinLibGdx);
    // Make options window slightly transparent
    winOptions.setColor(1, 1, 1, 0.8f);
    // Hide options window by default
    winOptions.setVisible(false);
    //showOptionsWindow(false, false);
    // Let TableLayout recalculate widget sizes and positions
    winOptions.pack();
    // Move options window to bottom right corner
    winOptions.setPosition(Constants.VIEWPORT_GUI_WIDTH - winOptions.getWidth() - 50, 50);
    return winOptions;
}

private void showOptionsWindow (boolean visible, boolean animated) {
    float alphaTo = visible ? 0.8f : 0.0f;
    float duration = animated ? 1.0f : 0.0f;
    Touchable touchEnabled = visible ? Touchable.enabled : Touchable.disabled;
    winOptions.addAction(sequence(touchable(touchEnabled), alpha(alphaTo, duration)));
}

private Table buildControlsLayer() {
    Table layer = new Table();
    startButton = new TextButton("Start Simulation", skinLibGdx);
    optionsButton = new TextButton("Options", skinLibGdx);
    layer.add(startButton);
    layer.row();
    layer.add(optionsButton);
    return layer;
}

private Table buildBackgroundLayer() {
    Table layer = new Table();
    imgBackground = new Image(demoSkin, "background");
    layer.add(imgBackground);
    return layer;
}

@Override
public void hide() {
    stage.dispose();
    skinLibGdx.dispose();
    demoSkin.dispose();
}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public InputProcessor getInputProcessor() {
    return stage;
}

}

1 个答案:

答案 0 :(得分:0)

您需要单独设置背景图像的大小。

imgBackground.setSize(imageWidth, imageHeight);

由于您要在Viewport方法中更改resize尺寸,因此对于分辨率较高的屏幕,图像会变得更小。因此,图像尺寸必须设置得足够大。

您可以通过将原始图像大小乘以缩放系数(实际分辨率与假定分辨率的比率)来计算所需的图像大小。

祝你好运。