2014-03-26 62 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