A correct explanation of the coordinate parameters in the android canvas. drawtext Method

Source: Internet
Author: User
Tags drawtext

Article transferred from: Workshop.

Canvas. drawtext ("www.jcodecraeer.com", X, Y, paint); is the X and Y parameters the coordinates of the center of the specified string? Or the coordinates in the upper left corner? The intuitive impression of this problem should be the coordinates in the upper left corner, but the android processing is a little different. I doubt whether the android designers have problems with their brains.
X is 'www .jcodecraeer.com 'by default. settextalign (paint. align. center); that is, the character center. y specifies the position of the baseline character on the screen.

API explanation:

Public void drawtext (string text, float X, float y, paint)
Since: API Level 1 draw the text, with origin at (x, y), using the specified paint.
The origin is interpreted based on the align setting in the paint.

The specific position of the starting point is determined by the align setting of the paint.
Parameters
Text the text to be drawn
X The X-coordinate of the origin of the text being drawn
Y The Y-coordinate of the origin of the text being drawn
Paint the paint used for the text (e.g. color, size, style)

When canvas is used as a text drawing, the fontmetrics object is used to calculate the coordinates of the position. The idea is basically the same as that of Java. AWT. fontmetrics.
The fontmetrics object is based on four basic coordinates:

Fontmetrics. Top
Fontmetrics. Ascent
Fontmetrics. Descent
Fontmetrics. Bottom

Paint textpaint = new paint (paint. anti_alias_flag );

Textpaint. settextsize (35 );

Textpaint. setcolor (color. White );

// Fontmetrics object

Fontmetrics = textpaint. getfontmetrics ();

String text = "abcdefghijklmnopqrstu ";

// Calculate each coordinate

Float basex = 0;

Float basis = 100;

Float topy = basey + fontmetrics. Top;

Float ascenty = basey + fontmetrics. ascent;

Float descenty = basey + fontmetrics. descent;

Float bottomy = basey + fontmetrics. bottom;

// Draw text

Canvas. drawtext (text, basex, basey, textpaint );

// Baseline plotting

Paint baselinepaint = new paint (paint. anti_alias_flag);>

Baselinepaint. setcolor (color. Red );

Canvas. drawline (0, basey, getwidth (), basey, baselinepaint );

// Base plotting

Canvas. drawcircle (basex, basey, 5, baselinepaint );

// Topline plotting

Paint toplinepaint = new paint (paint. anti_alias_flag );

Toplinepaint. setcolor (color. ltgray );

Canvas. drawline (0, topy, getwidth (), topy, toplinepaint );

// Ascentline plotting

Paint ascentlinepaint = new paint (paint. anti_alias_flag );

Ascentlinepaint. setcolor (color. Green );

Canvas. drawline (0, ascenty, getwidth (), ascenty, ascentlinepaint );

// Descentline

Paint descentlinepaint = new paint (paint. anti_alias_flag );

Descentlinepaint. setcolor (color. Yellow );

Canvas. drawline (0, descenty, getwidth (), descenty, descentlinepaint );

// Buttomline plotting

Paint bottomlinepaint = new paint (paint. anti_alias_flag );

Bottomlinepaint. setcolor (color. Magenta );

Canvas. drawline (0, bottomy, getwidth (), bottomy, bottomlinepaint );

The drawtext painting string is baseline aligned. So pay special attention to this. Otherwise, the text may be painted elsewhere, and you may mistakenly think that the text has not been painted.

If baseline is aligned, the Y coordinate at the bottom is: (Row Height-font height)/2 + font height, but the string is not centered. After testing, if: (Row Height-font height) /2 + font height-6, a little center. The above method is just a clever practice, and the method for setting text center is not found on the Internet.

There will be errors according to the above method. Add that distance:

FontMetrics fontMetrics = mPaint.getFontMetrics();float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;float offY = fontTotalHeight / 2 - fontMetrics.bottom;float newY = baseY + offY;canvas.drawText(text, baseX, newY, paint);

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.