8.3.15 Typeface in the Paint API
Category Android Basic Tutorial
>
This section presents the final API in the Paint API series, Typeface. From the meaning of the word, we can probably guess that this API is used to set the font and font style, and it is very easy to use! Let's learn about some related usage of Typeface below!
Official API documentation: Typeface ~
1. Available Font Styles
>
Four integer constants:
BOLD: Bold
ITALIC: Italic
BOLD_ITALIC: Bold Italic
NORMAL: Normal
2. Available Typeface Objects
>
The Android system natively supports three types of fonts, namely: sans, serif, monospace. The available static object values are five:
DEFAULT: Default normal font object
DEFAULT_BOLD: The default font object, note: this is actually not possible to be bold, it depends on the font settings. Determined by getStyle()
MONOSPACE: monospace font style
SANS_SERIF: sans serif font style
SERIF: serif font style
3. Custom Typeface Creation
>
The default three fonts may not meet your needs, perhaps you like the MAC font — Monaco font, you want the text in your APP to use this font, first prepare our TTF file, and then put it in the assets/font/ directory. Then create the corresponding object, the key code is as follows:
Typeface typeFace = Typeface.createFromAsset(getAssets(),"font/MONACO.ttf");
4. Usage Code Example:
Running Effect Picture :
Custom View class: MyView.java :
/**
* Created by Jay on 2015/11/5 0005.
*/
public class MyView extends View{
private Paint mPaint1,mPaint2,mPaint3,mPaint4,mPaint5;
private Context 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, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void init(){
mPaint1 = new Paint();
mPaint2 = new Paint();
mPaint3 = new Paint();
mPaint4 = new Paint();
mPaint5 = new Paint();
mPaint1.setColor(Color.RED);
mPaint2.setColor(Color.BLUE);
mPaint3.setColor(Color.BLACK);
mPaint4.setColor(Color.YELLOW);
mPaint5.setColor(Color.GRAY);
mPaint1.setTextSize(100);
mPaint2.setTextSize(100);
mPaint3.setTextSize(100);
mPaint4.setTextSize(100);
mPaint5.setTextSize(100);
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", 100, 100, mPaint1);
canvas.drawText("Coder-pig", 100, 200, mPaint2);
canvas.drawText("Coder-pig", 100, 300, mPaint3);
canvas.drawText("Coder-pig", 100, 400, mPaint4);
canvas.drawText("Coder-pig", 100, 500, mPaint5);
}
}
Well, it's very simple ~ no explanation, those who want fonts can search Baidu or download example code themselves~
Download of This Section's Example Code:
Summary of This Section:
>
Well, the detailed explanation of the Paint API for more than a dozen sections is here, it should have covered most of the APIs that may be used, I don't know if you have all got it, these are all paving the way for our advanced customization of controls ~ Well, that's all, thank you~
-[1.0.1 Latest Android Basic Tutorial Catalog for 201
2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Box)
2.5.8 Detailed Explanation of Notification (Status Bar Notification)
2.6.4 Simple Usage of DrawerLayout (Official Side-Slide Menu)
3.6 Responding to System Setting Events (Configuration Class)
4.4.2 Further Exploration of ContentProvider - Document Provider
5.2.1 Detailed Explanation of Fragment - Implementation of Bottom Navigation Bar (Method 1)
5.2.2 Detailed Explanation of Fragment - Implementation of Bottom Navigation Bar (Method 2)
5.2.3 Detailed Explanation of Fragment - Implementation of Bottom Navigation Bar (Method 3)
[5.2.4 Detailed Explanation of Fragment - Bottom Navigation Bar +
8.3.4 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 1)
8.3.5 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 2)
8.3.6 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 3)
8.3.7 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 4)
8.3.8 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 5)
8.3.14 Paint API — Several Enumerations/Constants and ShadowLayer Shadow Effect
8.3.15 Paint API — Typeface (Font Type)
8.3.17 Canvas API Detailed Explanation (Part 2) — Clipping Methods Collection
8.3.18 Canvas API Detailed Explanation (Part 3) — Matrix and drawBitmapMesh
8.4.3 Android Animation Collection — Property Animation - First Encounter
8.4.4 Android Animation Collection — Property Animation - Revisited
11.0 "2015 Latest Android Basic Tutorial" Finale Celebration~
[12.2 DrySister Girl Viewing App (First Edition) — 2. Parsing Backend