Android Learning 16#--Learning Canvas.drawtext

Source: Internet
Author: User

Originally this article is to write about my experience in the design of the Advanced Marquee program, but the writing process spent nearly a day to understand the third parameter in Canvas.drawtext [float y] represents the true meaning. Study this article should be able to help you master FontMetrics class and Rect class member variable value specific meaning.

DrawText Elicit problems

Let's take a look at how the DrawText parameters are defined in the API.

/** * Draw the text, withOrigin at(x, y), using theSpecified paint. The * origin isInterpreted based on  theAlign settinginch  thePaint. * * @paramtextThetext  toBe drawn * @param x the X-coordinate of  theOrigin of  the textBeing drawn * @param y the Y-coordinate of  theBaseline of  the textBeing drawn * @param paint the paint used for  the text(e.g. color, size, style) */public void DrawText (@NonNull Stringtext, float x, float y, @NonNull paint paint) {Native_drawtext (Mnativecanvaswrapper,text,0,text.length(), x, Y, Paint.mbidiflags, Paint.getnativeinstance (), paint.mnativetypeface); }

Simply explain the following parameters:
@param text: The textual content to be displayed, this is not difficult to understand.
@param x: Text relative to the screen origin x direction distance, no deeper meaning, also better understand
@param y: The baseline of the text relative to the Y-direction distance of the screen origin. Okay, here's the question, what the hell is baseline?
@param Paint: Brushes

The ins and outs of baseline

First look at the position of the baseline in the text area. Analysis by an example, where only the main code is posted.

@Override protected void OnDraw (canvas canvas) {//super. OnDraw(canvas);Mrect = new Rect ();Mpaint = new Paint ();Mpaint. Settextsize( -);Mpaint. SetColor(Color. RED);Paint. FontmetricsintFontMetrics = Mpaint. Getfontmetricsint();Mpaint. Gettextbounds(Textstr,0, Textstr. Length(), Mrect);int w = Mrect. Width();int h = mrect. Height();Mrect = new Rect (Mrectleft, Mrecttop, Mrectleft+w, mrecttop+ (-fontmetrics. Top) +fontmetrics. Bottom);Canvas. DrawRect(Mrect, Mpaint);Mbaseline = (mrect. Bottom+mrect. Top)/2-(FontMetrics. Bottom+fontmetrics. Top)/2;Mbaseline =-fontmetrics. Top;        /* where y = = value of Mbaseline */Mpaint. SetColor(Color. White);Canvas. DrawText(Textstr, Mrectleft, Mbaseline, Mpaint);Mpaint. SetColor(Color. BLUE);Mpaint. Setstrokewidth(3);        / * Draw out the location of the mbaseline * *Canvas. DrawLine(Mrectleft, Mbaseline, Mrectleft+w, Mbaseline, Mpaint);}

:

Analysis

As can be seen, baseline is not the bottom of the text area, but the position of this line is believed to be familiar to everyone, unless you have not studied English.

FontMetrics

FontMetrics is the paint static inner class, which mainly defines the key coordinates when paint draws text. This is described in the API:

thattheforatgiventextandthatclassisby

Main meaning: According to the set of fontsize to obtain the various variable values of the font. Obtained through the Getfontmetrics () method.
The FontMetrics class consists of 5 member variables:

     Public Static  class fontmetrics {        /** * The maximum distance above the baseline for the tallest glyph in * the font at a given text size         . */         Public floatTop/** * The recommended distance above the baseline for singled spaced text. */         Public floatAscent/** * The recommended distance below the baseline for singled spaced text. */         Public floatDescent;/** * The maximum distance below the baseline for the lowest glyph in * The font at a given text size. */         Public floatBottom/** * The recommended additional space to add between lines of text. */         Public floatLeading; }

It is clear that all values are based on baseline. Here I focus mainly on the definition of top.
Top:api's comments translate to mean that the font can draw the distance between the highest position of the area and the baseline. But it's a negative number, so -top is our baseline coordinate.
Ascent: The distance between the highest position of a single character and baseline in a font
Descent: The distance between the lowest position of a single character and the baseline in the font, corresponding to the ascent mirror
Bottom: The font can draw the distance between the lowest position of the area and the baseline.
Leading: Literal translation is an extra space for text lines. is generally 0, do not know what is the use of.

Rect

A rectangular area represented by a Rect class. The Gettextbounds () method in the paint class can get the rectangular area occupied by the text in the corresponding font size. Remember that the coordinate values of this rectangular area are also relative to the baseline calculation.
The RECT class member variable has 4, namely left, top, right, bottom. Because the rect area of the text is calculated relative to the baseline, the true top and bottom are added baseline values.

Use an example to understand FontMetrics and rect

Main Source:

@Override protected void OnDraw (canvas canvas) {//super. OnDraw(canvas);Mrect = new Rect ();Mpaint = new Paint ();Mpaint. Gettextbounds(Textstr,0,1, Mrect);Mpaint. Settextsize(textSize);Mpaint. SetColor(0xfff4a460);Paint. FontmetricsintFontMetrics = Mpaint. Getfontmetricsint();Mpaint. Gettextbounds(Textstr,0, Textstr. Length(), Mrect);int w = Mrect. Width();int h = mrect. Height();        / * Fill the FontMetrics area with 0xfff4a460 color * /Mrect = new Rect (Mrectleft, Mrecttop, Mrectleft+w, mrecttop+ (-fontmetrics. Top) +fontmetrics. Bottom);Canvas. DrawRect(Mrect, Mpaint);        / * Calculate baseline, where Mrecttop is the distance from the top of the control to the parent view * /Mbaseline =-fontmetrics. Top+ Mrecttop;        /* where y = = value of Mbaseline */Mpaint. SetColor(Color. White);Canvas. DrawText(Textstr, Mrectleft, Mbaseline, Mpaint);Mpaint. SetColor(Color. BLUE);Mpaint. Setstrokewidth(4);        /* Blue Line: Draw the position where the mbaseline is located */Canvas. DrawLine(Mrectleft, Mbaseline, Mrectleft+w, Mbaseline, Mpaint);Mpaint. SetColor(Color. GREEN);        /* Green Line: Draw the position of the bottom in the text area */Canvas. DrawLine(Mrectleft, Mbaseline+fontmetrics. Bottom, mrectleft+2*w, Mbaseline+fontmetrics. Bottom, Mpaint);Mpaint. SetColor(Color. RED);        /* Red line: Draw the position of the top of the text area */Canvas. DrawLine(Mrectleft, Mbaseline+fontmetrics. Top, mrectleft+2*w, Mbaseline+fontmetrics. Top, Mpaint);Mpaint. SetColor(Color. GRAY);        /* Gray Line: Draw the position of the ascent in the text area */Canvas. DrawLine(Mrectleft, Mbaseline+fontmetrics. Ascent, mrectleft+w+ -, Mbaseline+fontmetrics. Ascent, Mpaint);Mpaint. SetColor(Color. White);        /* White line: Draw the position of the descent in the text area */Canvas. DrawLine(Mrectleft, Mbaseline+fontmetrics. Descent, mrectleft+w+ -, Mbaseline+fontmetrics. Descent, Mpaint);Mpaint. SetColor(Color. YELLOW);        /* Yellow Line: Draw the position of the leading in the text area */Canvas. DrawLine(Mrectleft, Mbaseline+fontmetrics. Leading, mrectleft+w/2, Mbaseline+fontmetrics. Leading, Mpaint);        / * Draw the Rect of the "measured" word with a rectangle * /Mpaint. SetColor(Color. BLACK);Mpaint. SetStyle(Paint. Style. STROKE);Mpaint. Gettextbounds(Textstr,0,1, Mrect);Mrect. Top+=mbaseline;Mrect. Bottom+=mbaseline;Canvas. DrawRect(Mrect, Mpaint);        /* The rect of the "test: 12HG" Word is drawn with a rectangle */Mpaint. SetColor(Color. BLACK);Mpaint. SetStyle(Paint. Style. STROKE);Mpaint. Gettextbounds(Textstr,0,7, Mrect);Mrect. Top+=mbaseline;Mrect. Bottom+=mbaseline;Canvas. DrawRect(Mrect, Mpaint);}

:

FontMetrics:
1. The light yellow area is the FontMetrics area of the text, you can see that the text is in this area vertically centered drawing
2. Red Line: Top for FontMetrics
3. Gray Line: Ascent for FontMetrics
4. Yellow Line: Leading for FontMetrics
5. Blue Line: For baseline line
6. White line: Descent for FontMetrics
7. Green Line: Bottom for FontMetrics
Rect:
1. The first small black rectangle is the rect area of the "measured" word
2. The second long black rectangle is the rect area of the "Test: 12HG" word

Summarize

As long as thoroughly understand baseline can easily master the DrawText interface, it is not possible to develop the time always do not understand the drawing of the text is either biased or down.

Reference

http://blog.csdn.net/hursing/article/details/18703599
http://892848153.iteye.com/blog/2205002
http://blog.csdn.net/linghu_java/article/details/46404081
http://blog.csdn.net/sirnuo/article/details/21165665
http://gundumw100.iteye.com/blog/1025185

Android Learning 16#--Learning Canvas.drawtext

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.