• 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值接近,甚至一样
  • 相关阅读:
    基于安卓android微信小程序的快递取件及上门服务系统
    FlinkSQL-UDF自定义数据源
    c# --- 关于各种变量的一些补充
    解决:git SSL certificate problem: unable to get local issuer certificate
    每日练习------使用Java实现输出如下图形。(三角形,空心菱形等)
    数字图像处理(入门篇)四 像素关系
    [静态时序分析简明教程(八)]虚假路径
    【顶顶通呼叫中心中间件(mod_cti 基于 FreeSWITCH)-拨号方案和路由配置】
    降维算法实战项目(2)—使用PCA对图像降维(Python代码+数据集)
    玩机搞机-----安卓全机型刷机卡fastboot模式解决方法与故障解析
  • 原文地址:https://blog.csdn.net/z936689039/article/details/134367438