Because Libgdx is implemented using OpenGL at the underlying layer, Libgdx can support Chinese characters. In libgdx, Chinese characters are displayed by textures. BitmapFont and SpriteBatch are used to draw texts, when constructing BitmapFont, A fnt file consisting of the description text and a png file that provides the text image are required. For this reason, the document reads and displays the corresponding. PNG file containing Chinese information.
1. How to generate these two files?
First, we want to download the Hiero tool: https://code.google.com/p/libgdx/downloads/list
After the download is complete, double-click the interface to open the interface. You can make special effects on the text, change the background color, set the inner spacing, and so on. On the right side, click the font effects provided to us, you can Add the Add button to view the text I wrote:
650) this. length = 650; "src =" http://img1.51cto.com/attachment/201307/110251284.png "title =" QQ20130724110216.png "width =" 600 "height =" 451 "border =" 0 "hspace =" 0 "vspace =" 0 "style =" width: 600px; height: 451px; "/>
Finally, click File and save. We can see that two files are generated!
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201307/110430761.png "title =" QQ20130724110415.png "/>
2. How to use these two files and display them?
A. Put these two files in the Asset folder of the project, and then call them in the code.
Why are these two files used? Because Libgdx is implemented using OpenGL at the underlying layer, Libgdx supports Chinese. However, the Chinese characters in libgdx are displayed through textures. We use EditPlus to open Potato. the fnt file clearly shows that the libgdx text is based on the fnt file to obtain the Coordinate Position of the corresponding text in png, and then extract the corresponding part of the image for painting .)
650) this. length = 650; "src =" http://img1.51cto.com/attachment/201307/111219677.png "title =" QQ20130724111206.png "width =" 600 "height =" 166 "border =" 0 "hspace =" 0 "vspace =" 0 "style =" width: 600px; height: 166px; "/>
B. Display in code call
Package com. zhf. android_libgdx; import com. badlogic. gdx. backends. android. androidApplication; import android. OS. bundle; public class MainActivity extends AndroidApplication {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // start the game here. FirstGame implements the ApplicationListener class initialize (new FirstGame (), false); // specifying the configuration for the GLSurfaceView . // If the second parameter is set to true, opengl 2.0 is used when opengl 2.0 is available. }}
Main Interface:
Package com. zhf. android_libgdx; import javax. microedition. khronos. opengles. GL10; import com. badlogic. gdx. applicationListener; import com. badlogic. gdx. gdx; import com. badlogic. gdx. graphics. g2d. bitmapFont; import com. badlogic. gdx. graphics. g2d. spriteBatch; public class FirstGame implements ApplicationListener {private SpriteBatch batch; // SpriteBatch private BitmapFont bitmapFont; public void create () {// Instantiate batch = new SpriteBatch (); bitmapFont = new BitmapFont (Gdx. files. internal ("photo. fnt "), Gdx. files. internal ("photo.png"), false); bitmapFont. setColor (0.5f, 0.5f, 0.5f, 1); // sets the color bitmapFont. setScale (1.0f); // set the font ratio} public void render () {// STUB Gdx. gl. glClear (GL10.GL _ COLOR_BUFFER_BIT); Gdx. gl. glClearColor (0f, 0f, 0f, 0f); batch. begin (); bitmapFont. draw (batch, "Wild snails", 200,160); // display Specific characters // bitmapFont. draw (batch, "Wild snails", 100,200, 3, 5); // only show snails. // a line cannot be completely written. We only need to add the linefeed \ n to the Chinese characters) it is very simple and automatically recognized. BitmapFont. drawMultiLine (batch, "technical path, \ n step by step! ", Gdx. graphics. getWidth ()/5, Gdx. graphics. getHeight () * 0.8f); batch. end (); // display specific characters: You can call public TextBounds draw (SpriteBatch spriteBatch, CharSequence str, float x, float y) // The first parameter is an object of the SpriteBatch class, I will not explain it here. // The second parameter is a string and the Chinese character you want to output. For example, the "Snail guard" We just wrote is the second character. // The third and fourth parameters are float x and float y). They are the starting coordinate of the Chinese character to be drawn, and follow the starting point in the lower left corner of the Normal coordinate system ). // The Fifth and Sixth parameters are output starting from the nth character and ending with the nth character, for example, if we want to output the word "Snail", we will change start to 3 and end to 5. The first character is encoded as 0 rather than 1 by default. Please remember. // Or public TextBounds drawMultiLine (SpriteBatch spriteBatch, CharSequence str, float x, float y, float alignmentWidth, HAlignment alignment) // The following two parameters are described: float alignmentWidth ---- refers to the alignment mode of the text, which can be LEFT, RIGHT, or CENTER. Here, the LEFT draw origin is the upper LEFT corner, the RIGHT draw origin is from the middle, the CENTER of the draw origin is the CENTER of each line of text, expand to both sides. // HAlignment alignment ---- "does not work for LEFT. It only works for RIGHT and CENTER. For RIGHT, it is equivalent to translating the draw origin to the width of alignmentWidth, for the CENTER, it is equivalent to the width of the base point translation alignmentWidth/2.} Public void resize (int width, int height) {// STUB} public void pause () {// STUB} public void resume () {// STUB} public void dispose () {// STUB batch. dispose (); bitmapFont. dispose ();}}
C .:
650) this. length = 650; "src =" http://img1.51cto.com/attachment/201307/112505199.png "title =" device-2013-07-24-111754.png "width =" 300 "height =" 500 "border =" 0 "hspace =" 0 "vspace =" 0 "style =" width: 300px; height: 500px; "/>
OK! Done! The Chinese text is displayed. You can make your favorite text based on your situation.
Source code download: http://down.51cto.com/data/881831
This article from the "crazy Snail" blog, please be sure to keep this source http://smallwoniu.blog.51cto.com/3911954/1256054