无法正确缩放表LibGdx

时间:2014-04-06 02:19:25

标签: java android libgdx

Table table;
Stage stage;
Skin skin;
TextureAtlas atlas;
private BitmapFont black;
OrthographicCamera camera;

private static final int VIRTUAL_WIDTH = 1280;
private static final int VIRTUAL_HEIGHT = 720;
private static final float ASPECT_RATIO = (float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;
private Rectangle viewport;


@Override
public void render(float delta) {
    Gdx.gl.glClearColor(1,1,1,1);
    camera.update();
    try {
        camera.apply(Gdx.gl10);
    } catch(Exception e) {
    }

    // 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);

    Table.drawDebug(stage);
    stage.draw();
}

@Override
public void resize(int width, int height) {
    // calculate new viewport
    float aspectRatio = (float)width/(float)height;
    float scale = 1f;
    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 show() {
    stage = new Stage();

    camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);

    black = new BitmapFont(Gdx.files.internal("data/font.fnt"), false);

    atlas = new TextureAtlas("data/button.pack");
    skin = new Skin(atlas);

    table = new Table(skin);
    table.setPosition(50, Gdx.graphics.getHeight()-(Gdx.graphics.getHeight()-50));
    table.setSize(Gdx.graphics.getWidth()-100, Gdx.graphics.getHeight()-100);

    LabelStyle style = new LabelStyle();
    style.font = black;

    table.align(Align.top);

    Label label = new Label("ORTHO", style);
    label.setWrap(true);
    label.setAlignment(Align.left);

    Label label1 = new Label("A", style);
    label1.setWrap(true);
    label1.setAlignment(Align.center);

    Label label2 = new Label("B", style);
    label2.setWrap(true);
    label2.setAlignment(Align.center);

    Label label3 = new Label("C", style);
    label3.setWrap(true);
    label3.setAlignment(Align.center);

    table.add(label).width(Gdx.graphics.getWidth()-100).colspan(3).align(Align.left).padBottom(50);

    table.row();

    table.add(label1).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
    table.add(label2).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
    table.add(label3).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);

    table.row();

    table.add(label1).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
    table.add(label2).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
    table.add(label3).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);

    table.row();

    table.add(label1).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
    table.add(label2).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
    table.add(label3).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);

    table.debug();
    stage.addActor(table);

    TextButtonStyle textButtonStyle = new TextButtonStyle();
    textButtonStyle.up = skin.getDrawable("button.up");
    textButtonStyle.down = skin.getDrawable("button.down");
    textButtonStyle.pressedOffsetX = 1;
    textButtonStyle.pressedOffsetY = -1;
    textButtonStyle.font = black;
    textButtonStyle.font.setScale(3);

    TextButton button = new TextButton("game", textButtonStyle);

    button.addListener(new InputListener() {
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("hello");
            return false;
        }
    });
}

我期待我正在使用此代码创建的表格适合屏幕,并且它在我的Galaxy S3上完美运行,我添加了

private Rectangle viewport;
....
....
Gdx.gl.glClearColor(1,1,1,1);
camera.update();
try {
    camera.apply(Gdx.gl10);
} catch(Exception e) {
}
....
....
// set viewport
Gdx.gl.glViewport((int) viewport.x, (int) viewport.y,
                 (int) viewport.width, (int) viewport.height);
....
....
float aspectRatio = (float)width/(float)height;
    float scale = 1f;
    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);
....
....
camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);

将其正确地扩展到其他设备,例如我已经运行的480-320模拟器,但它根本没有,它可以很好地扩展它,但是将表格放低到它应该的位置......

0 个答案:

没有答案