Android Canvas DrawText () Text Center

Source: Internet
Author: User
Tags drawtext

Recently also in the study of their own definition of the control, had just played their own casually, has been considered to be more difficult, just now work easy, every day to see the book to see the post to learn their own definition of the control, you also don't say that you define the control learning or 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 know what the x, Y coordinates actually expressed the meaning, 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 this point has three different left,center,right for example


In the DrawText () method, the X, Y coordinates point is one of the three points on the baseline, in detail which one is based on the SetTextAlign () method of the paint, which defaults to the left

Demo sample code such as the following

Rect rect = new rect (100,100,500,500);//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 (+);        Textpaint.setstyle (Paint.Style.FILL);        The method is to set the baseline 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, that is, the top        float bottom = fontmetrics.bottom;//is the distance from the baseline to the bottom border of the font, which is the bottom in the        int baseliney = (int) (Rect.centery ()-TOP/2-BOTTOM/2);//The y-axis formula for the midpoint of the baseline        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.


Effects such as the following


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 due to settextalign (Paint.Align.Center)

So if you want to display the text in the middle, X will just be the midpoint x coordinate of the rectangle, x = Rect.centerx ()

To calculate the y-coordinate of the red dot on the middle of the baseline, see if 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.


To this I believe that we should be clear, there is no doubt that the right place to welcome the proposed.



Android Canvas DrawText () Text Center

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.