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: