日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Android游戲引擎libgdx使用教程3:繪制漢字與顯示中文

不知大家有沒有注意過libgdx顯示漢字的問題,其實(shí)還是非常值得關(guān)注,試想如果libgdx不支持中文,那么使用起來就很不方便了。

創(chuàng)新互聯(lián)建站主營(yíng)輪臺(tái)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶App定制開發(fā),輪臺(tái)h5小程序開發(fā)搭建,輪臺(tái)網(wǎng)站營(yíng)銷推廣歡迎輪臺(tái)等地區(qū)企業(yè)咨詢

我們來看看BitmapFont類,這是有關(guān)文字繪制的??匆幌略创a:

 
 
 
  1. public BitmapFont ()  
  2. {  
  3.      this(Gdx.files.classpath("com/badlogic/gdx/utils/arial-15.fnt"), Gdx.files.classpath("com/badlogic/gdx/utils/arial-15.png"), false, true);  
  4. }    

這是默認(rèn)的構(gòu)造函數(shù),可以看出它加載了兩個(gè)文件arial-15.fnt和arial-15.png。

arial-15.fnt文件的部分內(nèi)容:

 
 
 
  1. info face="Arial" size=15 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1   
  2. common lineHeight=18 base=14 scaleW=256 scaleH=256 pages=1 packed=0   
  3. page id=0 file="arial-15.png"   
  4. chars count=189   
  5. char id=32 x=0 y=0 width=0 height=0 xoffset=0 yoffset=14 xadvance=4 page=0 chnl=0     
  6. char id=255 x=0 y=0 width=8 height=19 xoffset=-1 yoffset=0 xadvance=7 page=0 chnl=0     
  7. char id=254 x=8 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0     
  8. char id=253 x=17 y=0 width=8 height=19 xoffset=-1 yoffset=0 xadvance=7 page=0 chnl=0     
  9. char id=252 x=25 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0     
  10. char id=251 x=34 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0     
  11. char id=250 x=43 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0     
  12. char id=249 x=52 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0     
  13. char id=248 x=61 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0     
  14. char id=247 x=70 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0     
  15. char id=246 x=79 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0     
  16. char id=245 x=88 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0     
  17. char id=244 x=97 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0     
  18. char id=243 x=106 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0     
  19. char id=242 x=115 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0     
  20. char id=241 x=124 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0     
  21. char id=240 x=133 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0     
  22. char id=239 x=142 y=0 width=5 height=19 xoffset=0 yoffset=0 xadvance=3 page=0 chnl=0     
  23. …    
  24.    
  25. kernings count=374   
  26. kerning first=49 second=49 amount=-1   
  27. kerning first=121 second=44 amount=-1   
  28. kerning first=121 second=46 amount=-1   
  29. kerning first=119 second=44 amount=-1   
  30. kerning first=119 second=46 amount=-1   
  31. kerning first=118 second=44 amount=-1   
  32. kerning first=118 second=46 amount=-1   
  33. kerning first=114 second=44 amount=-1   
  34. kerning first=114 second=46 amount=-1   
  35. kerning first=89 second=44 amount=-2   
  36. kerning first=89 second=45 amount=-1   
  37. kerning first=89 second=46 amount=-2   
  38. kerning first=89 second=58 amount=-1   
  39. kerning first=89 second=59 amount=-1   
  40. kerning first=89 second=65 amount=-1   
  41. kerning first=89 second=97 amount=-1   
  42. kerning first=89 second=101 amount=-1   
  43.    
  44. …    

再看看arial-15.png:

可以很明顯看出,libgdx的文字繪制是根據(jù)fnt文件獲取對(duì)應(yīng)文字的在png中的坐標(biāo)位置,然后截取圖片的相應(yīng)部分進(jìn)行繪制。

那么要讓libgdx支持中文思路就很簡(jiǎn)單了,我們自己構(gòu)造fnt和png文件,其中包含我們要使用的中文即可。

作者給我們提供了一個(gè)對(duì)應(yīng)的工具:Hiero。

下載后雙擊運(yùn)行,在右側(cè)列表中選擇一個(gè)可以用的字體,然后輸入需要的中文,最好保留自動(dòng)生成的英文和符號(hào)。

在右側(cè)的Effects中可以設(shè)置效果:

點(diǎn)File—Save as BMFont Files,生成兩個(gè)文件,將它們拷貝到asserts文件夾。

使用

 
 
 
  1. bitmapFont = new BitmapFont(Gdx.files.internal("cf.fnt"), Gdx.files.internal("cf.png"), false);  

指定我們生成的文件作為繪制的參考,然后繪制:

 
 
 
  1. bitmapFont.draw(spriteBatch, "FPS" + Gdx.graphics.getFramesPerSecond(), 5, Gdx.graphics.getHeight() - 10);  
  2. bitmapFont.draw(spriteBatch, "祝大家光棍節(jié)快樂", 0, Gdx.graphics.getHeight()/2-8);    

程序運(yùn)行效果:

關(guān)于多行文字,可以調(diào)用

 
 
 
  1. public TextBounds drawMultiLine (SpriteBatch spriteBatch, CharSequence str, float x, float y)    

或者

 
 
 
  1. public TextBounds drawMultiLine (SpriteBatch spriteBatch, CharSequence str, float x, float y, float alignmentWidth,HAlignment alignment)

當(dāng)前名稱:Android游戲引擎libgdx使用教程3:繪制漢字與顯示中文
URL分享:http://www.dlmjj.cn/article/cdpgepo.html