2013-05-30 70 views
3

我開始學習libgdx和它的scene2d,但是我的splashScreen一直有問題。我的淡入淡出的動作完美,但圖像沒有被縮放(即使當我添加一個縮放到構造函數)...Scene2d圖像不會縮放

我有一個紋理splashTexture從PNG 512x512加載,其中真正的圖像是512x256 ,所以我創建了一個TextureRegion。 所有這一切都在我的表演方式完成:

@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); 

} 

這些都是在一個延伸的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年齡,並沒有找到一個解決辦法!

謝謝

bnunamak

回答

5

退房stage.setViewport(float width, float height, boolean keepAspectRatio)。這聽起來像你想要的形象來填充屏幕,所以舞臺的視口寬度/高度設置爲圖像的寬/高:

stage.setViewport(512, 256, false); 

keepAspectRatio參數的說明,請參閱scene2d wiki article

setViewport有一個名爲keepAspectRatio的參數,當舞臺尺寸和視口尺寸縱橫比不同時,該參數僅具有 效果。如果 錯誤,則舞臺被拉伸以填充視口,這可能會扭曲縱橫比。如果屬實,則首先對舞臺進行縮放,以適合最長維度的視口 。接下來縮短尺寸 以填充視口,從而保持縱橫比從 更改。

如果這不完全是你想要的,這篇文章有幾個不同的例子,應該涵蓋你所需要的。

+0

謝謝!那正是我需要的! – bnunamak