The output of fonts in Android view DrawText

Source: Internet
Author: User

Canvas as the drawing text, uses the FontMetrics object to calculate the coordinates of the position.             public static class FontMetrics {public float top;             public float ascent;             public float descent;             public float bottom;   public float leading; Its various baselines can be consulted:

  is actually drawn through the code, the specific code is as follows:    mpaint.reset ();   mpaint.setcolor (Color.White);   mpaint.settextsize (80);   //FontMetrics object    fontmetrics fontmetrics = Mpaint.getfontmetrics ();    String Text = "ABCDEFG";   //calculates each coordinate    float textWidth = mpaint.measuretext (text);   float BaseX = 30;   float Basey = 700;   float topy = Basey + fontmetrics.top;   float Ascenty = Basey + fontmetrics.ascent;   float Descenty = Basey + fontmetrics.descent;   float bottomy = Basey + fontmetrics.bottom;   //Draw Text    canvas.drawtext (text, BaseX, Basey, Mpaint);   //Baseline Painted    mpaint.setcolor (color.red);   canvas.drawline (BaseX, Basey, BaseX + textWidth, Basey, Mpaint);   mpaint.settextsize (20);   canvas.drawtext ("base", BaseX + textWidth, Basey, Mpaint);   //BasE painted    canvas.drawcircle (BaseX, Basey, 5, mpaint);   //Topline painted    mpaint.setcolor (Color.ltgray);   canvas.drawline (BaseX, topy, BaseX + textWidth, topy, Mpaint);   canvas.drawtext ("Top", BaseX + textWidth, topy, Mpaint);   //Ascentline painted    mpaint.setcolor (Color.green);   canvas.drawline (BaseX, Ascenty, BaseX + textWidth, Ascenty, Mpaint);   canvas.drawtext ("Ascent", BaseX + textWidth, Ascenty + ten, mpaint);   //Descentline painted    mpaint.setcolor (Color.yellow);   canvas.drawline (BaseX, Descenty, BaseX + textWidth, Descenty, Mpaint);   canvas.drawtext ("Descent", BaseX + textWidth, Descenty, Mpaint);   //Buttomline painted    mpaint.setcolor (Color.magenta);   canvas.drawline (BaseX, bottomy, BaseX + textWidth, bottomy, Mpaint);   canvas.drawtext ("Buttom", BaseX + textWidth, bottomy + ten, mpaint); &nBSP, I believe that through the above procedures, can be a good understanding of topline,buttomline,baseline,ascentline,descentline.    In addition: The Paint class has two methods    public native float ascent ();        public native float descent ();  ascent (): The distance above the baseline (height above baseline)   descent (): The distance below the Baseline (height below baseline)   so ascent () + descent () can be regarded as the height of the text.   So far, how to get the height and width of the text has been announced:

The output of fonts in Android view DrawText

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.