float float y, paint paint);
Is the method of drawing text in Android, where x represents the starting point of the x-axis when the text is drawn, and Y is the baseline of the text at the y-axis, not the center of the text or the bottom of the text.
The following code calculates the Baseline,top,bottom,ascent and descent based on the centery of the center point of the plotted y-axis
@Overrideprotected voidOnDraw (canvas canvas) {inttop =Mpaint.getfontmetricsint (). Top; intBottom =Mpaint.getfontmetricsint (). Bottom; intAscent =mpaint.getfontmetricsint (). Ascent; intDescent =Mpaint.getfontmetricsint (). Descent; intBaseliney =centery + (bottom-top)/2-Bottom; Top= Baseliney +top; Bottom= Baseliney +Bottom; Ascent= Baseliney +ascent; Descent= Baseliney +descent; Mpaint.setcolor (Color.green); Canvas.drawtext (text,100, Baseliney, Mpaint); Canvas.drawline (0, CenterY, Getmeasuredwidth (), CenterY, mpaint); Mpaint.setstrokewidth (2); Mpaint.setcolor (Color.Black); Canvas.drawline (0, Top, Getmeasuredwidth (), top, mpaint); Mpaint.setcolor (Color.yellow); Canvas.drawline (0, Ascent, Getmeasuredwidth (), ascent, Mpaint); Mpaint.setcolor (color.red); Canvas.drawline (0, Baseliney, Getmeasuredwidth (), Baseliney, mpaint); Mpaint.setcolor (Color.yellow); Canvas.drawline (0, Descent, Getmeasuredwidth (), descent, Mpaint); Mpaint.setcolor (Color.Black); Canvas.drawline (0, Bottom, getmeasuredwidth (), bottom, mpaint); }
Vertical centering when drawing text on Android