This example describes how to use custom fonts in Android. Android supports TureType fonts, which are the same as Windows supports TrueType fonts.
You can view the font in the Windows \ Fonts directory, for example, copy the Edwardian font to the assest \ fonts directory in this example:
Modify the code in this example:
[Java]
Private static class SampleView extends View {
Private Paint mPaint = new Paint (Paint. ANTI_ALIAS_FLAG );
Private Typeface mFace;
Private Typeface mFace1;
Public SampleView (Context context ){
Super (context );
MFace = Typeface. createFromAsset (getContext (). getAssets (),
"Fonts/samplefont. ttf ");
MFace1 = Typeface. createFromAsset (getContext (). getAssets (),
"Fonts/edwardian. ttf ");
MPaint. setTextSize (64 );
}
@ Override protected void onDraw (Canvas canvas ){
Canvas. drawColor (Color. WHITE );
MPaint. setTypeface (null );
Canvas. drawText ("Default", 10,100, mPaint );
MPaint. setTypeface (mFace );
Canvas. drawText ("Custom", 10,200, mPaint );
MPaint. setTypeface (mFace1 );
Canvas. drawText ("Edwardian", 10,300, mPaint );
}
}
Private static class SampleView extends View {
Private Paint mPaint = new Paint (Paint. ANTI_ALIAS_FLAG );
Private Typeface mFace;
Private Typeface mFace1;
Public SampleView (Context context ){
Super (context );
MFace = Typeface. createFromAsset (getContext (). getAssets (),
"Fonts/samplefont. ttf ");
MFace1 = Typeface. createFromAsset (getContext (). getAssets (),
"Fonts/edwardian. ttf ");
MPaint. setTextSize (64 );
}
@ Override protected void onDraw (Canvas canvas ){
Canvas. drawColor (Color. WHITE );
MPaint. setTypeface (null );
Canvas. drawText ("Default", 10,100, mPaint );
MPaint. setTypeface (mFace );
Canvas. drawText ("Custom", 10,200, mPaint );
MPaint. setTypeface (mFace1); www.2cto.com
Canvas. drawText ("Edwardian", 10,300, mPaint );
}
}
Author: mapdigit