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.