2014-10-06 153 views
0

我在LibGDX中繪製字體時遇到問題。 我希望能夠調整窗口大小和文本的可讀性。 默認應用方向是肖像,像這樣:LibGDX調整窗口字體大小圖

Portrait Application

我可以在我的應用程序開始之前,將其更改爲景觀,一切工作正常:

Landscape Application

但我希望能夠調整從縱向到橫向的應用程序。但當我嘗試(例如,從「風景」到「肖像」,文本被拉伸

Resized Application

我的代碼渲染和調整功能(這是從無到有隻是代碼):

public void render() { 

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

     //background draw 
     //_spriteBatch.begin(); 
     //_spriteBatch.draw(background, 0, 0); 
     //_spriteBatch.end(); 

     _spriteBatch.begin(); 
     //sprite.draw(batch); 
     elapsedTime += Gdx.graphics.getDeltaTime(); 
     _spriteBatch.draw(animation.getKeyFrame(elapsedTime, true), 0, 0); 
     _spriteBatch.end(); 


     //content draw 
     _spriteBatch.begin(); 

     if(!_isLandscape) 
      _spriteBatch.draw(img, 20, 210, 30, 45); 

     //NameDayDOM nameDayDOM = new NameDayDOM(); 
     NameDaySAX nameDaySAX = new NameDaySAX(); 
     name = nameDaySAX.getCurrentName(_serviceProvider.getAssetHandleResolver().resolve("namesday.xml").file()); 
     //II _font.draw(_spriteBatch, "Happy Name Day, dear " + name, Gdx.graphics.getWidth()/7.0f, Gdx.graphics.getHeight()/2.0f); 

     if(!_isLandscape) { 
      BitmapFont.TextBounds bounds = _font40.getBounds("Happy Name Day"); 
      _font40.draw(_spriteBatch, "Happy Name Day", (Gdx.graphics.getWidth()-bounds.width)/5.0f, Gdx.graphics.getHeight() - 120); 
      bounds = _font30.getBounds("dear" + name); 
      _font30.draw(_spriteBatch, "dear " + name, (Gdx.graphics.getWidth()-bounds.width)/3.2f, Gdx.graphics.getHeight() - 200); 
      bounds = _font23.getBounds("Post picture from this venue"); 
      _font23.draw(_spriteBatch, "Post picture from this venue", (Gdx.graphics.getWidth()-bounds.width)/9.0f, Gdx.graphics.getHeight()/4.7f); 
      bounds = _font23.getBounds("and tell to world,"); 
      _font23.draw(_spriteBatch, "and tell to world,", (Gdx.graphics.getWidth()-bounds.width)/7.0f, Gdx.graphics.getHeight()/5.5f); 
      bounds = _font23.getBounds("that you are enjoying your NameDay!"); 
      _font23.draw(_spriteBatch, "that you are enjoying your NameDay!", (Gdx.graphics.getWidth()-bounds.width)/2.0f, Gdx.graphics.getHeight()/6.5f); 
     }else{ 
      BitmapFont.TextBounds bounds = _font40.getBounds("Happy Name Day"); 
      _font40.draw(_spriteBatch, "Happy Name Day", (Gdx.graphics.getWidth()-bounds.width)/14.0f, Gdx.graphics.getHeight()/1.7f); 
      bounds = _font30.getBounds("dear" + name); 
      _font30.draw(_spriteBatch, "dear " + name, (Gdx.graphics.getWidth()-bounds.width)/11.0f, Gdx.graphics.getHeight()/2.0f); 
      bounds = _font23.getBounds("Post picture from this venue"); 
      _font23.draw(_spriteBatch, "Post picture from this venue", (Gdx.graphics.getWidth()-bounds.width)/14.0f, Gdx.graphics.getHeight()/4.7f); 
      bounds = _font23.getBounds("and tell to world,"); 
      _font23.draw(_spriteBatch, "and tell to world,", (Gdx.graphics.getWidth()-bounds.width)/11.0f, Gdx.graphics.getHeight()/5.5f); 
      bounds = _font23.getBounds("that you are enjoying your NameDay!"); 
      _font23.draw(_spriteBatch, "that you are enjoying your NameDay!", (Gdx.graphics.getWidth()-bounds.width)/9.0f, Gdx.graphics.getHeight()/6.5f); 
     } 
    } 
    catch(RuntimeException e) 
    { 
     _log.error("Exception on render: " + e); 
     _serviceProvider.getAppStatusListener().onStateChanged(AppState.Error); 
    } 
    finally { 
     _spriteBatch.end(); 
    } 
} 

@Override 
public void resize(int width , int height){ 
    try { 
     System.out.println(width + " " + height); 
     if (height > width) { 
      _isLandscape = false; 
     } else { 
      _isLandscape = true; 
     } 
    }catch(Exception ex){ 

    } 
} 

我在做什麼不好麼文本的佈局沒有調整窗口後更改

回答

0

嘗試添加此:

Gdx.gl.glClearColor(1, 0, 0, 1); 
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); //<--This. 
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

這是因爲您的窗口更改了大小,但OpenGL視口沒有。儘管如此,我強烈建議你使用OrtographicCamera

+0

謝謝,看來我必須使用OrtographicCamera。但我不知道,以什麼方式。請你能提供一些樣品嗎?我只需在正確調整大小後繪製文本字符串。 – 2014-10-06 16:33:45

+0

請對此有任何更新? – 2014-10-09 13:22:49

+0

https://github.com/libgdx/libgdx/wiki/Orthographic-camera – Lestat 2014-10-09 19:59:54