Use the android game engine angle development interface to display Chinese Characters

Source: Internet
Author: User

Angle does not support the display of Chinese characters. anglefont in the inner is not a problem. Anglefont loads the font and prepares the texture by converting the character into bitmap. For Chinese character libraries, this process takes a long time, loading all of them and occupying a lot of memory. If some of them are loaded and updated based on the Usage frequency, the algorithm is too troublesome.

I am using a stupid method. I first use the android canvas to draw Chinese characters into the memory image, and then generate an anglesprite (including the texture associated with anglespritelayout and anglespritelayout) based on the image ). The following is a class for converting Chinese characters to bitmap. It supports single-line text painting and can be omitted as needed.

Public final class stringtobitmap {public static bitmap getbitmapfromsinglelinestring (string text, int textcolor, int backgroundcolor, string fonttype, int fontsize, int bitmapwidth, int bitmapheight, paint. align align) {Bitmap bitmap = bitmap. createbitmap (bitmapwidth, bitmapheight, bitmap. config. argb_8888); canvas = new canvas (Bitmap); // specifies the background color canvas. drawcolor (backgroundcolor); textpaint paint = new textpaint (); typeface = typeface. create (fonttype, typeface. normal); // eliminate the Sawtooth paint. setantialias (true); // The font is red paint. setcolor (textcolor); paint. settypeface (typeface); paint. settextsize (fontsize); paint. settextalign (align); charsequence truncatetext = textutils. ellipsize (text, paint, bitmapwidth, textutils. truncateat. end); // draw the font canvas. drawtext (truncatetext, 0, truncatetext. length (), 0, bitmapheight-1, paint); Return bitmap ;}}

Only the above processing is not enough. a bitmap is generated and needs to be converted into a texture.

Angletextureengine in Angle class library can only load textures from resouce. Step 1: I modified the angletextureengine class and added the following method to generate texture objects from an existing bitmap.

public AngleTexture createTextureFromBitmap(Bitmap bitmap){AngleTexture tex = null;Iterator<AngleTexture> it = mTexturesX.iterator();while (it.hasNext()){tex = it.next();if (tex instanceof AngleBitmapTexture){// Texture already existsif (((AngleBitmapTexture) tex).mBitmap == bitmap){tex.mRefernces++;return tex;}}}tex = new AngleBitmapTexture(this, bitmap);mTexturesX.add(tex);return tex;}

Step 2: Modify the anglespritelayout class to support creating textures from Bitmap. Add the following two methods (cropwidth and cropheight are not specified, and only the width and height of the texture are specified ).

public AngleSpriteLayout(AngleSurfaceView view, Bitmap bitmap){doInit(view, 0, 0, bitmap);}public AngleSpriteLayout(AngleSurfaceView view, int width, int height, Bitmap bitmap){doInit(view, width, height, bitmap);}

The above two methods call the doinit function. For more information, see the original doinit function of the anglespritelayout class.

Step 3: implement a texture class anglebitmaptexture. The Code is as follows:

public class AngleBitmapTexture extends AngleTexture {protected Bitmap mBitmap;public AngleBitmapTexture(AngleTextureEngine textureEngine, Bitmap bmp) {super(textureEngine);mBitmap = bmp;}@Overridepublic Bitmap create() {return mBitmap;}void setBitmap(Bitmap bitmap){mBitmap = bitmap;mTextureEngine.releaseHWTexture(this);}}

Now, the preparation is complete. Let's see how to use it. It is actually very simple. For example:

Long T = system. currenttimemillis (); angleobject textholder = mglsurfaceview. addobject (New angleobject (); int x = 120; int y = 100; for (INT I = 0; I <30; I ++) {if (I % 6 = 0) {Y + = 60; X = 120;} anglesprite textsprite = new anglesprite (X, Y, new anglespritelayout (mglsurfaceview, 190, 50, stringtobitmap. getbitmapfromsinglelinestring ("rendering test" + I, 0 xffeeeeee, 0x00000000, "", 28,190, 50, paint. align. left); textholder. addobject (textsprite); X ++ = 200;} log. V ("stringbitmap", "create 30 string bitmap, use:" + (system. currenttimemillis ()-t ));

OK, so far, the Chinese characters can be displayed normally, that is, the performance is unknown. In the above code, I added some test code, and the Creation Time was not too long. On my device, I created 30 strings of Arts and Sciences, about 150 milliseconds.

It seems that we should first analyze the angle engine framework, but there are not many codes and classes, so it is easy to understand. Reading the code will not take much time, and I will try again later. This may also be one of the reasons why the engine has no documentation. The author thinks that code can describe most of the things and does not need them.

Related Article

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.