Easy Tutorial
❮ Python Variable References And Copies Js Filter Lists ❯

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:


2. Available Typeface Objects

>

The Android system natively supports three types of fonts, namely: sans, serif, monospace. The available static object values are five:


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:

TypefaceDemo.zip


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 Android Basic Tutorial

-[1.0.1 Latest Android Basic Tutorial Catalog for 201

❮ Python Variable References And Copies Js Filter Lists ❯