2012-10-16 82 views
9

我見過很多處理相似問題的線程,但它們都不適合我。在一個畫布中,我有一個尺寸爲200x200px的矩形,並且我想將文本寫入此矩形。文本不需要填充所有的矩形,但重要的是當到達矩形的末尾時應該自動地有一個換行符。我怎麼能在android中做到這一點?Android編程:如何在矩形中繪製多行文本?

感謝您的幫助!

+0

我認爲這是一個更好的解決方案包的自定義佈局,你可以很容易做到內一個TextView,並設置寬度和你的TextView到的高度「FILL_PARENT」。它適合你的目的。:) – Daniel

回答

32

您可以使用StaticLayout

RectF rect = new RectF(....) 

StaticLayout sl = new StaticLayout("This is my text that must fit to a rectangle", textPaint, (int)rect.width(), Layout.Alignment.ALIGN_CENTER, 1, 1, false); 

canvas.save(); 
canvas.translate(rect.left, rect.top); 
sl.draw(canvas); 
canvas.restore(); 
+2

謝謝!我從來沒有遇到過StaticLayout,我可以用它來清理我的應用程序中的一些煩人的代碼。 – Simon

+0

這應該是這個問題的正確答案+1 –

+0

如果您需要更改文本,您還可以用[DynamicLayout](http://developer.android.com/reference/android/text/DynamicLayout.html)替換StaticLayout之後。 –

0

可能你只是用你的畫布上方的文本字段,並啓用了該文本框多 - 那麼你會做在文本框換行符免費的午餐;-)

1
public class MutilineText { 
private String mText; 
private int fontSize = 50; 


public MutilineText(String text) { 

    this.mText = text; 
} 

public String getText() { 
    return mText; 
} 

public void setText(String text) { 
    mText = text; 
} 

public void draw(Canvas canvas, Rect drawSpace) { 


    Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG); 

    paintText.setAntiAlias(true); 
    paintText.setDither(true); 
    paintText.setColor(Color.BLACK); 
    paintText.setStyle(Paint.Style.FILL); 
    paintText.setStrokeWidth(3); 
    paintText.setTextSize(fontSize); 
    drawMultilineText(mText, drawSpace.left, drawSpace.top + 15, paintText, canvas, fontSize, drawSpace); 
} 


private void drawMultilineText(String str, int x, int y, Paint paint, Canvas canvas, int fontSize, Rect drawSpace) { 
    int lineHeight = 0; 
    int yoffset = 0; 
    String[] lines = str.split("\n"); 

    lineHeight = (int) (calculateHeightFromFontSize(str, fontSize) * 1.4); 
    String line = ""; 
    for (int i = 0; i < lines.length; ++i) { 
     if (calculateWidthFromFontSize(line, fontSize) <= drawSpace.width()) { 
      canvas.drawText(line, x + 30, y + yoffset, paint); 
      yoffset = yoffset + lineHeight; 
      line = lines[i]; 
     } else { 
      canvas.drawText(divideString(line, drawSpace.width()), x + 30, y + yoffset, paint); 
     } 
    } 


} 

private String divideString(String inputString, int bound) { 
    String ret = inputString; 

    while (calculateWidthFromFontSize(ret, fontSize) >= bound) { 
     ret = ret.substring(0, (ret.length() - 1)); 
    } 
    ret = ret.substring(0, ret.length() - 3) + "..."; 

    return ret; 
} 

private int calculateWidthFromFontSize(String testString, int currentSize) { 
    Rect bounds = new Rect(); 
    Paint paint = new Paint(); 
    paint.setTextSize(currentSize); 
    paint.getTextBounds(testString, 0, testString.length(), bounds); 

    return (int) Math.ceil(bounds.width()); 
} 

private int calculateHeightFromFontSize(String testString, int currentSize) { 
    Rect bounds = new Rect(); 
    Paint paint = new Paint(); 
    paint.setTextSize(currentSize); 
    paint.getTextBounds(testString, 0, testString.length(), bounds); 

    return (int) Math.ceil(bounds.height()); 
} 
+0

沒有代碼只回答請添加評論來解釋你在做什麼。 –