Center display of cells in the font

Source: Internet
Author: User

Baseline, ascenter, and descenter)

Upper slope (ascent) and lower slope (Descent)

Leading: the distance from the bottom to the top of the next line.

Font Height = Top slope + bottom slope + row spacing

Ascent refers to the distance from a baseline to the top, and descent refers to the distance from the baseline of a word to the bottom.

Note that both ascent and top are negative.

 

The following code shows that the height of a cell is only 0.75 in the center, so move down to a certain distance from Y.

Foreground. setstyle (style. Fill );

Foreground. settextsize (height * 0.75f );

Foreground. settextscalex (width/height );

Foreground. settextalign (paint. Align. center );

// Draw the number in the center of the tile

Fontmetrics fm = foreground. getfontmetrics ();

// Centering in X: Use alignment (and X at midpoint)

Float x = width/2;

// Centering in Y: Measure ascent/descent first

Float y = height/2-(FM. ascent + FM. Descent)/2;

 

Network example :( reference http://www.javaeye.com/topic/474526)

Package com. texttest;

Import Android. App. activity;

Import Android. content. context;

Import Android. Graphics. Canvas;

Import Android. Graphics. color;

Import Android. Graphics. paint;

Import Android. Graphics. Paint. fontmetrics;

Import Android. OS. Bundle;

Import Android. View. view;

Public class texttest extends activity {

/** Called when the activity is first created .*/

@ Override

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

// Setcontentview (R. layout. Main );

Setcontentview (New graphview (this ));

}

Static public class graphview extends view {

Public graphview (context ){

Super (context );

}

@ Override

Protected void ondraw (canvas ){

//

Paint textpaint = new paint (paint. anti_alias_flag );

Textpaint. settextsize (35 );

Textpaint. setcolor (color. White );

// Fontmetrics object

Fontmetrics = textpaint. getfontmetrics ();

String text = "abcdefghijklmnopqrstu ";

// Calculate each coordinate

Float basex = 0;

Float basis = 100;

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, textpaint );

// Baseline plotting

Paint baselinepaint = new paint (paint. anti_alias_flag );

Baselinepaint. setcolor (color. Red );

Canvas. drawline (0, basey, getwidth (), basey, baselinepaint );

// Base plotting

Canvas. drawcircle (basex, basey, 5, baselinepaint );

// Topline plotting

Paint toplinepaint = new paint (paint. anti_alias_flag );

Toplinepaint. setcolor (color. ltgray );

Canvas. drawline (0, topy, getwidth (), topy, toplinepaint );

// Ascentline plotting

Paint ascentlinepaint = new paint (paint. anti_alias_flag );

Ascentlinepaint. setcolor (color. Green );

Canvas. drawline (0, ascenty, getwidth (), ascenty, ascentlinepaint );

// Descentline

Paint descentlinepaint = new paint (paint. anti_alias_flag );

Descentlinepaint. setcolor (color. Yellow );

Canvas. drawline (0, descenty, getwidth (), descenty, descentlinepaint );

// Buttomline plotting

Paint bottomlinepaint = new paint (paint. anti_alias_flag );

Bottomlinepaint. setcolor (color. Magenta );

Canvas. drawline (0, bottomy, getwidth (), bottomy, bottomlinepaint );

}

}

}

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.