Abstract 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 ' this
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 voidDrawText (String text,floatXfloaty, Paint paint) Since:api Level1Draw the text, with Origin at (x, y),usingThe specified paint. The Origin isInterpreted based on the Align settinginchthe paint. The location of the starting point is determined by the align setting of the paint. Parameterstext the text to be drawnx the X-Coordinate of the origin of the the text being drawn y the y-Coordinate of the origin of the text being drawn paint the paint used forThe 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 =NewPaint (Paint.anti_alias_flag); Textpaint.settextsize ( *); Textpaint.setcolor (Color.White); //FontMetrics ObjectFontMetrics FontMetrics =Textpaint.getfontmetrics (); String text="Abcdefghijklmnopqrstu"; //calculate each of the coordinates floatBaseX =0; floatBasey = -; floatTopy = Basey +Fontmetrics.top; floatAscenty = Basey +fontmetrics.ascent; floatDescenty = Basey +fontmetrics.descent; floatbottomy = Basey +Fontmetrics.bottom; //Draw Textcanvas.drawtext (text, BaseX, Basey, Textpaint); //Baseline DepictionPaint Baselinepaint =NewPaint (Paint.anti_alias_flag);>Baselinepaint.setcolor (color.red); Canvas.drawline (0, Basey, GetWidth (), Basey, baselinepaint); //base PaintingCanvas.drawcircle (BaseX, Basey,5, Baselinepaint); //topline DepictionPaint Toplinepaint =NewPaint (Paint.anti_alias_flag); Toplinepaint.setcolor (Color.ltgray); Canvas.drawline (0, Topy, GetWidth (), topy, toplinepaint); //Ascentline painted paint ascentlinepaint = new paint (Paint.anti_alias_flag); Ascentlinepaint.setcolor (Color.green); Canvas.drawline (0, Ascenty, GetWidth (), Ascenty, ascentlinepaint); //Descentline DepictionPaint Descentlinepaint =NewPaint (Paint.anti_alias_flag); Descentlinepaint.setcolor (Color.yellow); Canvas.drawline (0, Descenty, GetWidth (), Descenty, descentlinepaint); //Buttomline DepictionPaint Bottomlinepaint =NewPaint (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:
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);
Correct interpretation of the coordinate parameters in the Android Canvas.drawtext method