(Java LibGDX) 如何在 LibGDX 中调整纹理的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16886228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:16:35  来源:igfitidea点击:

(Java LibGDX) How do I resize my textures in LibGDX?

javaandroidimagetextureslibgdx

提问by user2445723

I have been fooling around with LibGDX for a while now, and wanted to easily port my programs to different systems. I have a background texture, which I want to scale to the currently used resolution. The image is 1920x1080, how do I change it to the currently used resolution at runtime?

我一直在玩 LibGDX 一段时间,想轻松地将我的程序移植到不同的系统。我有一个背景纹理,我想将其缩放到当前使用的分辨率。图像是 1920x1080,如何在运行时将其更改为当前使用的分辨率?

回答by rubmz

If you want to scale at drawing time use:

如果要在绘图时缩放,请使用:

Pixmap pixmap200 = new Pixmap(Gdx.files.internal("200x200.png"));
Pixmap pixmap100 = new Pixmap(100, 100, pixmap200.getFormat());
pixmap100.drawPixmap(pixmap200,
        0, 0, pixmap200.getWidth(), pixmap200.getHeight(),
        0, 0, pixmap100.getWidth(), pixmap100.getHeight()
);
Texture texture = new Texture(pixmap100);
pixmap200.dispose();
pixmap100.dispose();

From:

从:

https://www.snip2code.com/Snippet/774713/LibGDX-Resize-texture-on-load

https://www.snip2code.com/Snippet/774713/LibGDX-Resize-texture-on-load

回答by Lestat

    batch.begin();
    batch.draw(yourtexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    batch.end();

Using the camera viewport also works:

使用相机视口也有效:

    batch.begin();
    batch.draw(yourtexture, 0, 0, cam.viewportWidth, cam.viewportHeight);
    batch.end();