java - Scene2d 图像无法缩放

标签 java action libgdx scaling

我开始学习 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/

相关文章:

android - 如何在注销图标之前的右侧边缘(即)显示 ImageView ?

LibGDX 场景 2D : Actions won't work at all for actors inside groups

java - LibGdx 2半屏 "buttons"

java - org.hibernate.Criteria.setFetchSize() 不起作用

java - 为什么我仍然收到 NumberFormatException 错误?

java - 如何在 concurrentHashMap 中突破 foreach

java - 如何在 MAC OS 上的 LibGDX 中以编程方式调用未修饰窗口中窗口的最大化图标

java - "Ternary CAS operation"理论上可能吗?

php - WooCommerce:在订单中保存对产品的更改时 Hook

java - 如何在运行时在代码中调整 Libgdx 桌面窗口的大小