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 visual impression of this question should be the coordinates of the upper left corner, but the processing of Android is a bit different, I suspect that the Android designer is not a skull problem.
x default is ' www.jcodecraeer.com ' the left side of the string in the screen position, if paint.settextalign (Paint.Align.CENTER) is set, that is the center of the character, Y is the position of the specified character baseline on the screen.
Explanation of the API:
public void DrawText (String text, float x, float y, paint 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 location of the starting point depends on 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)
Canvas as the drawing text, uses the FontMetrics object to calculate the coordinates of the position. Its ideas and java.awt.FontMetrics are basically the same.
FontMetrics Object It is based on four basic coordinates, respectively:
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 FontMetrics = Textpaint.getfontmetrics ();
String Text = "Abcdefghijklmnopqrstu";
Calculate each of the coordinates
float BaseX = 0;
float Basey = 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 depiction
Paint Baselinepaint = new Paint (paint.anti_alias_flag);>
Baselinepaint.setcolor (color.red);
Canvas.drawline (0, Basey, getwidth (), Basey, baselinepaint);
Base painting
Canvas.drawcircle (BaseX, Basey, 5, baselinepaint);
Topline depiction
Paint Toplinepaint = new paint (Paint.anti_alias_flag);
Toplinepaint.setcolor (Color.ltgray);
Canvas.drawline (0, Topy, getwidth (), topy, toplinepaint);
Ascentline depiction
Paint Ascentlinepaint = new paint (Paint.anti_alias_flag);
Ascentlinepaint.setcolor (Color.green);
Canvas.drawline (0, Ascenty, getwidth (), Ascenty, ascentlinepaint);
Descentline depiction
Paint Descentlinepaint = new paint (Paint.anti_alias_flag);
Descentlinepaint.setcolor (Color.yellow);
Canvas.drawline (0, Descenty, getwidth (), Descenty, descentlinepaint);
Buttomline depiction
Paint Bottomlinepaint = new paint (Paint.anti_alias_flag);
Bottomlinepaint.setcolor (Color.magenta);
Canvas.drawline (0, Bottomy, getwidth (), bottomy, bottomlinepaint);
The DrawText draws a string that is baseline aligned. So pay special attention to this, otherwise the text may be drawn to other places and mistakenly think it is not drawn.
If baseline is aligned: the y-coordinate of the bottom is: (row height-font height)/2+ font height, but the string is not centered, tested if: (row height-font height)/2+ font height-6, it is slightly centered. The above method is only a trickery practice, the web also found no way to set text center.
There will be an error according to the above method. Plus that distance should be OK:
12345 |
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); |
About the correct interpretation of the coordinate parameters in the Android Canvas.drawtext method