Android字符串進階之三:字體屬性及測量(FontMetrics)

原文地址:http://blog.51cto.com/mikewang/871765


最近的一個模塊正好用到字體的相關內容,整理出來。

(一) 字體的幾個參數 ,以Android API文檔定義爲準,見下圖

要點如下:

1. 基準點是baseline

2. Ascent是baseline之上至字符最高處的距離

3. Descent是baseline之下至字符最低處的距離

4. Leading文檔說的很含糊,其實是上一行字符的descent到下一行的ascent之間的距離

5. Top指的是指的是最高字符到baseline的值,即ascent的最大值

6. 同上,bottom指的是最下字符到baseline的值,即descent的最大值

 

Note:網上有很多錯誤的圖,如果有疑問,就參看文檔,區分對錯。

 

爲了幫助理解,我特此搜索了不同的示意圖。對照示意圖,會很容易理解FontMetrics的參數。

pic-1

 

pic-2

 

pic-3

 

pic-4

 

pic-5

pic-6

(二) 測試

 1,測試的代碼直接使用網上的代碼,因爲重複着衆多,無所給出原始出處,故不注出。

我增加了Bitmap作爲輸出顯示,完整代碼如下:

  1. public class FontMetricsDemoActivity extends Activity { 
  2.     private Canvas canvas; 
  3.  
  4.     /** Called when the activity is first created. */ 
  5.     @Override 
  6.     public void onCreate(Bundle savedInstanceState) { 
  7.         super.onCreate(savedInstanceState); 
  8.         setContentView(R.layout.main); 
  9.          
  10.         Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
  11.         textPaint.setTextSize( 55); 
  12.         textPaint.setColor( Color.WHITE); 
  13.  
  14.         // FontMetrics對象 
  15.         FontMetrics fontMetrics = textPaint.getFontMetrics(); 
  16.         String text = "abcdefghijklmnopqrstu"
  17.          
  18.          
  19.  
  20.         // 計算每一個座標 
  21.         float baseX = 0
  22.         float baseY = 100
  23.         float topY = baseY + fontMetrics.top; 
  24.         float ascentY = baseY + fontMetrics.ascent; 
  25.         float descentY = baseY + fontMetrics.descent; 
  26.         float bottomY = baseY + fontMetrics.bottom; 
  27.         float leading = baseY + fontMetrics.leading; 
  28.          
  29.          
  30.         Log.d("fontMetrics""baseX    is:" + 0); 
  31.         Log.d("fontMetrics""baseY    is:" + 100); 
  32.         Log.d("fontMetrics""topY     is:" + topY); 
  33.         Log.d("fontMetrics""ascentY  is:" + ascentY); 
  34.         Log.d("fontMetrics""descentY is:" + descentY); 
  35.         Log.d("fontMetrics""bottomY  is:" + bottomY); 
  36.         Log.d("fontMetrics""leading  is:" + leading); 
  37.  
  38.          
  39.          
  40.         Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fontmetrics); 
  41.         Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 
  42.          
  43.         canvas  = new Canvas(mutableBitmap); 
  44.          
  45.          
  46.  
  47.         // 繪製文本 
  48.         canvas.drawText(text, baseX, baseY, textPaint); 
  49.  
  50.         // BaseLine描畫 
  51.         Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
  52.          
  53.         baseLinePaint.setColor( Color.RED); 
  54.         canvas.drawLine(0, baseY, canvas.getWidth(), baseY, baseLinePaint); 
  55.  
  56.         // Base描畫 
  57.         canvas.drawCircle( baseX, baseY, 5, baseLinePaint); 
  58.  
  59.         // TopLine描畫 
  60.         Paint topLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
  61.         topLinePaint.setColor( Color.LTGRAY); 
  62.         canvas.drawLine(0, topY, canvas.getWidth(), topY, topLinePaint); 
  63.  
  64.         // AscentLine描畫 
  65.         Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
  66.         ascentLinePaint.setColor( Color.GREEN); 
  67.         canvas.drawLine(0, ascentY, canvas.getWidth(), ascentY, ascentLinePaint); 
  68.  
  69.         // DescentLine描畫 
  70.         Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
  71.         descentLinePaint.setColor( Color.YELLOW); 
  72.         canvas.drawLine(0, descentY, canvas.getWidth(), descentY, descentLinePaint); 
  73.  
  74.         // ButtomLine描畫 
  75.         Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
  76.         bottomLinePaint.setColor( Color.MAGENTA); 
  77.         canvas.drawLine(0, bottomY, canvas.getWidth(), bottomY, bottomLinePaint);  
  78.          
  79.         ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
  80.         imageView.setImageBitmap(mutableBitmap); 
  81.          
  82.     } 

 log顯示如下:

Note1:注意到各個數值都是整數,這是建立在baseY=100的情況下,去掉baseY,重新運行代碼,log如下:

Note2: 參照線爲baseline,即baseline=0的情況下,其他各線的數值。leading = 0,即行間距=0

2,以上是根據paint設置,獲取相關的FontMetrics屬性,並且只繪製了一行字符串,我們猜想,如果是多行,是否可以獲得行間距leanding,代碼如下:

  1. //test_multiply_lines 
  2.         TextView textView = (TextView) findViewById(R.id.textView1); 
  3.         String textMultiLines = "abcdefghijklmnopqrstuabcdefghijklmnopqrstuabcdefghijklmnopqrstuabcdefghijklmnopqrstuabcdefghijklmnopqrstu"
  4.         textView.setTextSize(55); 
  5.         textView.setText(textMultiLines); 
  6.          
  7.         FontMetrics fontMetrics2 = textView.getPaint().getFontMetrics(); 
  8.          
  9.          
  10.  
  11.         // 計算每一個座標 
  12.  
  13.         float topY = fontMetrics2.top; 
  14.         float ascentY = fontMetrics2.ascent; 
  15.         float descentY =  fontMetrics2.descent; 
  16.         float bottomY = fontMetrics2.bottom; 
  17.         float leading =  fontMetrics2.leading; 
  18.          
  19.          
  20.  
  21.         Log.d("fontMetrics""topY     is:" + topY); 
  22.         Log.d("fontMetrics""ascentY  is:" + ascentY); 
  23.         Log.d("fontMetrics""descentY is:" + descentY); 
  24.         Log.d("fontMetrics""bottomY  is:" + bottomY); 
  25.         Log.d("fontMetrics""leading  is:" + leading); 

log如下:

Note:顯然,即使是多行的情況下,仍不能獲得leading。

3,如果text是單行,獲得各個屬性將會怎樣,代碼如下:

  1. String text = "abcdefghijklmnopqrstu"
  2.         TextView textView = (TextView) findViewById(R.id.textView1); 
  3.         textView.setTextSize(55); 
  4.         textView.setText(text); 
  5.          
  6.         FontMetrics fontMetrics = textView.getPaint().getFontMetrics(); 
  7.  
  8.         // 計算每一個座標 
  9.         float baseX = 0
  10.         float baseY = 100
  11.         float topY = baseY + fontMetrics.top; 
  12.         float ascentY = baseY + fontMetrics.ascent; 
  13.         float descentY = baseY + fontMetrics.descent; 
  14.         float bottomY = baseY + fontMetrics.bottom; 
  15.         float leading =  fontMetrics.leading; 
  16.          
  17.          
  18.  
  19.         Log.d("fontMetrics""topY     is:" + fontMetrics.top); 
  20.         Log.d("fontMetrics""ascentY  is:" + fontMetrics.ascent); 
  21.         Log.d("fontMetrics""descentY is:" + fontMetrics.descent); 
  22.         Log.d("fontMetrics""bottomY  is:" + fontMetrics.bottom); 
  23.         Log.d("fontMetrics""leading  is:" + fontMetrics.leading); 

log如下圖所示:

Note:與多行獲得的屬性都相同。

 

結論:

A:雖然paint和textView所設置的textSize均爲55,且爲相同的字符串,但是兩個獲得的FontMetrics屬性值並不相同。但是,我們發現,做除法之後,均爲1.5倍關係。做出猜測,即Paint下,爲mdpi對應的size,而TextView的size已經關聯到了顯示屏幕本身的320dip。所以獲得屬性值均爲整1.5倍數

B:各種情況下,均未獲得leading值。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章