LibGDX如何为每个设备缩放图像

时间:2016-05-27 07:54:02

标签: java android libgdx

我的代码运行如下:

public class MainMenuOptions extends AbstractScreen {

private Texture newGameButton;
private Texture exitGameButton;
private Texture highScoresButton;
private Texture background;


public MainMenuOptions(FallingPeopleMain game) {
    super(game);
    init();
}

 private void init() {
    newGameButton = new Texture("newGameButton.jpg");
     exitGameButton = new Texture("exitGameButton.jpg");
     highScoresButton = new Texture("highScoresGameButton.jpg");
     background = new Texture("BG.png");

}

@Override
public void render(float delta) {
    spriteBatch.begin();
    spriteBatch.draw(background, 0, 0);
    spriteBatch.draw(newGameButton, 180, 380);
    spriteBatch.draw(highScoresButton, 180, 340);
    spriteBatch.draw(exitGameButton, 180, 300);

    spriteBatch.end();

}

}

但是当我在我的设备上播放时,华为p8 Lite在40%的屏幕设备上看起来非常废话。如何扩大一切以保持良好状态?

我在主类

上声明了Width和Height

2 个答案:

答案 0 :(得分:0)

您是否在清单文件中正确设置了屏幕方向?如果您只希望应用程序以“站立模式”运行,请将清单中的android:screenOrientation设置为"portrait"。如果您希望应用程序以“放下模式”运行,请将其设置为"landscape"

希望这会有所帮助。 :)

答案 1 :(得分:0)

@Override
public void render(float delta) {
    float widthScale = Gdx.graphics.getWidth() / originalWidth;
    float heightScale = Gdx.graphics.getHeight() / originalHeight;
    spriteBatch.begin();
    spriteBatch.draw(background, 0, 0, background.getWidth() * widthScale, background.getHeight() * heightScale);
    spriteBatch.draw(newGameButton, 180, 380, newGameButton.getWidth() * widthScale, newGameButton.getHeight() * heightScale);
    spriteBatch.draw(highScoresButton, 180, 340, highScoresButton.getWidth() * widthScale, highScoresButton.getHeight() * heightScale);
    spriteBatch.draw(exitGameButton, 180, 300, exitGameButton.getWidth() * widthScale, exitGameButton.getHeight() * heightScale);

    spriteBatch.end();

}

originalWidth和originalHeight是您的桌面(开发)版本的宽度和高度(即使它们与实际宽度和高度相同,无论如何都要单独声明它们 - 它只表示纹理以原始大小显示) 。 originalWidth和originalHeight之间的比例必须是9:16(720x1280 / 360x640 ......)或16:9。