代码之家  ›  专栏  ›  技术社区  ›  anon

Canvas.drawText(…)不打印换行符

  •  6
  • anon  · 技术社区  · 14 年前

    我试着在我创建的表面视图上写一些文字。如果title为false,并且文本中没有添加换行符,则它可以正常工作。但是如果我添加了一个标题和一个换行符,换行符就不会像预期的那样打印,而是打印了这个符号[]。

    有什么线索吗?为什么?

    @Override
    public void drawObject() {
    String text = "";
    if (title) {
       text = getGameBoard().getGame().getForeignProfile().getName() + "\n";
    }
    text = text + getScoreAsString();
    getGameBoard().getCanvas().drawText(text, 0, text.length(), getPosition().getX(), getPosition().getY(), getPaint());
    }
    
    4 回复  |  直到 14 年前
        1
  •  1
  •   Trevor Johns    14 年前

    []符号可能是换行符。不管出于什么原因,drawText()都不知道如何处理换行符。

        2
  •  25
  •   dbotha    13 年前

    Rect bounds = new Rect();
    void drawString(Canvas canvas, Paint paint, String str, int x, int y) {
        String[] lines = str.split("\n");
    
        int yoff = 0;
        for (int i = 0; i < lines.length; ++i) {
            canvas.drawText(lines[i], x, y + yoff, paint);
            paint.getTextBounds(lines[i], 0, lines[i].length(), bounds);
            yoff += bounds.height();
        }
    }
    
        3
  •  10
  •   FrinkTheBrave    12 年前

    迪伊的回答很好。以下代码基于dee的答案,但对个人偏好进行了一些修改:

    1. 称之为drawMultilineText(因为它相当于drawText)
    2. 将参数按与drawText相同的顺序放置(text、x、y、paint和canvas在末尾)
    3. 在开始时获取一次行高,因为从边界返回的高度取决于字母的高度,即e没有e高
    4. 增加20%的线间距

      void drawMultilineText(String str, int x, int y, Paint paint, Canvas canvas) {
          int      lineHeight = 0;
          int      yoffset    = 0;
          String[] lines      = str.split("\n");
      
          // set height of each line (height of text + 20%)
          paint.getTextBounds("Ig", 0, 2, mBounds);
          lineHeight = (int) ((float) mBounds.height() * 1.2);
          // draw each line
          for (int i = 0; i < lines.length; ++i) {
              canvas.drawText(lines[i], x, y + yoffset, paint);
              yoffset = yoffset + lineHeight;
          }
      }
      
        4
  •  4
  •   Community dbr    7 年前

    继续改进迪伊和弗林克勇士的答案。我在方法中添加了一个Rect,以允许在特定宽度区域内绘制。将考虑修改它以选择适当的字体大小,并确保它适合正确的高度区域。

        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(" ");
    
        // set height of each line (height of text + 20%)
        lineHeight = (int) (calculateHeightFromFontSize(str, fontSize) * 1.2);
        // draw each line
        String line = "";
        for (int i = 0; i < lines.length; ++i) {
    
            if(calculateWidthFromFontSize(line + " " + lines[i], fontSize) <= drawSpace.width()){
                line = line + " " + lines[i];
    
            }else{
                canvas.drawText(line, x, y + yoffset, paint);
                yoffset = yoffset + lineHeight;
                line = lines[i];
            }
        }
        canvas.drawText(line, x, y + yoffset, paint);
    
    }
    
    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());
    }
    

    这个答案包含了这个优秀的代码片段,这个代码片段可以在user850688以前的答案中找到 https://stackoverflow.com/a/11353080/1759409