Android Canvas.drawText方法中的坐標參數的正確解釋


摘要 canvas.drawText(www.jcodecraeer.com, x, y, paint); x和y參數是指定字符串中心的坐標嗎?還是左上角的坐標?這個問題的直觀印象應該是左上角的坐標,但是安卓的處理有點另類,我都懷疑安卓的設計者是不是腦殼有問題了。 x默認是‘www.jcodecraeer.com’這

canvas.drawText("www.jcodecraeer.com", x, y, paint);  x和y參數是指定字符串中心的坐標嗎?還是左上角的坐標?這個問題的直觀印象應該是左上角的坐標,但是安卓的處理有點另類,我都懷疑安卓的設計者是不是 腦殼有問題了。
x默認是‘www.jcodecraeer.com’這個字符串的左邊在屏幕的位置,如果設置了paint.setTextAlign(Paint.Align.CENTER);那就是字符的中心,y是指定這個字符baseline在屏幕上的位置。

API的解釋:

public void drawText (String text, float x, float y, Paint paint)
Since: API Level 1 Draw the text, with origin at (x,y), using the specified paint.
The origin is interpreted based on the Align setting in the paint.

起始點的具體位置決定於paint的align設置。
Parameters
text The text to be drawn
x The x-coordinate of the origin of the text being drawn 
y The y-coordinate of the origin of the text being drawn 
paint The paint used for the text (e.g. color, size, style) 

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);

drawText畫字符串是baseline對齊的。所以要特別注意這點,不然畫文字可能畫到其它地方而誤以為沒有畫出來。 

如果baseline對齊的話:底端的Y坐標是:(行高-字體高度)/2+字體高度 ,但是字符串並不居中,經過測試如果:(行高-字體高度)/2+字體高度-6 ,就稍微居中了一點。 以上的方法只是一個取巧的做法,網上也沒有找到設置文字居中的方法。

按上面辦法會有誤差。加上那段距離應該就行了:

 
         
FontMetrics fontMetrics = mPaint.getFontMetrics();
 
         
float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
 
         
float offY = fontTotalHeight / 2 - fontMetrics.bottom;
 
         
float newY = baseY + offY;
 
         
canvas.drawText(text, baseX, newY, paint);
 
        


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM