Vertical centering when drawing text on Android

Source: Internet
Author: User

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 (; 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 (; 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

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: 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.