Recently also in the study of custom controls, previously only their own casually play the next, has been found to be more difficult, just now work easy, every day to see the book to see the Post learning custom control, you also don't say the custom control learning is quite interesting!!
Here is also to share with you my about DrawText () text Center method.
Please attach the method of DrawText ()
To tell the truth at that time, I did not understand the X, Y coordinates exactly what the meaning of the expression, but also always thought to be the coordinates of the text center, and later found that this understanding is wrong
To understand this, first look at the picture.
Like this, the Android text is drawn relative to the baseline, which is the red line in the graph, and the length of the top+bottom is equal to the font height. equals |top|+|bottom| Absolute Value
The actual drawing depends on a point on the baseline to draw the text, and there are three kinds of points that correspond to left,center,right.
The x, y coordinates in the DrawText () method refer to one of the three points on the baseline, which is set according to the SetTextAlign () method of the paint and defaults to left.
The sample code is as follows
[Java]View Plain copy
- Rect rect = new Rect (+, +, + ); Draw a rectangle
- Paint Rectpaint = new Paint ();
- Rectpaint.setcolor (Color.Blue);
- Rectpaint.setstyle (Paint.Style.FILL);
- Canvas.drawrect (rect, rectpaint);
- Paint Textpaint = new Paint ();
- Textpaint.setcolor (Color.White);
- Textpaint.settextsize (50);
- Textpaint.setstyle (Paint.Style.FILL);
- //The method is to set the baseline on whether the point is left,center, or right here I set the center
- Textpaint.settextalign (Paint.Align.CENTER);
- Paint.fontmetrics FontMetrics = Textpaint.getfontmetrics ();
- float top = fontmetrics.top; //is the distance from the baseline to the top border of the font, which is the top
- float bottom = fontmetrics.bottom; //is the distance from the baseline to the bottom border of the font, that is, the bottom
- int Baseliney = (int) (Rect.centery ()-top/2-bottom/2); The formula for calculating the y-axis of the baseline intermediate point
- Canvas.drawtext ("Hello World", Rect.centerx (), baseliney,textpaint);
Here's a bit of attention. Textpaint.getfontmetrics () This method must be set in the font size or style, and so on a series of methods that affect the font after the call, otherwise get the top and bottom value is not allowed.
The effect is as follows
Just in the middle, proving that the equation is not a problem, and then analyzing how the equation is calculated
The reason that x, y in the DrawText () method refers to the point in the middle of the baseline is because SetTextAlign (Paint.Align.Center)
So to display the text in the middle, X as long as the midpoint x coordinate of the rectangle can be x = Rect.centerx ()
To calculate the y-coordinate of the red dot on the middle of the baseline, you can see that the y of the red dot is the y-coordinate of the black point in the rectangle and the distance between the black and red dots in the graph.
The y coordinate of the rectangle is rect.centery ()
The distance between the black point and the red point is relative to the baseline (Top+bottom)/2-bottom
The top is a negative number relative to the baseline, so the formula is (-top+bottom)/2-bottom simplified to-TOP/2-BOTTOM/2
So the final calculation is RECT.CENTERY-TOP/2-BOTTOM/2.
I believe we should understand that there is no doubt that there is no place to welcome the proposed.
Android Canvas DrawText () Text Center