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的最大值

 测试

public class FontMetricsDemoActivity extends Activity { 
    private Canvas canvas; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 

        Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
        textPaint.setTextSize( 55); 
        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; 
        float leading = baseY + fontMetrics.leading; 


        Log.d("fontMetrics", "baseX    is:" + 0); 
        Log.d("fontMetrics", "baseY    is:" + 100); 
        Log.d("fontMetrics", "topY     is:" + topY); 
        Log.d("fontMetrics", "ascentY  is:" + ascentY); 
        Log.d("fontMetrics", "descentY is:" + descentY); 
        Log.d("fontMetrics", "bottomY  is:" + bottomY); 
        Log.d("fontMetrics", "leading  is:" + leading); 



        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fontmetrics); 
        Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 

        canvas  = new Canvas(mutableBitmap); 



        // 绘制文本 
        canvas.drawText(text, baseX, baseY, textPaint); 

        // BaseLine描画 
        Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); 

        baseLinePaint.setColor( Color.RED); 
        canvas.drawLine(0, baseY, canvas.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, canvas.getWidth(), topY, topLinePaint); 

        // AscentLine描画 
        Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
        ascentLinePaint.setColor( Color.GREEN); 
        canvas.drawLine(0, ascentY, canvas.getWidth(), ascentY, ascentLinePaint); 

        // DescentLine描画 
        Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
        descentLinePaint.setColor( Color.YELLOW); 
        canvas.drawLine(0, descentY, canvas.getWidth(), descentY, descentLinePaint); 

        // ButtomLine描画 
        Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
        bottomLinePaint.setColor( Color.MAGENTA); 
        canvas.drawLine(0, bottomY, canvas.getWidth(), bottomY, bottomLinePaint);  

        ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
        imageView.setImageBitmap(mutableBitmap); 

    } 
}

 结果是这样的:

Android 基本属性绘制文本对象FontMetrics_第1张图片

Android 基本属性绘制文本对象FontMetrics_第2张图片

 

由图中我们发现它fontMetrics.leading不会返回结果出来,而且,fontMetrics.top跟fontMetrics.ascent值接近,甚至一样,fontMetrics.descent跟fontMetrics.bottom值接近,甚至一样

你可能感兴趣的:(源码解析,android进阶,android基础,android,java,开发语言)