【问题标题】:Why is my background image stretched out in libGDX?为什么我的背景图片在 libGDX 中被拉长了?
【发布时间】:2018-02-14 08:41:00
【问题描述】:

这是我在 libGDX 中的第一次尝试,我以前从未见过这样的问题,谷歌搜索也没有帮助。我正在尝试显示背景,稍后我将进行此操作,但对我来说,实际显示图像是一个很好的开始。它显示,但它被拉伸(见下图)

我的代码是:

private BombArrangement game;
private OrthographicCamera gameCamera;
private Viewport gamePort;
private Texture backGroundTexture;

public PlayScreen(BombArrangement game) {
    this.game = game;
    gameCamera = new OrthographicCamera();
    gamePort = new FitViewport(BombArrangement.V_WIDTH, BombArrangement.V_HEIGHT, gameCamera);

    backGroundTexture = new Texture("startbackground.png");
}

@Override
public void show() {

}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    game.batch.begin();
    game.batch.draw(new TextureRegion(backGroundTexture, 0, 0, BombArrangement.V_WIDTH, BombArrangement.V_HEIGHT), 0, 0);
    game.batch.end();
}

@Override
public void resize(int width, int height) {
    gamePort.update(width, height);
}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {

}
}

我尝试了一些东西,比如纹理区域、精灵等等,但它们都给出了这个结果。

【问题讨论】:

  • 拉伸是什么意思?它应该是什么样子的?更小?还是填满整个屏幕?
  • 我想让我的图片填满屏幕,这是原图i.imgur.com/RUaxa5g.jpg。它需要从宽度填充高度和 1920 像素
  • 什么是BombArrangement.V_WIDTHBombArrangement.V_HEIGHT
  • 我对 libgdx 一无所知,也许 libgdx pro 可以提供帮助,但在我看来,您可能想试试 ExtendViewport。 github.com/libgdx/libgdx/wiki/Viewports
  • 我实际上使用了那些,V_HEIGHT = 1920 和 V_WIDTH = 1080

标签: java android libgdx


【解决方案1】:

不完全确定你想做什么,但我用它在我的主菜单中呈现我的背景:

    //Camera
    camera = new OrthographicCamera();
    camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    //Viewport
    ScreenViewport viewport = new ScreenViewport(camera); 

    //Background
    backgroundImage = new Texture(pathToImage);

    //Stage 
    stage = new Stage();
    stage.setViewport(viewport);

(这位于我的构造函数和相机中,backgroundImage 和 stage 是我类中的字段)

在渲染方法中 (ConfigData 保存应用于游戏的设置数据;DEFAULT_WIDHT 和 -HEIGHT 只是我在不处于全屏模式时用于初始化窗口的一些值;将它们替换为 DesktopLauncher 中使用的值

config.widht

config.height

):

@Override
public void render(float delta) {

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

    stage.act();
    stage.getBatch().begin();
    stage.getBatch().draw(backgroundImage, 0, 0, ConfigData.DEFAULT_WIDTH, ConfigData.DEFAULT_HEIGHT);
    stage.getBatch().end();

    stage.draw();
}

我的调整大小方法:

@Override
public void resize(int width, int height) {
    camera.setToOrtho(false, width, height);
    stage.getViewport().update(width, height);
}

希望这对某人有所帮助,因为我自己想出了这个并且花费了一些精力(:

【讨论】:

    【解决方案2】:

    作为对您上一条评论的回应:

    您现在可以正确缩小高度,但宽度保持不变。尝试将宽度乘以与缩小高度相同的量,因此对注释中链接的代码进行一些更改(未测试):

    private Texture texture;
    private int x1, x2, speed, scaledHeight, scaledWidth;
    
    public StartBackground() {
        texture = new Texture("startbackground.png");
    
        x1 = 0;
        x2 = texture.getWidth();
        speed = 5;
        float imageRatio = Gdx.graphics.getHeight() / texture.getHeight();
        scaledHeight = (int) (texture.getHeight() * imageRatio);
        scaledWidth = (int) (texture.getWidth() * imageRatio);
    }
    
    public void updateAndRender(float deltaTime, SpriteBatch spriteBatch) {
        x1 -= speed;
        x2 -= speed;
    
        // If image is off screen and not visible
        if (x1 + texture.getWidth() <= 0) x1 = x2 + texture.getWidth();
        if (x2 + texture.getWidth() <= 0) x2 = x1 + texture.getWidth();
    
        // Render
        spriteBatch.draw(texture, x1, 0, scaledWidth, scaledHeight);
        spriteBatch.draw(texture, x2, 0, scaledWidth, scaledHeight);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-29
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多