• Android 基本属性绘制文本对象FontMetrics


    FontMetrics对象


    它以四个基本坐标为基准,分别为:

    ・FontMetrics.top
    ・FontMetrics.ascent
    ・FontMetrics.descent
    ・FontMetrics.bottom

    如图:

    要点如下:
    1. 基准点是baseline
    2. Ascent是baseline之上至字符最高处的距离
    3. Descent是baseline之下至字符最低处的距离
    4. Leading文档说的很含糊,其实是上一行字符的descent到下一行的ascent之间的距离
    5. Top指的是指的是最高字符到baseline的值,即ascent的最大值
    6. 同上,bottom指的是最下字符到baseline的值,即descent的最大值

     测试

    1. public class FontMetricsDemoActivity extends Activity {
    2. private Canvas canvas;
    3. /** Called when the activity is first created. */
    4. @Override
    5. public void onCreate(Bundle savedInstanceState) {
    6. super.onCreate(savedInstanceState);
    7. setContentView(R.layout.main);
    8. Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG);
    9. textPaint.setTextSize( 55);
    10. textPaint.setColor( Color.WHITE);
    11. // FontMetrics对象
    12. FontMetrics fontMetrics = textPaint.getFontMetrics();
    13. String text = "abcdefghijklmnopqrstu";
    14. // 计算每一个坐标
    15. float baseX = 0;
    16. float baseY = 100;
    17. float topY = baseY + fontMetrics.top;
    18. float ascentY = baseY + fontMetrics.ascent;
    19. float descentY = baseY + fontMetrics.descent;
    20. float bottomY = baseY + fontMetrics.bottom;
    21. float leading = baseY + fontMetrics.leading;
    22. Log.d("fontMetrics", "baseX is:" + 0);
    23. Log.d("fontMetrics", "baseY is:" + 100);
    24. Log.d("fontMetrics", "topY is:" + topY);
    25. Log.d("fontMetrics", "ascentY is:" + ascentY);
    26. Log.d("fontMetrics", "descentY is:" + descentY);
    27. Log.d("fontMetrics", "bottomY is:" + bottomY);
    28. Log.d("fontMetrics", "leading is:" + leading);
    29. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fontmetrics);
    30. Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    31. canvas = new Canvas(mutableBitmap);
    32. // 绘制文本
    33. canvas.drawText(text, baseX, baseY, textPaint);
    34. // BaseLine描画
    35. Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
    36. baseLinePaint.setColor( Color.RED);
    37. canvas.drawLine(0, baseY, canvas.getWidth(), baseY, baseLinePaint);
    38. // Base描画
    39. canvas.drawCircle( baseX, baseY, 5, baseLinePaint);
    40. // TopLine描画
    41. Paint topLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
    42. topLinePaint.setColor( Color.LTGRAY);
    43. canvas.drawLine(0, topY, canvas.getWidth(), topY, topLinePaint);
    44. // AscentLine描画
    45. Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
    46. ascentLinePaint.setColor( Color.GREEN);
    47. canvas.drawLine(0, ascentY, canvas.getWidth(), ascentY, ascentLinePaint);
    48. // DescentLine描画
    49. Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
    50. descentLinePaint.setColor( Color.YELLOW);
    51. canvas.drawLine(0, descentY, canvas.getWidth(), descentY, descentLinePaint);
    52. // ButtomLine描画
    53. Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
    54. bottomLinePaint.setColor( Color.MAGENTA);
    55. canvas.drawLine(0, bottomY, canvas.getWidth(), bottomY, bottomLinePaint);
    56. ImageView imageView = (ImageView) findViewById(R.id.imageView1);
    57. imageView.setImageBitmap(mutableBitmap);
    58. }
    59. }

     结果是这样的:

     

    由图中我们发现它fontMetrics.leading不会返回结果出来,而且,fontMetrics.top跟fontMetrics.ascent值接近,甚至一样,fontMetrics.descent跟fontMetrics.bottom值接近,甚至一样
  • 相关阅读:
    学生作业管理系统的设计与实现-计算机毕业设计源码20912
    windows本地认证
    【云原生之k8s】k8s基础详解
    Rocky Linux安装Docker
    【C++模拟实现】手撕AVL树
    QSS(Qt样式表)概念
    【附源码】Python计算机毕业设计天润律师事务所管理系统
    【licode】基于webrtc的客户端
    axios简单使用
    云计算专业创新人才培养体系的探索与实践
  • 原文地址:https://blog.csdn.net/z936689039/article/details/134367438