Android中添加思源字体/NotoSansCJK/SourceHanSans

系统版本:Android 4.2.2_r1

本文主要是在Android中添加思源字体的过程记录。思源字体是Google和Adobe在2014.07.18发布的中文字体。

1.获取思源字体(Google:Noto Sans CJK; Adobe:Source Han Sans)。

2.解压后如下几个才是思源字体。

  • NotoSansHans-Regular.otf    常规
  • NotoSansHans-Black.otf      黑体
  • NotoSansHans-DemiLight.otf
  • NotoSansHans-Medium.otf
  • NotoSansHans-Thin.otf
  • NotoSansHans-Bold.otf        加粗
  • NotoSansHans-Light.otf

注:这里说下otf,意为opentype和ttf(truetype)是兼容的。

3.安装体验

由于Android中只使用到常规和加粗体,这里也只以这两个做实验(同时也有容量的考虑),在高PPI(分辨率)的屏幕上或许只是感觉到字形变了,在低分辨率的屏幕上变的更加清晰了,前后对比:

之前:

之后:

(上传到这里就不清晰了,真是怪,点击链接看原图更为清晰)

  1) 将NotoSansHans-Regular.otf NotoSansHans-Bold.otf放到/system/fonts/目录下。

  2) 将如下fallback_fonts.xml放到/vendor/etc/下。

<?xml version="1.0" encoding="utf-8"?>

<!--

Vendor-provided fallback fonts

This file can be edited to add references to fonts that are not installed or referenced in the

default system. The file should then be placed in /vendor/etc/fallback_fonts.xml. Note

that in your makefile, this directory should be referenced as $(TARGET_COPY_OUT_VENDOR)/etc/:

PRODUCT_COPY_FILES += \

frameworks/base/data/fonts/vendor_fonts.xml:$(TARGET_COPY_OUT_VENDOR)/etc/fallback_fonts.xml

For example, vendors might want to build configurations for locales that are

better served by fonts which either handle glyphs not supported in the default fonts or which

handle these glyphs differently than the default fallback fonts.

Each entry in this list is a "family", which consists of a list of "files"

(the filenames for that family). The files objects are

provided in the order of the styles supported for that family: regular, bold, italic, and

bold-italic. Only providing one font means that all styles will be rendered with that font.

Providing two means that these two fonts will render regular and bold fonts (italics will

be mapped to these two fonts).

There is also an optional "order" attribute on the Family tag. This specifies the index at

which that family of fonts should be inserted in the fallback font list, where the

default fallback fonts on the system (in /system/etc/fallback_fonts.xml) start at index 0.

If no ‘order‘ attribute is supplied, that family will be inserted either at the end of the

current fallback list (if no order was supplied for any previous family in this file) or

after the previous family (if there was an order specified previously). Typically, vendors

may want to supply an order for the first family that puts this set of fonts at the appropriate

place in the overall fallback fonts. The order of this list determines which fallback font

will be used to support any glyphs that are not handled by the default system fonts.

Han languages (Chinese, Japanese, and Korean) share a common range of unicode characters;

their ordering in the fallback or vendor files gives priority to the first in the list.

Language-specific ordering can be configured by adding a BCP 47-style "lang" attribute to

a "file" element; fonts matching the language of text being drawn will be prioritised over

all others.

The sample configuration below is an example of how one might provide two families of fonts

that get inserted at the first and second (0  and 1) position in the overall fallback fonts.

See /system/etc/system_fonts.xml and /system/etc/fallback_fonts.xml for more information

and to understand the order in which the default system fonts are loaded and structured for

lookup.

-->

<!-- Sample fallback font additions to the default fallback list. These fonts will be added

to the top two positions of the fallback list, since the first has an order of 0. -->

<familyset>

<family order="0">

<fileset>

<file>NotoSansHans-Regular.otf</file>

<file>NotoSansHans-Bold.otf</file>

</fileset>

</family>

<!-- Italic 等等会造成启动不了的问题,所以只添加前两个足矣。

<family>

<fileset>

<file>NotoSansHans-Regular.otf</file>

<file>NotoSansHans-Italic.otf</file>

<file>NotoSansHans-Bolditalic.otf</file>

<file>NotoSansHans-Black.otf</file>

<file>NotoSansHans-DemiLight.otf</file>

<file>NotoSansHans-Light.otf</file>

<file>NotoSansHans-Medium.otf</file>

<file>NotoSansHans-Thin.otf</file>

</fileset>

</family>

-->

</familyset>

同时也有做好的字库加xml在这里下载,还原的时将这三个文件删除即可。

4.以源码方式添加

如果需要在制作rom时就可以自动添加默认中文字体,思路和上边的类似,以下是git log信息。

commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Author: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Date:   Mon Aug 18 15:44:24 2014 +0800

Add Noto Sans CJK fonts for Chinese

diff --git a/device/rockchip/rk30sdk/device.mk b/device/rockchip/rk30sdk/device.mk

index 8befd40..b4e2822 100755

--- a/device/rockchip/rk30sdk/device.mk

+++ b/device/rockchip/rk30sdk/device.mk

@@ -119,6 +119,10 @@ PRODUCT_COPY_FILES += \

+#fonts

+PRODUCT_COPY_FILES += \

+    frameworks/base/data/fonts/vendor_fonts.xml:$(TARGET_COPY_OUT_VENDOR)/etc/fallback_fonts.xml

diff --git a/frameworks/base/data/fonts/Android.mk b/frameworks/base/data/fonts/Android.mk

index e02e95a..0b4bc6c 100755

--- a/frameworks/base/data/fonts/Android.mk

+++ b/frameworks/base/data/fonts/Android.mk

@@ -157,6 +157,8 @@ font_src_files += \

DroidSansHebrew-Regular.ttf \

DroidSansHebrew-Bold.ttf \

DroidSansThai.ttf \

+    NotoSansHans-Regular.otf \

+    NotoSansHans-Bold.otf \

DroidSansArmenian.ttf \

DroidSansGeorgian.ttf \

AndroidEmoji.ttf

diff --git a/frameworks/base/data/fonts/NotoSansHans-Bold.otf b/frameworks/base/data/fonts/NotoSansHans-Bold.otf

new file mode 100644

index 0000000..006372e

Binary files /dev/null and b/frameworks/base/data/fonts/NotoSansHans-Bold.otf differ

diff --git a/frameworks/base/data/fonts/NotoSansHans-Regular.otf b/frameworks/base/data/fonts/NotoSansHans-Regular.otf

new file mode 100644

index 0000000..923f89c

Binary files /dev/null and b/frameworks/base/data/fonts/NotoSansHans-Regular.otf differ

diff --git a/frameworks/base/data/fonts/fonts.mk b/frameworks/base/data/fonts/fonts.mk

index 875795a..490c6e6 100755

--- a/frameworks/base/data/fonts/fonts.mk

+++ b/frameworks/base/data/fonts/fonts.mk

@@ -38,6 +38,8 @@ PRODUCT_PACKAGES := \

DroidSansHebrew-Regular.ttf \

DroidSansHebrew-Bold.ttf \

DroidSansThai.ttf \

+    NotoSansHans-Regular.otf \

+    NotoSansHans-Bold.otf \

DroidSerif-Regular.ttf \

DroidSerif-Bold.ttf \

DroidSerif-Italic.ttf \

diff --git a/frameworks/base/data/fonts/vendor_fonts.xml b/frameworks/base/data/fonts/vendor_fonts.xml

index 8690ee1..fba0c88 100755

--- a/frameworks/base/data/fonts/vendor_fonts.xml

+++ b/frameworks/base/data/fonts/vendor_fonts.xml

@@ -45,17 +45,26 @@

<!-- Sample fallback font additions to the default fallback list. These fonts will be added

to the top two positions of the fallback list, since the first has an order of 0. -->

-<!--

<familyset>

<family order="0">

<fileset>

-            <file>MyFont.ttf</file>

+            <file>NotoSansHans-Regular.otf</file>

+            <file>NotoSansHans-Bold.otf</file>

</fileset>

</family>

+

+<!-- Italic 等等会造成启动不了的问题,所以只添加前两个足矣。

<family>

<fileset>

-            <file>MyOtherFont.ttf</file>

+            <file>NotoSansHans-Regular.otf</file>

+            <file>NotoSansHans-Italic.otf</file>

+            <file>NotoSansHans-Bolditalic.otf</file>

+            <file>NotoSansHans-Black.otf</file>

+            <file>NotoSansHans-DemiLight.otf</file>

+            <file>NotoSansHans-Light.otf</file>

+            <file>NotoSansHans-Medium.otf</file>

+            <file>NotoSansHans-Thin.otf</file>

</fileset>

</family>

+-->

</familyset>

---->

需要强调一点,关于字体拷贝,经过测试发现需要同时修改Android.mk和fonts.mk才可以完成拷贝。Google是以Apache License, version 2.0协议发布的,所以大可不必担心会有律师函的问题,在高版本的Android中这套字体已经是内置到Android中了。DroidSansFallback.ttf并不是完全指中文字体,由于之前并没有开发出专心的中文字体,目前处理方法是把所以暂时不规范的字体都放入了DroidSansFallback.ttf中,Fallback是备胎的意思,所有的胎都爆了才会使用这个。

参考:《关于Android的字体》。低版本的Android参考《Android 如何增加新的字库》 <完>

Android中添加思源字体/NotoSansCJK/SourceHanSans,布布扣,bubuko.com

时间: 2024-12-24 19:05:10

Android中添加思源字体/NotoSansCJK/SourceHanSans的相关文章

Android中加入思源字体/NotoSansCJK/SourceHanSans

系统版本号:Android 4.2.2_r1 本文主要是在Android中加入思源字体的过程记录. 思源字体是Google和Adobe在2014.07.18公布的中文字体. 1.获取思源字体(Google:Noto Sans CJK; Adobe:Source Han Sans). 2.解压后例如以下几个才是思源字体. NotoSansHans-Regular.otf    常规 NotoSansHans-Black.otf      黑体 NotoSansHans-DemiLight.otf

Android中通过typeface设置字体

Android系统默认支持三种字体,分别为:"sans", "serif", "monospace",除此之外还可以使用其他字体文件(*.ttf) 方法一:XML中使用android默认字体 <!--  使用默认的sans字体-->         <TextView    Android:id="@+id/sans"                    Android:text="Hello,W

如何在iOS开发中添加新的字体

iOS 开发默认字体是 Heiti SC ,即我们常用的「黑体-简」 用过一段时间后,就会发现字体过于单调了 ,那么怎么在iOS开发中添加新的字体呢 ,这里教你两种方法. 1.使用系统自带的字体 搜索fonts,不难发现,系统中是有不少字体的,而这些字体也是都是可以使用的. 这些字体只用时可以通过代码来设置,也可以直接来设置,方法很简单: 将text的属性设置为Attributed,再选择自己想要的系统字体就OK啦 2.使用自己从网上下载的字体 首先从网上下载自己喜欢的字体,将下载好的字体拖入当

android中添加背景音乐

方法一:这是使用java中的多线程,另外new出一个类,用类来启动音乐. 这个方法,就像当初写java的小游戏一样,不过是在电脑上运行的,可以控制每一个动作,比如你的触碰动作,但是,在我这个游戏中,我需要的不是一点的音乐,虽               然后期会用到,而是一开始就有的. 不过,用这个方法,没用service,就是开起来的时候,会很卡,而且会闪退. 1 package com.example.flybird_anla; 2 3 4 import java.util.HashMap;

Android中快速实现自定义字体!

前言:我们都知道,Android中默认的字体是黑体,而大多数app也都是使用的这种字体,但我们发现,大多数app中,个别地方字体非常好看,例如app的标题栏,菜单栏等地方,那他们是怎么做到的呢?有两种方式,第一是图片来代替文字,第二,就是今天我要教大家的自定义字体. 开发环境: Android Studio 2.2.2 compileSdkVersion 25 buildToolsVersion "25.0.0" minSdkVersion 19 targetSdkVersion 25

Flutter使用思源字体

1.找到字体ttf https://github.com/Pal3love/Source-Han-TrueType 2.导入项目   fonts: - family: NotoSerif   fonts:   - asset: assets/fonts/NotoSerif-Regular.ttf   - asset: assets/fonts/SourceHanSansCN-Light.ttf 3.使用 import 'package:flutter/material.dart'; import

Android中添加和识别手势操作

实现思路:将手势写入GestureLibrary中,对其他手势进行匹配对比,进行识别 先在布局中加入GestureOverlayView组件 <android.gesture.GestureOverlayView android:id="@+id/add_gesture" android:layout_width="match_parent" android:layout_height="match_parent" android:layo

Android中添加水平线和垂直线

1.添加水平线 <View android:layout_height="0.5dip" android:background="#686868" android:layout_width="match_parent"/> 2.添加垂直线 <View android:layout_height="match_parent" android:background="#686868" andr

Android中添加竖线和横线的方法

竖线 <View       android:layout_width="1dip"     android:layout_height="match_parent"     /> 横线 <View  android:layout_height="1px"            android:layout_width="match_parent" />