Java 如何设置Libgdx位图字体大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33633395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How set Libgdx bitmap font size?
提问by Kevin Bryan
I'm rendering on screen the game fps using bitmap font but there are no methods for the size. This is a problem for me because my camera viewport size is very small so the text when rendered is huge and pixelated.
我正在使用位图字体在屏幕上渲染游戏 fps,但没有大小的方法。这对我来说是一个问题,因为我的相机视口尺寸非常小,因此渲染时的文本很大且像素化。
font.draw(batch, Float.toString(Gdx.graphics.getFramesPerSecond), x, y);
采纳答案by Netero
Did you try setScale() method that what i use to resize my font
您是否尝试过我用来调整字体大小的 setScale() 方法
myFont.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
myFont.setScale(scale);
if you have trouble, leave a comment
如果您遇到问题,请发表评论
Good Luck !!
祝你好运 !!
Edit :
编辑 :
With the latest libgdx version, try to scale font like this :
使用最新的 libgdx 版本,尝试像这样缩放字体:
myFont.getData().setScale();
回答by Madmenyo
I often use what minos23 suggested. But the downfall is that it can look pixelated especially when scaling upwards. A fancy large bitmap font can take up a lot of space and if you need many different fonts you might go over your budget.
我经常使用 minos23 建议的内容。但缺点是它可能看起来像素化,尤其是在向上缩放时。花哨的大位图字体可能会占用大量空间,如果您需要许多不同的字体,您可能会超出预算。
By using Gdx.Freetypeyou are able to create bitmapfonts at runtime from small .ttf
files. This means you only need to ship the .ttf
files with your app and can generate a font based on user settings like resolution.
通过使用Gdx.Freetype,您可以在运行时从小.ttf
文件创建位图字体。这意味着您只需要.ttf
随应用程序一起发送文件,并且可以根据用户设置(如分辨率)生成字体。
Other then scaling and the freetype solution is having multiple bitmaps of different font sizes. This way your fonts stay crisp all time but at the cost of runtime performance to generate the proper fonts.
除了缩放之外,freetype 解决方案还有多个不同字体大小的位图。这样您的字体始终保持清晰,但以生成正确字体的运行时性能为代价。
回答by Khachatur Badalyan
I use setScale() function too as others to reduce the font size, but here I want to offer another solution and I have a question. Why you don't use FPSRenderer instance or why you don't draw your fps label on another batch which projection matrix has the screen size?
我也像其他人一样使用 setScale() 函数来减小字体大小,但在这里我想提供另一种解决方案,我有一个问题。为什么不使用 FPSRenderer 实例,或者为什么不在投影矩阵具有屏幕大小的另一个批次上绘制 fps 标签?
回答by Zoe
setScale is the function to use. Please note that with the newest LibGDX version (this changed earlier though) you need to do this isntead:
setScale 是要使用的函数。请注意,使用最新的 LibGDX 版本(虽然较早更改),您需要这样做:
font.getData().setScale(2, 2);
before it was enough to do:
在它足够做之前:
font.setScale(2, 2);
The first number in setScale is the X scale, and the second is the Y scale.
setScale 中的第一个数字是 X 比例,第二个数字是 Y 比例。
回答by Deepscorn
Netero's answer works. If you want to make code cleaner, you can write a helper function:
Netero 的答案有效。如果你想让代码更简洁,你可以写一个辅助函数:
public static void setLineHeight(BitmapFont font, float height) {
font.getData().setScale(height * font.getScaleY() / font.getLineHeight());
}