Android uses custom Fonts

Source: Internet
Author: User

Android uses custom Fonts
Preface

  Android has its own default font, but sometimes we don't want to use its default font. We want to use fonts such as and. How can we do this? This article describes how to use a Custom font.

Implementation

Zookeeper first comes with a source code:

Public class MainActivity extends Activity {

@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (new CustomView (this);} class CustomView extends View {private Paint mPaint = new Paint (Paint. ANTI_ALIAS_FLAG); private Typeface mTypeface; public CustomView (Context context) {super (context); mTypeface = Typeface. createFromAsset (getContext (). getAssets (), kaiti. ttf); mPaint. setTextSize (32) ;}@ Override protected void onDraw (Canvas canvas) {canvas. drawColor (Color. WHITE); mPaint. setTypeface (mTypeface); canvas. drawText (from ProgramChangesWorld, 10, 50, mPaint );}}

}
Zookeeper is as follows:

 

You can see that the method is relatively simple, that is, the custom View. In the custom View, we define a paint brush Class Object mPaint and a Typeface Class Object mTypeface, then call Typeface. createFromAsset gets the font content, and finally mPaint calls the setTypeface method. In this way, the text drawn by mPaint is the font we define.
Here, we need to note that I put the ttf font file under the assets folder, or under other folders (SD can also be used), as long as we can find the ttf file.

[Source code download]
[Android font collection 1]
[Android font collection 2]

I used a Custom font for the novel function in my own music player. If you are interested, please take a look.
The following figure shows an example of zookeeper:

 

 

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.