This article illustrates the implementation of the text (FontMetrics) for Android programming. Share to everyone for your reference, specific as follows:
Canvas as the text is drawn, the FontMetrics object is used to calculate the coordinates of the position.
Its mentality and java.awt.FontMetrics are basically the same.
FontMetrics objects
It is based on four basic coordinates, respectively:
Fontmetrics.top
Fontmetrics.ascent
Fontmetrics.descent
Fontmetrics.bottom
The picture will be as follows
The code is as follows:
Paint textpaint = new Paint (Paint.anti_alias_flag);
Textpaint.settextsize (35);
Textpaint.setcolor (Color.White);
FontMetrics object FontMetrics fontmetrics = Textpaint.getfontmetrics ();
String Text = "Abcdefghijklmnopqrstu";
calculates each coordinate 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;
Draws the text canvas.drawtext (text, Basex, Basey, Textpaint);
Baseline depicts Paint Baselinepaint = new Paint (paint.anti_alias_flag);> Baselinepaint.setcolor (color.red);
Canvas.drawline (0, Basey, getwidth (), Basey, baselinepaint);
Base-Painted Canvas.drawcircle (Basex, Basey, 5, baselinepaint);
Topline painted Paint toplinepaint = new Paint (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 painted Paint descentlinepaint = new Paint (Paint.anti_alias_flag);
Descentlinepaint.setcolor (Color.yellow);
Canvas.drawline (0, Descenty, getwidth (), Descenty, descentlinepaint);
Buttomline painted Paint bottomlinepaint = new Paint (Paint.anti_alias_flag);
Bottomlinepaint.setcolor (Color.magenta);
Canvas.drawline (0, Bottomy, getwidth (), bottomy, bottomlinepaint);
I hope this article will help you with your Android programming.