Android Game Development Framework LIBGDX use (21)-Support Chinese with TTF font

Source: Internet
Author: User
Tags libgdx

TTF Font

TTF (Truetypefont) is a font file format co-launched by Apple and Microsoft, and, with the popularity of Windows, has become one of the most commonly used font file representations for a wide range of applications.

If you are a Windows operating system, you can find many fonts from the Fonts folder. can also be downloaded from the Internet.

I recommend Yahei.consolas font, look very cool, anyway, my eclipse is the use of this font.

Use of Gdx-setup-ui

To download version 0.96 from LIBGDX, you can see the file structure as follows:

Libgdx-chinese-1

Strongly run Gdx-setup-ui.jar, which is a project initialization tool.

The operating interface is as follows:

Libgdx-chinese-2

The name of the project, the name of the package and so on. In the middle of the required configuration, you can download the latest stable and nightly versions directly. It is recommended to use local accessories.

Libgdx-chinese-3

Without decompression, directly select the zip file, it will automatically extract the required files.

I use Eclipse, direct import can.

Libgdx-chinese-4

I've only built desktop projects, which can generally be built with Android and the Web.

Note that the default Android project corresponding to the SDK is 15 and can be changed as needed. Sometimes the imported items will have a red exclamation point, and a assets folder can be added.

Libgdx-chinese-5

One of the BACK-LIBGDX projects is our core, where all the logic and everything is realized here. Back-libgdx-xxx is the corresponding start-up.

Libgdx using TTF font

Prepare a TTF font to rename the Font.ttf copy to the assets folder. Locate the Gdx-stb-truetype-natives.jar and Gdx-stb-truetype.jar from the archive package. Where Gdx-stb-truetype-natives.jar is added to the desktop project, Gdx-stb-truetype.jar is added to the core project.

Libgdx-chinese-6

Now look at the most critical method of Truetypefontfactory.createbitmapfont.

We create a bitmapfont that supports the TTF font through it. It needs a filehandle and text to support. Of course, there are some parameters, the specific meaning can refer to the API documentation.

That means we need to use what text in the TTF font must be declared. But the problem is not, 3500 commonly used Chinese characters can meet the requirements. There is a commonly used 500 words on the internet, but that is not enough.

I'm here to write a few as a demonstration.



1 Public static final String font_characters = "The world's people love the good Chinese support to see again";


Then use initialize



123 Font = Truetypefontfactory.createbitmapfont (Gdx.files.internal ("Font.ttf"), Font_characters, 12.5f, 7.5f,1.0f, Gdx.graphics.getWidth (), Gdx.graphics.getHeight ());


The rest of the work is the same as using Hiero.



123456789101112131415161718192021222324252627282930313233343536373839 public class App implements Applicationlistener { Bitmapfont font; SpriteBatch Batch;public static final String font_characters = "The world's people love the good Chinese support to see again"; @Overridepublic void Create () { Font = Truetypefontfactory.createbitmapfont (Gdx.files.internal ("Font.ttf"), Font_characters, 12.5f, 7.5f, 1.0f, Gdx.graphics.getWidth (), Gdx.graphics.getHeight ());Font.setcolor (1f, 0f, 0f, 1f); Batch =new SpriteBatch ();}@Overridepublic void Dispose () { Font.dispose ();}@Overridepublic void render () { Gdx.gl.glClear (gl10.gl_color_buffer_bit); Batch.begin (); Font.draw (Batch,"Chinese support", ten, ten); Batch.end ();}@Overridepublic void Resize (int width, int height) { }@Overridepublic void Pause () { }@Overridepublic void Resume () { }}


The effect is as follows:

Libgdx-chinese-7

Using the TTF font principle in the stage is the same as the only Bitmapfont instantiation method that needs to be modified.

Written in the last

Using TTF font to solve LIBGDX display Chinese is undoubtedly a good choice, there is basically no text display. But the TTF font size is generally relatively large, such as I use the font is 13.8M. The smaller one also has 3 m.

This size is sometimes hard to accept for Android apps.

Android system comes with the font in/system/fonts/, you can use absolute path access ... But not recommended.

Using Herio is an efficient (program-efficient, not development-efficient) approach, but LIBGDX does not support multiple graphs of herio.

There are documents mentioning the use of spritesheetpacker, but I have not been found. Google code has been out of the way since it moved to GitHub, and I don't know if it's removed or what.

Android Game Development Framework LIBGDX use (21)-Support Chinese with TTF font

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.