android遊戲開發架構libgdx的使用(三)--中文顯示與漢字繪製

來源:互聯網
上載者:User

本來這篇想寫情境的,但是昨天和群裡一個朋友討論了一下libgdx顯示漢字的問題。以前沒有注意到這個問題,發現還是蠻嚴重的,要是不支援中文,libgdx用著就有點不愉快了。

我們來看看BitmapFont類,這是有關文字繪製的。看一下源碼:


public BitmapFont () {  

        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=15 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1common lineHeight=18 base=14 scaleW=256 scaleH=256 pages=1 packed=0page id=0 file="arial-15.png"chars count=189char id=32   x=0     y=0     width=0     height=0     xoffset=0     yoffset=14    xadvance=4     page=0  chnl=0 char id=255   x=0     y=0     width=8     height=19     xoffset=-1     yoffset=0    xadvance=7     page=0  chnl=0 char id=254   x=8     y=0     width=9     height=19     xoffset=1     yoffset=0    xadvance=8     page=0  chnl=0 char id=253   x=17     y=0     width=8     height=19     xoffset=-1     yoffset=0    xadvance=7     page=0  chnl=0 char id=252   x=25     y=0     width=9     height=19     xoffset=1     yoffset=0    xadvance=8     page=0  chnl=0 char id=251   x=34     y=0     width=9     height=19     xoffset=1     yoffset=0    xadvance=8     page=0  chnl=0 char id=250   x=43     y=0     width=9     height=19     xoffset=1     yoffset=0    xadvance=8     page=0  chnl=0 char id=249   x=52     y=0     width=9     height=19     xoffset=1     yoffset=0    xadvance=8     page=0  chnl=0 char id=248   x=61     y=0     width=9     height=19     xoffset=1     yoffset=0    xadvance=8     page=0  chnl=0 char id=247   x=70     y=0     width=9     height=19     xoffset=1     yoffset=0    xadvance=8     page=0  chnl=0 char id=246   x=79     y=0     width=9     height=19     xoffset=1     yoffset=0    xadvance=8     page=0  chnl=0 char id=245   x=88     y=0     width=9     height=19     xoffset=1     yoffset=0    xadvance=8     page=0  chnl=0 char id=244   x=97     y=0     width=9     height=19     xoffset=1     yoffset=0    xadvance=8     page=0  chnl=0 char id=243   x=106     y=0     width=9     height=19     xoffset=1     yoffset=0    xadvance=8     page=0  chnl=0 char id=242   x=115     y=0     width=9     height=19     xoffset=1     yoffset=0    xadvance=8     page=0  chnl=0 char id=241   x=124     y=0     width=9     height=19     xoffset=1     yoffset=0    xadvance=8     page=0  chnl=0 char id=240   x=133     y=0     width=9     height=19     xoffset=1     yoffset=0    xadvance=8     page=0  chnl=0 char id=239   x=142     y=0     width=5     height=19     xoffset=0     yoffset=0    xadvance=3     page=0  chnl=0 …kernings count=374kerning first=49  second=49  amount=-1kerning first=121  second=44  amount=-1kerning first=121  second=46  amount=-1kerning first=119  second=44  amount=-1kerning first=119  second=46  amount=-1kerning first=118  second=44  amount=-1kerning first=118  second=46  amount=-1kerning first=114  second=44  amount=-1kerning first=114  second=46  amount=-1kerning first=89  second=44  amount=-2kerning first=89  second=45  amount=-1kerning first=89  second=46  amount=-2kerning first=89  second=58  amount=-1kerning first=89  second=59  amount=-1kerning first=89  second=65  amount=-1kerning first=89  second=97  amount=-1kerning first=89  second=101  amount=-1…

 

再看看arial-15.png:

可以很明顯看出,libgdx的文字繪製是根據fnt檔案擷取對應文字的在png中的座標位置,然後截取圖片的相應部分進行繪製。

那麼要讓libgdx支援中文思路就很簡單了,我們自己構造fnt和png檔案,其中包含我們要使用的中文即可。

作者給我們提供了一個對應的工具:Hiero。

下載後雙擊運行,在右側列表中選擇一個可以用的字型,然後輸入需要的中文,最好保留自動產生的英文和符號。

在右側的Effects中可以設定效果:

點File—Save as BMFont Files,產生兩個檔案,將它們拷貝到asserts檔案夾。

使用


bitmapFont = new BitmapFont(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);

 

 效果:

關於多行文字:

可以調用


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

或者


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


作者:黃雲坤
出處:http://www.cnblogs.com/htynkn/

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.