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() //判斷當前風格是否為斜體
例程:
1http://blog.csdn.net/wonderful19891024/archive/2010/11/24/6033304.aspx
2http://www.eoeandroid.com/thread-536-1-1.html
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。