"Android" Font Basic

Source: Internet
Author: User

Font

The final size of the text is related to the type of font and the size of the font in which the text is drawn.

Set Font type Paint.settypeface (Typeface Typeface)

Set Font size paint.settextsize (float textSize)

Several related dimensions and methodsPaint.fontmetrics

There are 5 properties, and these 5 properties are font-related.

top: 字体中字符超出基线之上的最大距离bottom: 字体中字符超出基线之下的最大距离ascent: 单个字符超出基线之上的推荐距离descent: 单个字符超出基线之上的推荐距离leading: 标准行间距
Paint.gettextbounds

This method gets the rectangle area occupied by the character [string], meaning the rectangular area of the visible part of the font

Rect bound = new Rect();mPaint.getTextBounds(text, 0, text.length(), bound);bound.right - bound.left //得到的就是字符[字符串]的可见部分矩形区域的宽度
Mpaint.measuretext (text)

Returns the width of the character [string], paying attention to the

bound.right - bound.left

Differentiated from one another.

Because there is usually a portion of white space on either side of each character, it is easy to read. So the size of the measuretext is usually greater than bound.right-bound.left. So, for a single character, say:

measureText = bound.right - bound.left + 字符两边的留白宽度
The overall diagram is as follows

Application Scenarios
    1. Vertically centered text, the calculation of the baseline position using FontMetrics is more convenient.

    2. Or the size of the problem to achieve alignment, the use of gettextbounds more convenient.

    3. Get the ideal width of text, easy to use Measuretext

Diagram code
Paint Mpaint = new paint (Paint.anti_alias_flag);    Rect bound = new rect (), @Overridepublic void OnDraw (canvas canvas) {super.ondraw (canvas);    Mpaint.settextsize (600);    String text = "P"; DRAW1 (canvas, text, 800);}    private void Draw1 (canvas canvas, String text, float Baseliney) {mpaint.gettextbounds (text, 0, text.length (), bound);    Mpaint.setcolor (Color.rgb (0xb4, 0xb4, 0xb4));    Canvas.drawrect (Bound.left, Bound.top + Baseliney, bound.right, Bound.bottom + Baseliney, mPaint);    Float width = mpaint.measuretext (text);    Mpaint.setcolor (Color.rgb (0x00, 0x00, 0x00));    Canvas.drawrect (Bound.left, Bound.top + baseLineY-30, Bound.left + width, bound.top + baseLineY-20, mpaint);    Paint.fontmetrics FontMetrics = Mpaint.getfontmetrics ();    Mpaint.setcolor (Color.Black);    Canvas.drawtext (text, 0, Baseliney, mpaint);    Canvas.drawline (0, Baseliney, getwidth (), Baseliney, mpaint);    Mpaint.setcolor (color.red); Canvas.drawline (0, Baseliney + fontmetrics.top, getwidth (), Baseliney + fontmetrics.top, mpaint);    Canvas.drawline (0, Baseliney + fontmetrics.bottom, getwidth (), Baseliney + Fontmetrics.bottom, mPaint);    Mpaint.setcolor (Color.Blue);    Canvas.drawline (0, Baseliney + fontmetrics.ascent, getwidth (), Baseliney + fontmetrics.ascent, mPaint); Canvas.drawline (0, Baseliney + fontmetrics.descent, getwidth (), Baseliney + fontmetrics.descent, mPaint);}
Android Share Q Group: 315658668

"Android" Font Basic

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.