com.tadu.android中文,Android游戏引擎libgdx使用教程3:绘制汉字与显示中文

不知大家有没有注意过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:

6429062681f0f4f81425319bf57e1243.png

可以很明显看出,libgdx的文字绘制是根据fnt文件获取对应文字的在png中的坐标位置,然后截取图片的相应部分进行绘制。

那么要让libgdx支持中文思路就很简单了,我们自己构造fnt和png文件,其中包含我们要使用的中文即可。

作者给我们提供了一个对应的工具:Hiero。

下载后双击运行,在右侧列表中选择一个可以用的字体,然后输入需要的中文,最好保留自动生成的英文和符号。

765c42002d750c52c8f449d769b72e10.png

在右侧的Effects中可以设置效果:

ef717aee15baab0c838a144675c025cb.png

点File—Save as BMFont Files,生成两个文件,将它们拷贝到asserts文件夹。

95c45adc4a369279cb8af184b5f5d0b8.png

使用

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);

程序运行效果:

06d9744a213a449296896c371371baeb.png

关于多行文字,可以调用

publicTextBounds drawMultiLine (SpriteBatch spriteBatch, CharSequence str,floatx,floaty)

或者

publicTextBounds drawMultiLine (SpriteBatch spriteBatch, CharSequence str,floatx,floaty,floatalignmentWidth,HAlignment alignment)

【编辑推荐】

【责任编辑:milk TEL:(010)68476606】

点赞 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值