https://my.oschina.net/wolfcs/blog/366228?fromerr=RPZf2zmJ android 5.0中字庫文件管理配置的變化
https://blog.csdn.net/a282255307/article/details/76870441 淺析Android字體加載原理 Leslie_Yu
https://www.jianshu.com/p/c5087d5a2268?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation Android8.1系統字體
一.Android api Typeface 字型
android.graphics.Typeface提供了3個API供應用程序創建特定字體:
1. Typeface createFromAsset(AssetManager mgr, String path);
2. Typeface createFromFile(File path);
3. Typeface createFromFile(String path);
以及如下3個API來創建系統字體:
1. Typeface create(String familyName, int style);
2. Typeface create(Typeface family, int style);
3. Typeface defaultFromStyle(int style);
1.ZK_RXXX_RK3288_ANDROID5.1\frameworks\base\graphics\java\android\graphics\Typeface.java
static Typeface[] sDefaults; private static final LongSparseArray<SparseArray<Typeface>> sTypefaceCache = new LongSparseArray<SparseArray<Typeface>>(3); static Typeface sDefaultTypeface; static Map<String, Typeface> sSystemFontMap; static FontFamily[] sFallbackFonts; static final String FONTS_CONFIG = "fonts.xml";
static代碼塊 初始化系統字體
static { init(); // Set up defaults and typefaces exposed in public API DEFAULT = create((String) null, 0); DEFAULT_BOLD = create((String) null, Typeface.BOLD); SANS_SERIF = create("sans-serif", 0); SERIF = create("serif", 0); MONOSPACE = create("monospace", 0); sDefaults = new Typeface[] { DEFAULT, DEFAULT_BOLD, create((String) null, Typeface.ITALIC), create((String) null, Typeface.BOLD_ITALIC), }; }
init
/* * (non-Javadoc) * * This should only be called once, from the static class initializer block. */ private static void init() { // Load font config and initialize Minikin state //獲取系統字體配置文件位置放置於system/etc目錄下 File systemFontConfigLocation = getSystemFontConfigLocation(); //獲取配置文件fonts.xml File configFilename = new File(systemFontConfigLocation, FONTS_CONFIG); //以下代碼是對fonts.xml的解析,即是對系統字體的解析 try { FileInputStream fontsIn = new FileInputStream(configFilename); FontListParser.Config fontConfig = FontListParser.parse(fontsIn); Map<String, ByteBuffer> bufferForPath = new HashMap<String, ByteBuffer>(); //用來承載fonts.xml中的每個family節點 List<FontFamily> familyList = new ArrayList<FontFamily>(); // Note that the default typeface is always present in the fallback list; // this is an enhancement from pre-Minikin behavior. //從每個family節點中解析字體樣式,這里解析系統默認字體 for (int i = 0; i < fontConfig.families.size(); i++) { FontListParser.Family f = fontConfig.families.get(i); if (i == 0 || f.name == null) { familyList.add(makeFamilyFromParsed(f, bufferForPath)); } } //系統默認字體集合 sFallbackFonts = familyList.toArray(new FontFamily[familyList.size()]); //設置默認系統字體 setDefault(Typeface.createFromFamilies(sFallbackFonts)); //這里加載系統字體,包括默認字體 Map<String, Typeface> systemFonts = new HashMap<String, Typeface>(); for (int i = 0; i < fontConfig.families.size(); i++) { Typeface typeface; FontListParser.Family f = fontConfig.families.get(i); if (f.name != null) { if (i == 0) { // The first entry is the default typeface; no sense in // duplicating the corresponding FontFamily. typeface = sDefaultTypeface; } else { //從每個family節點中解析字體 FontFamily fontFamily = makeFamilyFromParsed(f, bufferForPath); FontFamily[] families = { fontFamily }; typeface = Typeface.createFromFamiliesWithDefault(families); } //解析的字體添加到系統字體中 systemFonts.put(f.name, typeface); } } //通過權重別號解析字體,別名必須與字體對應 for (FontListParser.Alias alias : fontConfig.aliases) { Typeface base = systemFonts.get(alias.toName); Typeface newFace = base; int weight = alias.weight; if (weight != 400) { newFace = new Typeface(nativeCreateWeightAlias(base.native_instance, weight)); } systemFonts.put(alias.name, newFace); } //系統字體集合 sSystemFontMap = systemFonts; } catch (RuntimeException e) { Log.w(TAG, "Didn't create default family (most likely, non-Minikin build)", e); // TODO: normal in non-Minikin case, remove or make error when Minikin-only } catch (FileNotFoundException e) { Log.e(TAG, "Error opening " + configFilename, e); } catch (IOException e) { Log.e(TAG, "Error reading " + configFilename, e); } catch (XmlPullParserException e) { Log.e(TAG, "XML parse exception for " + configFilename, e); } }
二.fonts.xm<?xml version="1.0" encoding="utf-8"?><!-- NOTE: this is the newer (L) version of the system font configuration,
supporting richer weight selection. Some apps will expect the older version, so please keep system_fonts.xml and fallback_fonts.xml in sync with any changes, even though framework will only read this file. All fonts withohut names are added to the default list. Fonts are chosen based on a match: full BCP-47 language tag including script, then just language, and finally order (the first font containing the glyph). Order of appearance is also the tiebreaker for weight matching. This is the reason why the 900 weights of Roboto precede the 700 weights - we prefer the former when an 800 weight is requested. Since bold spans effectively add 300 to the weight, this ensures that 900 is the bold paired with the 500 weight, ensuring adequate contrast. --> <familyset version="22"> <!-- first font is default --> <family name="sans-serif"> <font weight="100" style="normal">Roboto-Thin.ttf</font> <font weight="100" style="italic">Roboto-ThinItalic.ttf</font> <font weight="300" style="normal">Roboto-Light.ttf</font> <font weight="300" style="italic">Roboto-LightItalic.ttf</font> <font weight="400" style="normal">Roboto-Regular.ttf</font> <font weight="400" style="italic">Roboto-Italic.ttf</font> <font weight="500" style="normal">Roboto-Medium.ttf</font> <font weight="500" style="italic">Roboto-MediumItalic.ttf</font> <font weight="900" style="normal">Roboto-Black.ttf</font> <font weight="900" style="italic">Roboto-BlackItalic.ttf</font> <font weight="700" style="normal">Roboto-Bold.ttf</font> <font weight="700" style="italic">Roboto-BoldItalic.ttf</font> </family> <!-- Note that aliases must come after the fonts they reference. --> <alias name="sans-serif-thin" to="sans-serif" weight="100" /> <alias name="sans-serif-light" to="sans-serif" weight="300" /> <alias name="sans-serif-medium" to="sans-serif" weight="500" /> <alias name="sans-serif-black" to="sans-serif" weight="900" /> <alias name="arial" to="sans-serif" /> <alias name="helvetica" to="sans-serif" /> <alias name="tahoma" to="sans-serif" /> <alias name="verdana" to="sans-serif" /> <family name="sans-serif-condensed"> <font weight="300" style="normal">RobotoCondensed-Light.ttf</font> <font weight="300" style="italic">RobotoCondensed-LightItalic.ttf</font> <font weight="400" style="normal">RobotoCondensed-Regular.ttf</font> <font weight="400" style="italic">RobotoCondensed-Italic.ttf</font> <font weight="700" style="normal">RobotoCondensed-Bold.ttf</font> <font weight="700" style="italic">RobotoCondensed-BoldItalic.ttf</font> </family> <alias name="sans-serif-condensed-light" to="sans-serif-condensed" weight="300" /> <family name="serif"> <font weight="400" style="normal">NotoSerif-Regular.ttf</font> <font weight="700" style="normal">NotoSerif-Bold.ttf</font> <font weight="400" style="italic">NotoSerif-Italic.ttf</font> <font weight="700" style="italic">NotoSerif-BoldItalic.ttf</font> </family> <alias name="times" to="serif" /> <alias name="times new roman" to="serif" /> <alias name="palatino" to="serif" /> <alias name="georgia" to="serif" /> <alias name="baskerville" to="serif" /> <alias name="goudy" to="serif" /> <alias name="fantasy" to="serif" /> <alias name="ITC Stone Serif" to="serif" /> <family name="monospace"> <font weight="400" style="normal">DroidSansMono.ttf</font> </family> <alias name="sans-serif-monospace" to="monospace" /> <alias name="monaco" to="monospace" /> <family name="serif-monospace"> <font weight="400" style="normal">CutiveMono.ttf</font> </family> <alias name="courier" to="serif-monospace" /> <alias name="courier new" to="serif-monospace" /> <family name="casual"> <font weight="400" style="normal">ComingSoon.ttf</font> </family> <family name="cursive"> <font weight="400" style="normal">DancingScript-Regular.ttf</font> <font weight="700" style="normal">DancingScript-Bold.ttf</font> </family> <family name="sans-serif-smallcaps"> <font weight="400" style="normal">CarroisGothicSC-Regular.ttf</font> </family> ……………………………………………………………………………………………………………………………………………
<family> <font weight="400" style="normal">NanumGothic.ttf</font> </family> <family> <font weight="400" style="normal">NotoSansSymbols-Regular-Subsetted.ttf</font> </family> <family> <font weight="400" style="normal">NotoColorEmoji.ttf</font> </family> <family> <font weight="400" style="normal">DroidSansFallback.ttf</font> </family> <family lang="ja"> <font weight="400" style="normal">MTLmr3m.ttf</font> </family> <!-- Noto Sans Tai Le is intentionally kept last, to make sure it doesn't override the East Asian punctuation for Chinese. --> <family> <font weight="400" style="normal">NotoSansTaiLe-Regular.ttf</font> </family> </familyset>
開頭注釋:暫時為保持兼容性 會保留老的字庫配置文件 system_fonts.xml和fallback_fonts.xml 。並且要保持system_fonts.xml和fallback_fonts.xml同步。
對於任何更改,即使框架將只讀取此文件。所有有名稱的字體都添加到默認列表中。
三.Android5.1 修改系統默認字體,系統默認字體樣式
diff --git a/frameworks/base/data/fonts/fonts.xml b/frameworks/base/data/fonts/fonts.xml old mode 100644 new mode 100755 index 37527e9..892a982 --- a/frameworks/base/data/fonts/fonts.xml +++ b/frameworks/base/data/fonts/fonts.xml @@ -16,13 +16,25 @@ paired with the 500 weight, ensuring adequate contrast. --> <familyset version="22"> +<family lang="zh-Hans"> + <font weight="400" style="normal">hifont.ttf</font> + </family> + <family lang="zh-Hant"> + <font weight="400" style="normal">hifont.ttf</font> + </family> + <family lang="ja"> + <font weight="400" style="normal">hifont.ttf</font> + </family> + <family lang="ko"> + <font weight="400" style="normal">hifont.ttf</font> + </family> <!-- first font is default --> <family name="sans-serif"> <font weight="100" style="normal">Roboto-Thin.ttf</font> <font weight="100" style="italic">Roboto-ThinItalic.ttf</font> <font weight="300" style="normal">Roboto-Light.ttf</font> <font weight="300" style="italic">Roboto-LightItalic.ttf</font> - <font weight="400" style="normal">Roboto-Regular.ttf</font> + <font weight="400" style="normal">Roboto-Black.ttf</font> <font weight="400" style="italic">Roboto-Italic.ttf</font> <font weight="500" style="normal">Roboto-Medium.ttf</font> <font weight="500" style="italic">Roboto-MediumItalic.ttf</font>