Android系统中通过Typeface.java加载字体:
frameworks/base/graphics/java/android/graphics/Typeface.java
private static void init() {
// Load font config and initialize Minikin state
File systemFontConfigLocation = getSystemFontConfigLocation();
File configFilename = new File(systemFontConfigLocation, FONTS_CONFIG);
...
}
该类初始化时File configFilename = new File(systemFontConfigLocation, FONTS_CONFIG);的FONTS_CONFIG为字体的配置文件:
static Typeface sDefaultTypeface;
...
static final String FONTS_CONFIG = "fonts.xml";
该fonts.xml位于frameworks/base/data/fonts/fonts.xml
<?xml version="1.0" encoding="utf-8"?>
....
<family lang="zh-Hans">
<font weight="400" style="normal">NotoSansSC-Regular.otf</font>
</family>
<family lang="zh-Hant">
<font weight="400" style="normal">NotoSansTC-Regular.otf</font>
</family>
...
从该xml中发现zh-Hans字段代表中文简体的字体,zh-Hant字段代表中文繁体的字体,目前我们需要将其中的字体NotoSansSC-Regular.otf替换成想要的字体。
实例:将系统默认中文字体修改为宋体常规(simsun.ttc)。
1.把字体simsun.ttc文件copy到frameworks\base\data\fonts目录
2.修改frameworks/base/data/fonts/fonts.xml
<family lang="zh-Hans">
<font weight="400" style="normal">simsun.ttc</font>
</family>
2.修改frameworks\base\data\fonts\Android.mk
################################
# Build the rest of font files as prebuilt.
# $(1): The source file name in LOCAL_PATH.
# It also serves as the module name and the dest file name.
define build-one-font-module
$(eval include $(CLEAR_VARS))\
$(eval LOCAL_MODULE := $(1))\
$(eval LOCAL_SRC_FILES := $(1))\
$(eval LOCAL_MODULE_CLASS := ETC)\
$(eval LOCAL_MODULE_TAGS := optional)\
$(eval LOCAL_MODULE_PATH := $(TARGET_OUT)/fonts)\
$(eval include $(BUILD_PREBUILT))
endef
font_src_files := \
Clockopia.ttf \
AndroidClock.ttf \
AndroidClock_Highlight.ttf \
AndroidClock_Solid.ttf \
simsun.ttc
3.修改frameworks\base\data\fonts\fonts.mk文件
... ...
PRODUCT_PACKAGES := \
DroidSansFallback.ttf \
DroidSansMono.ttf \
Clockopia.ttf \
AndroidClock.ttf \
AndroidClock_Highlight.ttf \
AndroidClock_Solid.ttf \
simsun.ttc \
4..在frameworks/base/data/fonts目录下单编,执行mm,将生成到对应项目的simsun.ttc文件push到system/fonts下。
5.将修改后的frameworks/base/data/fonts/fonts.xml push 到system/etc目录下,重启设备,系统默认字体就被替换为宋体常规(simsun.ttc)了。