有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Sprite图像出现拉伸且质量差,无法获得正确的位置GDXLib

我对编程很陌生,所以请容忍我在这里

我正在制作一个2d基本游戏,只是为了在安卓 studio中练习编程,但无法将我的精灵放在屏幕上的正确位置。另外,当我绘制精灵时,它看起来是拉伸的,质量非常差。感谢您的帮助

public class MyGdxGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture background;
    Texture ball;

    @Override
    public void create () {
        batch = new SpriteBatch();
        background = new Texture("gamebackground.png");
        ball = new Texture("ball2.png");
    }

    @Override
    public void render () {

        batch.begin();
        batch.draw(background, 0,0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch.draw(ball, 0,0, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
        batch.end();
    }

共 (1) 个答案

  1. # 1 楼答案

    您需要保持原始的宽高比:

    与其将其缩放到屏幕大小的一半,不如这样定义缩放:

    float scaleFactor = 2.0f;
    batch.draw(ball, 0,0, ball.getWidth()*scaleFactor, ball.getHeight*scaleFactor);
    

    如果图像“模糊”,并且希望各个像素保持清晰,请尝试这样加载纹理:

    ball = new Texture("ball2.png");
    ball.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
    

    这将防止缩放纹理时(默认)线性插值