Android ApiDemos example (81): Graphics-& gt; Text Align

Source: Internet
Author: User
Tags drawtext

In the previous example, Android ApiDemos (68): Graphics-> MeasureText describes how to obtain the size (width and height) of the drawn text string. The default alignment of text is left alignment, this example describes other Alignment Methods: Left, Center, Right, and how to draw text along any curve.

The getTextWidths method of painting gets the width of each character in the string:

[Java]
Private float [] buildTextPositions (String text,
Float y, Paint paint ){
Float [] widths = new float1;
// Initially get the widths for each char
Int n = paint. getTextWidths (text, widths );
// Now popuplate the array,
// Interleaving spaces for the Y values
Float [] pos = new float [n * 2];
Float accumulatedX = 0;
For (int I = 0; I <n; I ++ ){
Pos [I * 2 + 0] = accumulatedX;
Pos [I * 2 + 1] = y;
AccumulatedX + = widths [I];
}
Return pos;
}

Private float [] buildTextPositions (String text,
Float y, Paint paint ){
Float [] widths = new float1;
// Initially get the widths for each char
Int n = paint. getTextWidths (text, widths );
// Now popuplate the array,
// Interleaving spaces for the Y values
Float [] pos = new float [n * 2];
Float accumulatedX = 0;
For (int I = 0; I <n; I ++ ){
Pos [I * 2 + 0] = accumulatedX;
Pos [I * 2 + 1] = y;
AccumulatedX + = widths [I];
}
Return pos;
} Then draw the text in three different alignment modes: Left, Center, Right:

[Java]
P. setTextAlign (Paint. Align. LEFT );
...
P. setTextAlign (Paint. Align. CENTER );
...
P. setTextAlign (Paint. Align. RIGHT );
Canvas. drawText (TEXT_R, x, y, p );

P. setTextAlign (Paint. Align. LEFT );
...
P. setTextAlign (Paint. Align. CENTER );
...
P. setTextAlign (Paint. Align. RIGHT );
Canvas. drawText (TEXT_R, x, y, p); create a path named

[Java]
Private static void makePath (Path p ){
P. moveTo (10, 0 );
P. cubicTo (100,-50,200, 50,300, 0 );
}

Private static void makePath (Path p ){
P. moveTo (10, 0 );
P. cubicTo (100,-50,200, 50,300, 0 );
} Then draw text along the Path in three different alignment ways:

[Java]
P. setTextAlign (Paint. Align. LEFT );
...
P. setTextAlign (Paint. Align. CENTER );
...
P. setTextAlign (Paint. Align. RIGHT );
Canvas. drawPath (mPath, mPathPaint); www.2cto.com
Canvas. drawTextOnPath (TEXTONPATH, mPath, 0, 0, p );

P. setTextAlign (Paint. Align. LEFT );
...
P. setTextAlign (Paint. Align. CENTER );
...
P. setTextAlign (Paint. Align. RIGHT );
Canvas. drawPath (mPath, mPathPaint );
Canvas. drawTextOnPath (TEXTONPATH, mPath, 0, 0, p );

 


Author: mapdigit

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.