Android编程之绘制文本(FontMetrics)实现方法
程序员文章站
2023-12-11 15:31:40
本文实例讲述了android编程之绘制文本(fontmetrics)实现方法。分享给大家供大家参考,具体如下:
canvas 作为绘制文本时,使用fontmetrics对...
本文实例讲述了android编程之绘制文本(fontmetrics)实现方法。分享给大家供大家参考,具体如下:
canvas 作为绘制文本时,使用fontmetrics对象,计算位置的坐标。
它的思路和java.awt.fontmetrics的基本相同。
fontmetrics对象
它以四个基本坐标为基准,分别为:
fontmetrics.top
fontmetrics.ascent
fontmetrics.descent
fontmetrics.bottom
该图片将如下
代码如下:
paint textpaint = new paint( paint.anti_alias_flag); textpaint.settextsize( 35); textpaint.setcolor( color.white); // fontmetrics对象 fontmetrics fontmetrics = textpaint.getfontmetrics(); string text = "abcdefghijklmnopqrstu"; // 计算每一个坐标 float basex = 0; float basey = 100; float topy = basey + fontmetrics.top; float ascenty = basey + fontmetrics.ascent; float descenty = basey + fontmetrics.descent; float bottomy = basey + fontmetrics.bottom; // 绘制文本 canvas.drawtext( text, basex, basey, textpaint); // baseline描画 paint baselinepaint = new paint( paint.anti_alias_flag);> baselinepaint.setcolor( color.red); canvas.drawline(0, basey, getwidth(), basey, baselinepaint); // base描画 canvas.drawcircle( basex, basey, 5, baselinepaint); // topline描画 paint toplinepaint = new paint( paint.anti_alias_flag); toplinepaint.setcolor( color.ltgray); canvas.drawline(0, topy, getwidth(), topy, toplinepaint); // ascentline描画 paint ascentlinepaint = new paint( paint.anti_alias_flag); ascentlinepaint.setcolor( color.green); canvas.drawline(0, ascenty, getwidth(), ascenty, ascentlinepaint); // descentline描画 paint descentlinepaint = new paint( paint.anti_alias_flag); descentlinepaint.setcolor( color.yellow); canvas.drawline(0, descenty, getwidth(), descenty, descentlinepaint); // buttomline描画 paint bottomlinepaint = new paint( paint.anti_alias_flag); bottomlinepaint.setcolor( color.magenta); canvas.drawline(0, bottomy, getwidth(), bottomy, bottomlinepaint);
希望本文所述对大家android程序设计有所帮助。