Basic Android tutorial -- 8.3.15 Paint API -- Typeface (font)

Source: Internet
Author: User
Tags drawtext

Basic Android tutorial -- 8.3.15 Paint API -- Typeface (font)
Basic Android tutorial -- 8.3.15 Paint API -- Typeface (font)

Tags (separated by spaces): basic Android tutorial

This section introduces:

This section introduces the last API of the Paint API series,Typeface (font)From the meaning, we can probably guess, this
The API is used to set the font and font style. It is also very easy to use! Next, let's take a look at some of the Typeface problems.
Usage!

1. Optional Font Style

Four Integer constants:

BOLD: Bold ITALIC: Italic BOLD_ITALIC: Bold italic NORMAL: Normal
2. Optional font objects (Typeface)

The Android system supports three fonts by default:Sans,Serif,Monospace
The optional static object values are as follows:

DEFAULT: Default normal font object DEFAULT_BOLD: Default font object. Note: This is not actually bold, depending on the font settings.
Determined by getStyle (). MONOSPACE: Monospace Font Style SANS_SERIF: Sans serif font style SERIF: Serif font style
3. Create a Custom font

The default three fonts may not meet your requirements. You may like MAC fonts --Monaco font, You want your APP
We can use this font to prepare our TTF file and then drop itAssets/font/Directory
Then create the corresponding object. The key code is as follows:
Typeface typeFace = Typeface. createFromAsset (getAssets (), "font/MONACO. ttf ");

4. Sample Code:

Run:

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 ~ I will not explain it. You can use your own Baidu or download the sample code ~

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.