字体的单元格居中显示

基线(baeseline),坡顶(ascenter),坡底(descenter)
上坡度(ascent),下坡度(descent)
行间距(leading):坡底到下一行坡顶的距离
字体的高度=上坡度+下坡度+行间距

ascent是指从一个字的基线(baseline)到最顶部的距离,descent是指一个字的基线到最底部的距离

注意, ascent和top都是负数

 

下面代码实现高度只有0.75单元格高度的居中显示,所以要下移一定距离y。

        foreground.setStyle(Style.FILL);
        foreground.setTextSize(height * 0.75f);
        foreground.setTextScaleX(width / height);
        foreground.setTextAlign(Paint.Align.CENTER);

        // Draw the number in the center of the tile
        FontMetrics fm = foreground.getFontMetrics();
        // Centering in X: use alignment (and X at midpoint)
        float x = width / 2;
        // Centering in Y: measure ascent/descent first
        float y = height / 2 - (fm.ascent + fm.descent) / 2;

 

网络的例子:(引用http://www.javaeye.com/topic/474526)

package com.TextTest;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.os.Bundle;
import android.view.View;

public class TextTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        setContentView(new GraphView(this));
    }
   
    static public class GraphView extends View {
        public GraphView(Context context) {
            super(context);
        }
       
        @Override
        protected void onDraw(Canvas canvas) {
            //
            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); 
        }
    }

}

你可能感兴趣的:(网络,String,Class,float,alignment)