使用ExtendViewport的Java libGDX黑条

时间:2017-04-12 08:46:59

标签: java android graphics libgdx viewport

根据documentationExtendViewport应该扩展视图并保持纵横比,但每当我尝试实现此viewPort时,我在手机屏幕的侧面有两个小节。 enter image description here(这是示例背景图片,上面有一些文字和按钮,用于检查拉伸的方式)。

代码:

 public class Overlap2d extends Game {
public static  float ASPECT_RATIO ;
public static final int GAME_SCREEN_WIDTH = 40;
public static final int GAME_SCREEN_HEIGHT = 80;

@Override
public void create () {
    ASPECT_RATIO=(float)Gdx.graphics.getWidth()/(float)Gdx.graphics.getHeight() ;
    ScreenManager.getScreenManager().init(this);
    ScreenManager.getScreenManager().showScreen( ScreenName.MAIN_MENU );
}}

public abstract class AbstractScreen extends Stage implements Screen {

public AbstractScreen() {
    super(new ExtendViewport(GAME_SCREEN_WIDTH,GAME_SCREEN_HEIGHT,new OrthographicCamera()));
}
public abstract void buildStage();
@Override
public void render(float delta) {

    Gdx.gl.glClearColor(0, 1, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    super.act(delta);
    super.draw();
}
@Override
public void resize(int width, int height) {
getViewport().update(width,height);
}
@Override
public void show() {
    Gdx.input.setInputProcessor(this);
}
@Override
public void resume() {}
@Override
public void pause() {}
@Override
public void hide() {}}



public class MenuScreen extends AbstractScreen{
private Texture txtrBg;
public MenuScreen() {
    super();
    txtrBg   = new Texture( Gdx.files.internal("test.png") );
}

@Override
public void buildStage() {

    getCamera().position.set(GAME_SCREEN_WIDTH / 2, GAME_SCREEN_HEIGHT / 2, 0);
    Image bg = new Image(txtrBg);
    bg.setSize(GAME_SCREEN_WIDTH,GAME_SCREEN_HEIGHT);
    bg.setPosition(0,0);
    addActor(bg);
}

@Override
public void dispose() {
    super.dispose();
    txtrBg.dispose();

}}

1 个答案:

答案 0 :(得分:0)

public static final int GAME_SCREEN_WIDTH = 40;
public static final int GAME_SCREEN_HEIGHT = 80;

你使用的宽高比为1:2,但只有非常适合的视图设备适合这个比例,所以为什么不尝试48和80作为视口宽度和高度。

问题几乎相同,所以解释也适用于此

https://stackoverflow.com/a/43398153/3445320