The method of rendering text (FontMetrics) for Android programming _android

Source: Internet
Author: User

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.

Related Article

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.