Android Basics Getting Started tutorial--8.3.15 Paint API--typeface (font)
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
This section brings the last API of the Paint API series,Typeface (font), by meaning, as we can probably guess, this
API is used to set the font and font style, it is very easy to use! Now let's learn some of the related typeface
The usage! Official API Document: typeface~
1. Optional Styles for fonts
Four integer constants:
- Bold: Bold
- ITALIC: Italic
- bold_italic: Coarse italic
- Normal: OK
2. Optional Font object (Typeface)
The Android system supports three fonts by default:sans,serif,monospace
There are five optional static object values provided:
- Default: Normal Font object
- default_bold: Default Font object, note: This cannot actually be bold, depending on the font settings.
Determined by GetStyle ()
- monospace: monospace font style
- sans_serif: SANS SERIF font style
- SERIF: SERIF font style
3. Custom Create Font
Maybe the default three fonts do not meet you, maybe you like Mac Fonts--Monaco fonts , you want to make your app
In the text can be used in this font, first prepare our TTF file, and then dropped into the assets/font/ directory
Then create the corresponding object, the key code is as follows:
Typeface Typeface =typeface.createfromasset (Getassets (), "Font/monaco.ttf");
4. Use the code example:
Run :
Custom view class:Myview.java:
/** * Created by Jay on 2015/11/5 0005. * * Public class MyView extends View{ PrivatePaint mpaint1,mpaint2,mpaint3,mpaint4,mpaint5;PrivateContext Mcontext; Public MyView(Context context) { This(Context,NULL); } Public MyView(context context, AttributeSet attrs) {Super(context, attrs); Mcontext = context; Init (); } Public MyView(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); }Private void Init() {mPaint1 =NewPaint (); MPaint2 =NewPaint (); MPaint3 =NewPaint (); MPAINT4 =NewPaint (); MPAINT5 =NewPaint (); Mpaint1.setcolor (color.red); Mpaint2.setcolor (Color.Blue); Mpaint3.setcolor (Color.Black); Mpaint4.setcolor (Color.yellow); Mpaint5.setcolor (Color.gray); Mpaint1.settextsize ( -); Mpaint2.settextsize ( -); Mpaint3.settextsize ( -); Mpaint4.settextsize ( -); Mpaint5.settextsize ( -); Mpaint1.settypeface (Typeface.default_bold); Mpaint2.settypeface (Typeface.monospace); Mpaint3.settypeface (Typeface.sans_serif); Mpaint4.settypeface (Typeface.serif); Mpaint5.settypeface (Typeface.createfromasset (Mcontext.getassets (),"Font/monaco.ttf")); }@Override protected void OnDraw(Canvas canvas) {Canvas.drawtext ("Coder-pig", -, -, mPaint1); Canvas.drawtext ("Coder-pig", -, $, MPaint2); Canvas.drawtext ("Coder-pig", -, -, MPAINT3); Canvas.drawtext ("Coder-pig", -, -, MPAINT4); Canvas.drawtext ("Coder-pig", -, -, MPAINT5); }}
Well, very simple ~ do not explain, to the font can own Baidu or download sample code ~
The sample code for this section is downloaded:
Typefacedemo.zip
This section summarizes:
Well, the more than 10-section paint API is here, and should have covered most of the APIs that might be used,
I don't know if you've got it. No, these are for our advanced part of the custom control to do the mat ~ Well, say so much, thank you ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started tutorial--8.3.15 Paint API--typeface (font)