text——为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)编译SDK

5)将新生成的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。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值