Android font drawing, distance calculation notes, and android draw

Source: Internet
Author: User
Tags drawtext

Android font drawing, distance calculation notes, and android draw

Please indicate the source for reprinting. If any error occurs, please point out

Android FontMetrics is defined in the Paint class and has five attributes respectively. To understand this attribute, you can draw a pen and draw a picture with me. Then, we can compare the image to understand the attributes:

Top: The top green and green lines in Ag ~, Right, that is, the top we see. We use the baseline font to limit the maximum height. Where is baseline? The blue line in the figure is.

Ascent: The red line above baseline.

Descent: the green line below baseline.

Bottom: The image is painted. He is under decent, that is, the minimum height. Do you understand ?~

Leading: according to the information I have checked and my personal understanding, leading has two places. The distance between top-ascent, that is, the distance from the top Green Line to the red line, and the distance from bottom-descent, the green line at the bottom.



We pass
FontMetrics fontMetrics = new FontMetrics ();
Paint. getFontMetrics (fontMetrics );

Method.

Here, let's look at a few more pictures to deepen our understanding:




The figure below is complex. If you forget it, you can skip it and use it as a remark ..


Next, how do I calculate the distance for the words I want to draw?


First line: "test"

We do not need to draw the canvas offset,

Use the drawText (string, float, float, and paint) method to draw the image.

In the drawText (string, float, float, paint) method, the words drawn are above float x and float y. So here, x and y are understood as the baseline position.

When drawing the first line, we only need to calculate the baseline distance and draw it directly.


Line 2: Select "Next line", that is, his FontMetrics. height.

Some useful test code: (there are many places to modify)

final float marginTop = 1;final float metricsHeight = metrics.bottom - metrics.top;final float leading = metrics.leading;final float baseline = metrics.leading - metrics.top;float temp1 = marginTop + baseline; float temp2 = metricsHeight + leading;for (int i = 0; i < lineCount; i++) {canvas.drawText(strs[i], rectF.centerX(), ,<span style="font-family: Arial, Helvetica, sans-serif;">temp1 + i * temp2</span><span style="font-family: Arial, Helvetica, sans-serif;">, paint);</span>}

Finally, csdn has a lot of bugs. The texture cannot be pasted, and the code is pasted in and copied and modified... Please recommend a website with reliable notes.

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.