Android字体简介
发布日期:2021-11-16 18:19:13 浏览次数:1 分类:技术文章

本文共 6101 字,大约阅读时间需要 20 分钟。

From: http://blog.csdn.net/cuiyan0214/article/details/6524877

Android字体简介

Android系统默认支持三种字体,分别为:“sans”,“serif”,“monospace”。

android.graphic.typeface字体类:

本类的常量静态定义,首先为字体类型(typeface)名称

TypefaceDEFAULT
Typeface DEFAULT_BOLD
Typeface MONOSPACE
TypefaceSANS_SERIF
Typeface SERIF
字体风格(style)名称
intBOLD
int BOLD_ITALIC
int ITALIC
int NORMAL

设置TextView的字体可以通过TextView中的setTypeface方法来指定一个Typeface对象,因为Android的字体类

比较简单,我们列出所有成员方法:

  • staticTypeface create(Typeface family, int style)//静态方法,参数一为字体类型这里是Typeface的静态定义,如宋体,参数二风格,如粗体,斜体

  • staticTypeface create(String familyName, int style)//静态方法,参数一为字体名的字符串,参数二为风格同上,这里我们推荐使用上面的方法。

  • staticTypeface createFromAsset(AssetManager mgr, String path)//静态方法,参数一为AssetManager对象,主要用于从APK的assets文件夹中取出字体,参数二为相对于Android工程下的assets文件夹中的外挂字体文件的路径。

  • staticTypeface createFromFile(File path)//静态方法,从文件系统构造一个字体,这里参数可以是sdcard中的某个字体文件

  • staticTypeface createFromFile(String path) //静态方法,从指定路径中构造字体

  • staticTypeface defaultFromStyle(int style) //静态方法,返回默认的字体风格

  • intgetStyle() //获取当前字体风格

  • finalboolean isBold() //判断当前是否为粗体

  • finalboolean isItalic() //判断当前风格是否为斜体

例程:

1

2

Android字体工作原理

android字体由android 2D图形引擎skia实现,并在Zygote的Preloading classes中对系统字体进行load。相关文件有:skTypeface.cpp和skFontHost_android.cpp,其中后者是skia针对android平台字体实现的port。主要的变量有: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系统中所有的字体并给每种字体分配唯一的ID,并将字体分为两种:FamilyFonts和FallbackFonts,skPaint通过应用程序设置的字体(Typeface)所对应的ID最终实现字符的显示。

替换Android默认的汉字字体

在android系统中,DroidSans是默认字体,只包含西方字符,应用程序默认情况下都会调用它,而DroidSansFallback包含了东亚字符,当需要显示的字符在DroidSans字体中不存在(如:汉字)时,即没有对应编码的字符时,系统会到DroidSansFallback中去找相应编码的字符,如果找到,则使用DroidSansFallback字体来显示它,如果仍找不到该编码对应的字符,则无法在屏幕上显示该字符。更换默认中文字体的步骤为:

1)将幼圆字体库youyuan.ttf重命名为DroidSansFallback.ttf,覆盖Android源码中frameworks/base/data/fonts目录下的DroidSansFallback.ttf文件;

2)重新编译Android系统;

3)编译SDK。生成的SDK中,android默认的中文字体已更换为幼圆字体。该方法的不足是删除了Android系统原来的中文字体。

为android系统添加一种默认字体,类似“sans”,“serif”,“monospace”

在android系统中,默认的中文字体只有一种:DroidSansFallback.ttf,如果想在android应用程序中随意设置想要的中文字体,除了在应用程序中通过assets目录引入字体文件外,还可以通过增加android默认字体的方式来实现。添加步骤大致如下:1)在frameworks/base/data/fonts目录下添加字体文件,例如Driod-kaishu.ttf;2)在skia中增加楷书这一字体,需要修改的文件主要有skFontHost.cpp、skTypeface.cpp、Typeface.java等;3)在java层添加楷书字体相关API,需要修改的文件主要有typeface.java和textview.java;4)编译SDK5)将新生成的sdk导入eclipse,在eclipse中即可通过setTypeface(Typeface.KAISHU)和android:typeface=(“kaishu”)两种方式设置自己添加的字体。以增加楷书字体为例:==========main.xml=====================

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#f3243646"

>

<TextView

android:id="@+id/TextView1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/normal"

android:typeface="normal"

android:textSize="25px"

/>

<TextView

android:id="@+id/TextView2"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/sans"

android:typeface="sans"

android:textSize="25px"

/>

<TextView

android:id="@+id/TextView3"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/serif"

android:typeface="serif"

android:textSize="25px"

/>

<TextView

android:id="@+id/TextView4"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/monospace"

android:typeface="monospace"

android:textSize="25px"

/>

<TextView

android:id="@+id/TextView5"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/kaishu"

android:typeface="kaishu"

android:textSize="25px"

/>

</LinearLayout>

==============String.xml=====================

<?xmlversion="1.0"encoding="utf-8"?>

<resources>

<stringname="normal">(normal)字体测试!!!TypefacesTest!!!</string>

<stringname="app_name">Typefaces4</string>

<stringname="kaishu">(kaishu)字体测试!!!TypefacesTest!!!</string>

<stringname="sans">(sans)字体测试!!!TypefacesTest!!!</string>

<stringname="serif">(serif)字体测试!!!TypefacesTest!!!</string>

<stringname="monospace">(monospace)字体测试!!!TypefacesTest!!!</string>

</resources>

==============Typefaces4.java=====================

packagecom.cy.Typefaces4;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.widget.TextView;

publicclassTypefaces4 extendsActivity{

/**Called when the activity is first created. */

@Override

publicvoidonCreate(BundlesavedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

}

结果图如下:

需要注意的是,在一个TextView里的所有字符只能设置成一种字体,比如说textview.settext(“字体测试!!!TypefacesTest!!!”)中的所有字符只能被设置成同一种字体,如果想将“字体测试!!!”设置为楷书,而“TypefacesTest!!!”设置为sans就必须将这两段字符用两个TextView显示,并对各自的字体分别进行设置。

==============main.xml=============

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#ffffffff"

>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/kaishu"

android:textSize="30px"

android:textColor="#ff000000"

android:typeface="kaishu"

/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/sans"

android:textSize="30px"

android:textColor="#ff000000"

android:typeface="sans"

/>

</LinearLayout>

效果图如下:

ttf字体

TTF(TrueTypeFont)是Apple公司和Microsoft公司共同推出的字体文件格式,随着windows的流行,已经变成最常用的一种字体文件表示方式。是字库中的一种,应用范围非常广。

桌面出版系统使用的字库有两种标准:postscript字库和truetype字库。android系统中使用的是truetype,这两种字体标准都是采用曲线方式描述字体轮廓,因此都可以输出很高质量的字形。常用的字库标准是truetype字库,truetype字体是windows操作系统使用的唯一字体标准。truetype字体的最大优点是可以很方便地把字体轮廓转换成曲线,可以对曲线进行填充,制成各种颜色和效果,它可以进一步变形,制作特殊效果字体,因此经常用来制作一些标题字或花样字。truetype字便宜,字款丰富。

ttf与ttc字体有什么区别?

TrueType字体通常包含在单个TrueType字体文件中, 其文件后缀为.TTF。OpenType字体是以类似于TrueType字体的格式编码的POSTSCRIPT字体。OPENTYPE字体使用.OTF文件后缀。OPENTYPE还允许把多个 OPENTYPE字体组合在一个文件中以利于数据共享。这些字体被称为TrueType字体集(TrueType collection),其文件后缀为.TTC。

转载地址:https://blog.csdn.net/taoshengyang/article/details/6664789 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:InputMethod Framework
下一篇:java serilization

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月15日 04时29分37秒