不知大家有没有注意过libgdx显示汉字的问题,其实还是非常值得关注,试想如果libgdx不支持中文,那么使用起来就很不方便了。
我们来看看BitmapFont类,这是有关文字绘制的。看一下源码:
publicBitmapFont ()
{
this(Gdx.files.classpath("com/badlogic/gdx/utils/arial-15.fnt"), Gdx.files.classpath("com/badlogic/gdx/utils/arial-15.png"),false,true);
}
这是默认的构造函数,可以看出它加载了两个文件arial-15.fnt和arial-15.png。
arial-15.fnt文件的部分内容:
info face="Arial"size=15bold=0italic=0charset=""unicode=0stretchH=100smooth=1aa=1padding=0,0,0,0spacing=1,1
common lineHeight=18base=14scaleW=256scaleH=256pages=1packed=0
page id=0file="arial-15.png"
chars count=189
charid=32x=0y=0width=0height=0xoffset=0yoffset=14xadvance=4page=0chnl=0
charid=255x=0y=0width=8height=19xoffset=-1yoffset=0xadvance=7page=0chnl=0
charid=254x=8y=0width=9height=19xoffset=1yoffset=0xadvance=8page=0chnl=0
charid=253x=17y=0width=8height=19xoffset=-1yoffset=0xadvance=7page=0chnl=0
charid=252x=25y=0width=9height=19xoffset=1yoffset=0xadvance=8page=0chnl=0
charid=251x=34y=0width=9height=19xoffset=1yoffset=0xadvance=8page=0chnl=0
charid=250x=43y=0width=9height=19xoffset=1yoffset=0xadvance=8page=0chnl=0
charid=249x=52y=0width=9height=19xoffset=1yoffset=0xadvance=8page=0chnl=0
charid=248x=61y=0width=9height=19xoffset=1yoffset=0xadvance=8page=0chnl=0
charid=247x=70y=0width=9height=19xoffset=1yoffset=0xadvance=8page=0chnl=0
charid=246x=79y=0width=9height=19xoffset=1yoffset=0xadvance=8page=0chnl=0
charid=245x=88y=0width=9height=19xoffset=1yoffset=0xadvance=8page=0chnl=0
charid=244x=97y=0width=9height=19xoffset=1yoffset=0xadvance=8page=0chnl=0
charid=243x=106y=0width=9height=19xoffset=1yoffset=0xadvance=8page=0chnl=0
charid=242x=115y=0width=9height=19xoffset=1yoffset=0xadvance=8page=0chnl=0
charid=241x=124y=0width=9height=19xoffset=1yoffset=0xadvance=8page=0chnl=0
charid=240x=133y=0width=9height=19xoffset=1yoffset=0xadvance=8page=0chnl=0
charid=239x=142y=0width=5height=19xoffset=0yoffset=0xadvance=3page=0chnl=0
…
kernings count=374
kerning first=49second=49amount=-1
kerning first=121second=44amount=-1
kerning first=121second=46amount=-1
kerning first=119second=44amount=-1
kerning first=119second=46amount=-1
kerning first=118second=44amount=-1
kerning first=118second=46amount=-1
kerning first=114second=44amount=-1
kerning first=114second=46amount=-1
kerning first=89second=44amount=-2
kerning first=89second=45amount=-1
kerning first=89second=46amount=-2
kerning first=89second=58amount=-1
kerning first=89second=59amount=-1
kerning first=89second=65amount=-1
kerning first=89second=97amount=-1
kerning first=89second=101amount=-1
…
再看看arial-15.png:
可以很明显看出,libgdx的文字绘制是根据fnt文件获取对应文字的在png中的坐标位置,然后截取图片的相应部分进行绘制。
那么要让libgdx支持中文思路就很简单了,我们自己构造fnt和png文件,其中包含我们要使用的中文即可。
作者给我们提供了一个对应的工具:Hiero。
下载后双击运行,在右侧列表中选择一个可以用的字体,然后输入需要的中文,最好保留自动生成的英文和符号。
在右侧的Effects中可以设置效果:
点File—Save as BMFont Files,生成两个文件,将它们拷贝到asserts文件夹。
使用
bitmapFont =newBitmapFont(Gdx.files.internal("cf.fnt"), Gdx.files.internal("cf.png"),false);
指定我们生成的文件作为绘制的参考,然后绘制:
bitmapFont.draw(spriteBatch,"FPS"+ Gdx.graphics.getFramesPerSecond(),5, Gdx.graphics.getHeight() -10);
bitmapFont.draw(spriteBatch, "祝大家光棍节快乐",0, Gdx.graphics.getHeight()/2-8);
程序运行效果:
关于多行文字,可以调用
publicTextBounds drawMultiLine (SpriteBatch spriteBatch, CharSequence str,floatx,floaty)
或者
publicTextBounds drawMultiLine (SpriteBatch spriteBatch, CharSequence str,floatx,floaty,floatalignmentWidth,HAlignment alignment)
【编辑推荐】
【责任编辑:milk TEL:(010)68476606】
点赞 0