Android 學習 之 圖形繪製篇 獲取要繪製的文字的寬度/長度
阿新 • • 發佈:2019-02-03
android自定義View繪製的時候,會有很多情況下要與字型打交道,涉及到字型寬度、高度的時候要特別注意一下幾個概念,見圖:
baseLine:一行文字的底線。
Ascent: 字元頂部到baseLine的距離。
Descent: 字元底部到baseLine的距離。
Leading: 字元行間距。
Java程式碼- publicclass TestOnDraw extends Activity {
- @Override
- protectedvoid onCreate(Bundle savedInstanceState) {
-
// TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- MyView v = new MyView(this);
- this.setContentView(v);
- }
- }
- class MyView extends View
- {
- public MyView(Context context) {
- super(context);
- }
- @Override
- protectedvoid onDraw(Canvas canvas) {
-
super.onDraw(canvas);
- Paint p = new Paint();
- p.setColor(Color.WHITE);
- p.setTextSize(50);
- p.setAntiAlias(true);
- FontMetrics fm = p.getFontMetrics();
- System.out.println("top = "+ fm.top);
- System.out.println("ascent = "+ fm.ascent);
-
System.out.println(
- System.out.println("bottom = "+ fm.bottom);
- System.out.println("leading = "+ fm.leading);
- int textHeight = (int) (Math.ceil(fm.descent - fm.ascent) + 2);
- System.out.println("textHeight = " + textHeight);
- float width =500;
- float baseline = 100f;
- float offsetAscent = baseline + fm.ascent;
- float offsetDescent = baseline +fm.descent;
- float offsetTop = baseline + fm.top;
- float offsetBottom = baseline + fm.bottom;
- canvas.drawText("中國 bp Android", 0, baseline, p);
- canvas.drawLine(0, baseline, width, baseline, p);//baseline
- canvas.drawLine(0, offsetAscent, width, offsetAscent, p);//ascent
- canvas.drawLine(0, offsetDescent, width, offsetDescent, p);//descent
- canvas.drawLine(0, offsetTop, width, offsetTop, p);//top
- canvas.drawLine(0, offsetBotton, width, offsetBottom, p);//bottom
- }
- }
- publicclass TestOnDraw extends Activity {
- @Override
- protectedvoid onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- MyView v = new MyView(this);
- this.setContentView(v);
- }
- }
- class MyView extends View
- {
- public MyView(Context context) {
- super(context);
- }
- @Override
- protectedvoid onDraw(Canvas canvas) {
- super.onDraw(canvas);
- Paint p = new Paint();
- p.setColor(Color.WHITE);
- p.setTextSize(50);
- p.setAntiAlias(true);
- FontMetrics fm = p.getFontMetrics();
- System.out.println("top = "+ fm.top);
- System.out.println("ascent = "+ fm.ascent);
- System.out.println("descent = "+ fm.descent);
- System.out.println("bottom = "+ fm.bottom);
- System.out.println("leading = "+ fm.leading);
- int textHeight = (int) (Math.ceil(fm.descent - fm.ascent) + 2);
- System.out.println("textHeight = " + textHeight);
- float width =500;
- float baseline = 100f;
- float offsetAscent = baseline + fm.ascent;
- float offsetDescent = baseline +fm.descent;
- float offsetTop = baseline + fm.top;
- float offsetBottom = baseline + fm.bottom;
- canvas.drawText("中國 bp Android", 0, baseline, p);
- canvas.drawLine(0, baseline, width, baseline, p);//baseline
- canvas.drawLine(0, offsetAscent, width, offsetAscent, p);//ascent
- canvas.drawLine(0, offsetDescent, width, offsetDescent, p);//descent
- canvas.drawLine(0, offsetTop, width, offsetTop, p);//top
- canvas.drawLine(0, offsetBotton, width, offsetBottom, p);//bottom
- }
- }
執行效果:
這是程式的輸出結果:
得出結論: canvas drawText() 的startX是從左下角的baseline的底線開始繪畫的,如果我們要得到字型的高度需要關注descent - ascent (ascent線在baseline上面,所以是負數)