Android ApiDemos example (83): Graphics-& gt; Typefaces

Source: Internet
Author: User
Tags drawtext

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
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.