2014-03-26 61 views
1

我想要的是在更改屏幕尺寸時相應地更改大小的位圖字體。我的意思是在我的電腦上,字體顯得相當大,但在我的手機上,它是一種難以閱讀的小字體。我可以改變尺寸,但我希望它在所有屏幕上都看起來很相似,而不是在一個屏幕上顯示較大,而在另一個屏幕上顯示較小。這裏是我的代碼,看看我有什麼工作,並具:libgdx如何將BitmapFont縮放爲更改屏幕尺寸?

public void render() { 
    //score system 
    scoreFont.setColor(1.0f, 1.0f, 1.0f, 1.0f); 
    scoreBatch.begin(); 
    scoreFont.draw(scoreBatch, Long.toString(getScore()), 10, Gdx.graphics.getHeight() - 10); 
    scoreFont.setScale(3, 3); 
    scoreBatch.end(); 
} 

public void resize(int width, int height) { 
    camera.viewportWidth = 450; 
    camera.viewportHeight = 250; 
    camera.update(); 
    stage.setViewport(450, 250, true); 
    stage.getCamera().translate(-stage.getGutterWidth(), -stage.getGutterHeight(), 0); 
} 

回答

2

認爲它這樣,你總是希望有相同的比例。

例如:

3分之500= 450/X

x是文本的新的大小。所以你必須做一些交叉乘法。

500X = 1350

1350÷500 = X

所以現在以編程方式做到這一點。

public void resizeText(float width, float currentSize, float currentWidth){ 
    //currentWidth/currentSize = width/x 
    a = width * currentSize;//450 * 3 in example above 
    b = a/currentWidth; 
    return b;//returns the x or the new size that your text should be 
} 

此外,你還說,它需要改變,取決於大小超過一定的數量。

因此,這裏是我設計

ApplicationType appType = Gdx.app.getType(); 
    if (appType == ApplicationType.Android || appType == ApplicationType.iOS) { 
     screenFont.setScale(your number) 
    } else { screen font.setScale(otherNum)} //if its a desktop 
+0

其實我找到了一個簡單的解決方案。我在render()和resize()方法中設置字體大小。我從render()方法中刪除了它 – Mercify

0

規模由Gdx.graphics.getDensity()

+1

請加如何工作的一些解釋一件小事。這看起來更像是評論而不是答案。 –

+0

@AdiInbar我同意。這看起來很模糊 – Mercify

+0

從API文檔 - 這是密度無關像素單位的縮放因子,遵循與android.util.DisplayMetrics#密度相同的約定,其中一個DIP在大約160 dpi的屏幕上是一個像素。 – Chase