gpt4 book ai didi

java - Scene2d 图像无法缩放

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:49:50 25 4
gpt4 key购买 nike

我开始学习 libgdx 和它的 scene2d,但我的 splashScreen 一直有问题。我的淡入淡出 Action 完美无缺,但图像未缩放(即使我向构造函数添加了缩放)...

我有一个从 png 512x512 加载的纹理 splashTexture,其真实图像是 512x256,所以我创建了一个 TextureRegion。所有这些都在我的 show 方法中完成:

@Override
public void show() {
super.show(); //sets inputprocessor to stage

splashTexture = new Texture(SPLASHADR);

// set the linear texture filter to improve the stretching
splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
splashTextureRegion = new TextureRegion(splashTexture, 0, 0, 512, 256);

}

然后在我的调整大小方法中出现以下内容:

@Override
public void resize(int width, int height) {
stage.clear();
Drawable splashTextureDrawable = new TextureRegionDrawable(
splashTextureRegion);

Image splashImg = new Image(splashTextureDrawable);

splashImg.getColor().a = 0f;
splashImg.addAction(Actions.sequence(Actions.fadeIn(0.5f),
Actions.delay(2f), Actions.fadeOut(0.5f)));

stage.addActor(splashImg);

}

这些是 SplashScreen 类中的函数,它扩展了 AbstractScreen 类(实际上具有渲染函数):

@Override
public void render(float delta) {
stage.act(delta);

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

stage.draw();
}

欢迎提出任何想法,我已经查看了 javadoc 多年,但还没有找到解决方案!

谢谢,

布努纳马克

最佳答案

查看 stage.setViewport(float width, float height, boolean keepAspectRatio)。听起来您希望图像填满屏幕,因此请将舞台的视口(viewport)宽度/高度设置为图像的宽度/高度:

stage.setViewport(512, 256, false);

参见 scene2d wiki article keepAspectRatio 参数的解释:

setViewport has a parameter named keepAspectRatio which only has an effect when the stage size and viewport size aspect ratio differ. If false, the stage is stretched to fill the viewport, which may distort the aspect ratio. If true, the stage is first scaled to fit the viewport in the longest dimension. Next the shorter dimension is lengthened to fill the viewport, which keeps the aspect ratio from changing.

如果这不是您想要的,本文提供了几个不同的示例,应该可以满足您的需要。

关于java - Scene2d 图像无法缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16840895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com