Android Font knowledge, androidfont

Source: Internet
Author: User
Tags skia

Android Font knowledge, androidfont

Android font overview the Android system supports three fonts by default: "sans", "serif", and "monospace ".

Android. graphic. typeface font class:
The constant static definition of this class, first for the font type (typeface) Name

TypefaceDEFAULT
Typeface DEFAULT_BOLD
Typeface MONOSPACE
TypefaceSANS_SERIF
Typeface SERIF

Font style name
IntBOLD
Int BOLD_ITALIC
Int ITALIC
Int NORMAL

To set the font of A TextView, you can use the setTypeface method in TextView to specify a Typeface object. Because the font class of Android is relatively simple, we will list all member methods:
StaticTypeface create (Typeface family, int style) // The static method. Parameter 1 is the font type. Here it is the static definition of Typeface, such as ,. Parameter 2 is the style, such as bold or italic.

StaticTypeface create (String familyName, int style) // static method. The parameter is a String of the name of the character body. The parameter 2 is the same as the style. The above method is recommended.

StaticTypeface createFromAsset (AssetManager mgr, String path) // static method. The parameter is an AssetManager object, which is used to retrieve the font from the assets folder of the APK, parameter 2 is the path of the plug-in font file relative to the assets folder in the Android project.

StaticTypeface createFromFile (File path) // a static method to construct a font from the File system. The parameter can be a font File in sdcard.

StaticTypeface createFromFile (String path) // static method to construct the font from the specified path

StaticTypeface defaultFromStyle (int style) // The default font style is returned.

IntgetStyle () // obtain the current font style

Finalboolean isBold () // determines whether it is currently in bold

Finalboolean isItalic () // determines whether the current style is italic.

How Android fonts work
The android font is implemented by the android 2D graphics engine skia, and the system font is loaded in the Preloading classes of Zygote.
Related Files include skTypeface. cpp and skFontHost_android.cpp. The latter is the port implemented by skia for the font of the android platform.


The main variables are:

Struct FontInitRec {

Const char * fFileName;

Const char * const * fNames; // null-terminated list

};

Struct FamilyRec {

FamilyRec * fNext;

SkTypeface * fFaces [5];

};

Uint32_t gFallbackFonts [SK_ARRAY_COUNT (gSystemFonts) + 1];

Load_system_fonts () @ skFontHost_android.cpp

Load all fonts in the system, assign a unique ID to each font, and divide the font into two types: FamilyFonts and FallbackFonts. skPaint uses the Typeface font set by the application) the corresponding ID is used to display characters.


Replace the default Chinese font of Android

In android, DroidSans is the default font and only contains Western characters. By default, applications call DroidSansFallback, which contains East Asian characters, when the characters to be displayed do not exist in the DroidSans font (for example, Chinese characters,
If no character is encoded, the system will go to DroidSansFallback to find the character,
The DroidSansFallback font is used to display the character. If the character corresponding to the encoding cannot be found, the character cannot be displayed on the screen.

To change the default Chinese font, follow these steps:

1. Rename youyuan. ttf as DroidSansFallback. ttf to overwrite the DroidSansFallback. ttf file under the frameworks/base/data/fonts directory in the Android source code.

2. recompile the Android system

3. Compile the SDK. In the generated SDK, the default Chinese font of android has been replaced with the child font. The disadvantage of this method is that the original Chinese font of the Android system is deleted.
Add a default font for the android system In the android system, the default Chinese font only has one type: DroidSansFallback. ttf. If you want to set the desired Chinese font in the android Application, In addition to introducing the font file through the assets directory in the application, you can also add the default font of android.


The procedure is as follows:

1. Add a font file under the frameworks/base/data/fonts directory, such as a Driod-kaishu.ttf;

2. Add the book in skia. The files to be modified mainly include skFontHost. cpp, skTypeface. cpp, and Typeface. java;

3. Add an API related to the font of the exercise book on the java layer. The files to be modified mainly include typeface. java and textview. java;

4. Compile the SDK

5. Import the newly generated sdk to eclipse. In eclipse, you can set the font you added by using setTypeface (Typeface. KAISHU) and android: typeface = ("kaishu ").

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.