Нарисуйте многострочный текст на холсте

Надеюсь, быстрый вопрос, но я не могу найти примеров... Я хотел бы написать многострочный текст в пользовательский View через Canvas, а в onDraw() у меня есть:

...
String text = "This is\nmulti-line\ntext";
canvas.drawText(text, 100, 100, mTextPaint);
...

Я надеялся, что это приведет к разрывам строк, но вместо этого я вижу загадочные символы там, где будет \n.

Любые указатели приветствуются.

Павел


person Paul Mennega    schedule 20.07.2011    source источник
comment
Документация рекомендует использовать Layout вместо прямого вызова Canvas.drawText. В этом разделе вопросов и ответов показано, как использовать StaticLayout для рисования многострочного текста.   -  person Suragch    schedule 26.01.2017


Ответы (18)


К сожалению, Android не знает, что такое \n. Что вам нужно сделать, так это убрать \n, а затем сместить Y, чтобы ваш текст появился на следующей строке. Что-то вроде этого:

canvas.drawText("This is", 100, 100, mTextPaint);
canvas.drawText("multi-line", 100, 150, mTextPaint);
canvas.drawText("text", 100, 200, mTextPaint);
person Icemanind    schedule 20.07.2011
comment
Так что мне пришлось бы разбить текст на три отдельных фрагмента, а затем три раза вызывать drawText()? - person Paul Mennega; 20.07.2011
comment
да. Я просто добавил пример. Используйте String.Split, чтобы разделить на «\n», а затем сместить каждый из них. - person Icemanind; 20.07.2011
comment
Большое спасибо за эту идею. - person Sumit Kumar; 23.04.2019
comment
Android знает, что такое \n, просто добавьте его в текст строки - person Danny RDX; 24.12.2020

Я нашел другой способ, используя статические макеты. Код здесь для всех, чтобы обратиться к:

TextPaint mTextPaint=new TextPaint();
StaticLayout mTextLayout = new StaticLayout(mText, mTextPaint, canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

canvas.save();
// calculate x and y position where your text will be placed

textX = ...
textY = ...

canvas.translate(textX, textY);
mTextLayout.draw(canvas);
canvas.restore();
person GreenBee    schedule 03.12.2011
comment
лучшее решение, на мой взгляд.. нет необходимости разбивать текст на строки.. Особенно удобно, если текст не имеет разрывов строк в начале или мы не знаем, есть ли они... - person Ewoks; 18.04.2012
comment
Круто, это сработало для меня. Можем ли мы предотвратить выход большого текста за пределы холста? - person moDev; 11.08.2013
comment
Очень полезно, но при центрировании StaticLayout будьте осторожны с тем, как вы устанавливаете выравнивание в TextPaint(). Использование TextPaing.setTextAlign(Align.CENTER) вызвало у меня проблемы, поскольку разные телефоны будут делать с этим разные вещи. - person greg7gkb; 20.08.2013
comment
canvas.getWidth() на самом деле должно быть getWidth() - getPaddingLeft() - getPaddingRight(), чтобы учитывать отступы представления. Кроме того, обратите внимание, что вы можете вычислить StaticLayout только тогда, когда ваш текст или размер вашего представления изменяются, и рисовать его, не создавая новый, что, вероятно, лучше! - person Jules; 12.11.2014
comment
Если вы хотите центрировать его, попробуйте: canvas.translate(viewBounds.centerX(), viewBounds.centerY() - mTextLayout.getHeight()/2); где viewBounds - это Rect вашего представления - person Cory Roy; 21.03.2015
comment
Я знаю, что это довольно старо, но могу ли я увидеть ваше окончательное решение, как и весь код? - person Eenvincible; 07.11.2015
comment
@Eenvincible, вы можете проверить мой блог здесь: skoumal.net/ en/android-drawing-multiline-text-on-bitmap - person gingo; 15.11.2015
comment
Лучшее решение, если вам все равно, где разделяется строка. Спасибо Как раз то, что мне было нужно - person Jean-Pierre Semery; 15.08.2018
comment
stackoverflow.com/questions/53105715 / - person Leo Leontev; 29.03.2019

Просто повторите каждую строку:

int x = 100, y = 100;
for (String line: text.split("\n")) {
      canvas.drawText(line, x, y, mTextPaint);
      y += mTextPaint.descent() - mTextPaint.ascent();
}
person Dave    schedule 20.07.2011
comment
Есть ли достойный способ рассчитать новую позицию y? Добавление, казалось бы, случайного числа не заставляет меня чувствовать себя очень комфортно... - person AgentKnopf; 20.02.2012
comment
Если вы чувствуете, что восхождение+прилично слишком мало, вы можете добавить постоянный коэффициент разрыва или умножить (например, на 1,5 строки) по вкусу. - person Dave; 21.02.2012
comment
обратите внимание, что восхождение отрицательно. Вам действительно нужен спуск-подъем, чтобы получить высоту - person Amir Uval; 09.01.2015
comment
Вы можете получить метрики для выбранного персонажа, например. шрифт.мера(Y) - person GregD; 10.04.2020

Я написал полный пример

введите описание изображения здесь

цвета.xml

  <color name="transparentBlack">#64000000</color>

Java-класс

 public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.amit);
        ImageView imageView = (ImageView)findViewById(R.id.imageView);
        imageView.setImageBitmap(drawTextToBitmap(this, bm, "Name: Kolala\nDate: Dec 23 2016 12:47 PM, \nLocation: 440 Banquets & Restaurents"));

    }

  public Bitmap drawTextToBitmap(Context gContext,
                                   Bitmap bitmap,
                                   String gText) {
        Resources resources = gContext.getResources();
        float scale = resources.getDisplayMetrics().density;

        android.graphics.Bitmap.Config bitmapConfig =
                bitmap.getConfig();
        // set default bitmap config if none
        if(bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
        }
        // resource bitmaps are imutable,
        // so we need to convert it to mutable one
        bitmap = bitmap.copy(bitmapConfig, true);

        Canvas canvas = new Canvas(bitmap);
        // new antialised Paint
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        // text color - #3D3D3D
        paint.setColor(Color.WHITE);
        // text size in pixels
        paint.setTextSize((int) (25 * scale));
        // text shadow
        paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

        // draw text to the Canvas center
        Rect bounds = new Rect();

        int noOfLines = 0;
        for (String line: gText.split("\n")) {
           noOfLines++;
        }

        paint.getTextBounds(gText, 0, gText.length(), bounds);
        int x = 20;
        int y = (bitmap.getHeight() - bounds.height()*noOfLines);

        Paint mPaint = new Paint();
        mPaint.setColor(getResources().getColor(R.color.transparentBlack));
        int left = 0;
        int top = (bitmap.getHeight() - bounds.height()*(noOfLines+1));
        int right = bitmap.getWidth();
        int bottom = bitmap.getHeight();
        canvas.drawRect(left, top, right, bottom, mPaint);

        for (String line: gText.split("\n")) {
            canvas.drawText(line, x, y, paint);
            y += paint.descent() - paint.ascent();
        }

        return bitmap;
    }
}
person Panchal Amit    schedule 27.12.2016
comment
Зачем использовать цикл для подсчета строк? int noOfLines = gText.split("\n").length - person Tomasz; 14.03.2018

Это мое решение, основанное на ответе @Dave (кстати, спасибо ;-))

import android.graphics.Canvas;
import android.graphics.Paint;

public class mdCanvas
{
    private Canvas m_canvas;

    public mdCanvas(Canvas canvas)
    {
        m_canvas = canvas;
    }

    public void drawMultiline(String str, int x, int y, Paint paint)
    {
        for (String line: str.split("\n"))
        {
              m_canvas.drawText(line, x, y, paint);
              y += -paint.ascent() + paint.descent();
        }
    }
}

Я пытался унаследовать Canvas, но на самом деле это не позволяет. Так это промежуточный класс!

person noelicus    schedule 07.08.2012
comment
Я пробовал так... все работает нормально, за исключением того, что последний символ последней строки моей самой большой строки отображается только наполовину. ? - person Aada; 18.04.2013

Я должен добавить сюда свою версию, которая также учитывает ШИРИНУ ШТРИХА.

void drawMultiLineText(String str, float x, float y, Paint paint, Canvas canvas) {
   String[] lines = str.split("\n");
   float txtSize = -paint.ascent() + paint.descent();       

   if (paint.getStyle() == Style.FILL_AND_STROKE || paint.getStyle() == Style.STROKE){
      txtSize += paint.getStrokeWidth(); //add stroke width to the text size
   }
   float lineSpace = txtSize * 0.2f;  //default line spacing

   for (int i = 0; i < lines.length; ++i) {
      canvas.drawText(lines[i], x, y + (txtSize + lineSpace) * i, paint);
   }
}
person Lumis    schedule 07.01.2013

это сработает. я проверял

 public Bitmap drawMultilineTextToBitmap(Context gContext,
                                       int gResId,
                                       String gText) {    
      // prepare canvas
      Resources resources = gContext.getResources();
      float scale = resources.getDisplayMetrics().density;
      Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);

      android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
      // set default bitmap config if none
      if(bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
      }
      // resource bitmaps are imutable,
      // so we need to convert it to mutable one
      bitmap = bitmap.copy(bitmapConfig, true);

      Canvas canvas = new Canvas(bitmap);

      // new antialiased Paint
      TextPaint paint=new TextPaint(Paint.ANTI_ALIAS_FLAG);
      // text color - #3D3D3D
      paint.setColor(Color.rgb(61, 61, 61));
      // text size in pixels
      paint.setTextSize((int) (14 * scale));
      // text shadow
      paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

      // set text width to canvas width minus 16dp padding
      int textWidth = canvas.getWidth() - (int) (16 * scale);

      // init StaticLayout for text
      StaticLayout textLayout = new StaticLayout(
        gText, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);

      // get height of multiline text
      int textHeight = textLayout.getHeight();

      // get position of text's top left corner
      float x = (bitmap.getWidth() - textWidth)/2;
      float y = (bitmap.getHeight() - textHeight)/2;

      // draw text to the Canvas center
      canvas.save();
      canvas.translate(x, y);
      textLayout.draw(canvas);
      canvas.restore();

      return bitmap;
    }

Источник: http://www.skoumal.net/en/android-drawing-multiline-text-on-bitmap/

person Premkumar Manipillai    schedule 21.09.2016
comment
пока я использую растровое изображение = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.transparent_flag); он работает нормально, но если я использую идентификатор текстового представления, он не будет работать - person DKV; 04.10.2016
comment
Спасибо, это сработало именно так, как я хотел, но если вы можете помочь мне с редактированием текста в нем или перемещением его в любое другое положение, как это делает фотошоп, то еще раз заранее спасибо. - person kvadityaaz; 22.12.2018

да. Используйте canvas.getFontSpacing() в качестве приращения. Я попробовал это сам из любопытства, и это работает для любого размера шрифта.

person Richard    schedule 09.04.2012
comment
Я думаю, вы имеете в виду Paint.getFontSpacing - person Jose M.; 15.09.2015

попробуй это

Paint paint1 = new Paint();
paint1.setStyle(Paint.Style.FILL);
paint1.setAntiAlias(true);
paint1.setColor(Color.BLACK);
paint1.setTextSize(15);


TextView tv = new TextView(context);
tv.setTextColor(Color.BLACK);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(5, 2, 0, 0); // llp.setMargins(left, top, right, bottom);
tv.setLayoutParams(llp);
tv.setTextSize(10);
String text="this is good to see you , i am the king of the team";

tv.setText(text);
tv.setDrawingCacheEnabled(true);
tv.measure(MeasureSpec.makeMeasureSpec(canvas.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(canvas.getHeight(), MeasureSpec.EXACTLY));
tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
canvas.drawBitmap(tv.getDrawingCache(), 5, 10, paint1);
tv.setDrawingCacheEnabled(false);
person Sunil Kumar    schedule 26.06.2014
comment
Я думаю, что это прекрасный пример того, что НЕЛЬЗЯ делать в onDraw. - person rupps; 19.04.2017
comment
@rupps Да, включать все это в onDraw может быть полным излишеством, но ответ не говорит вам об этом. Идея гениальная (и она решила мою проблему). К черту StaticLayout и String.split! - person Rodia; 05.09.2018

Я повторно использовал решение, предложенное GreenBee, и создал функцию для рисования многострочного текста в указанных границах с "..." в конце, если произошло усечение:

public static void drawMultiLineEllipsizedText(final Canvas _canvas, final TextPaint _textPaint, final float _left,
            final float _top, final float _right, final float _bottom, final String _text) {
        final float height = _bottom - _top;

        final StaticLayout measuringTextLayout = new StaticLayout(_text, _textPaint, (int) Math.abs(_right - _left),
                Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

        int line = 0;
        final int totalLineCount = measuringTextLayout.getLineCount();
        for (line = 0; line < totalLineCount; line++) {
            final int lineBottom = measuringTextLayout.getLineBottom(line);
            if (lineBottom > height) {
                break;
            }
        }
        line--;

        if (line < 0) {
            return;
        }

        int lineEnd;
        try {
            lineEnd = measuringTextLayout.getLineEnd(line);
        } catch (Throwable t) {
            lineEnd = _text.length();
        }
        String truncatedText = _text.substring(0, Math.max(0, lineEnd));

        if (truncatedText.length() < 3) {
            return;
        }

        if (truncatedText.length() < _text.length()) {
            truncatedText = truncatedText.substring(0, Math.max(0, truncatedText.length() - 3));
            truncatedText += "...";
        }
        final StaticLayout drawingTextLayout = new StaticLayout(truncatedText, _textPaint, (int) Math.abs(_right
                - _left), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

        _canvas.save();
        _canvas.translate(_left, _top);
        drawingTextLayout.draw(_canvas);
        _canvas.restore();
    }
person androidseb    schedule 23.01.2015
comment
Когда текст усекается, ваш код может вырезать целое слово, которое также помещается в пробел. Итак, вот небольшое предложение по улучшению вашего кода: замените три символа ... только одним, содержащим три точки: … (код в HTML). Затем вы можете удалить только один символ (который часто является пробелом) вместо трех и оставить свое слово неразрезанным: truncatedText = truncatedText.substring(0, Math.max(0, truncatedText.length() - 1)); - person Asterius; 02.06.2015

Решение без StaticLayout

//Get post text
    String text = post.getText();

    //Get weight of space character in px
    float spaceWeight = paint.measureText(" ");

    //Start main algorithm of drawing words on canvas
    //Split text to words
    for (String line : text.split(" ")) {
        //If we had empty space just continue
        if (line.equals("")) continue;
        //Get weight of the line
        float lineWeight = paint.measureText(line);
        //If our word(line) doesn't have any '\n' we do next
        if (line.indexOf('\n') == -1) {
            //If word can fit into current line
            if (cnv.getWidth() - pxx - defaultMargin >= lineWeight) {
                //Draw text
                cnv.drawText(line, pxx, pxy, paint);
                //Move start x point to word weight + space weight
                pxx += lineWeight + spaceWeight;
            } else {
                //If word can't fit into current line
                //Move x point to start
                //Move y point to the next line
                pxx = defaultMargin;
                pxy += paint.descent() - paint.ascent();
                //Draw
                cnv.drawText(line, pxx, pxy, paint);
                //Move x point to word weight + space weight
                pxx += lineWeight + spaceWeight;
            }
            //If line contains '\n'
        } else {
            //If '\n' is on the start of the line
            if (line.indexOf('\n') == 0) {
                pxx = defaultMargin;
                pxy += paint.descent() - paint.ascent();
                cnv.drawText(line.replaceAll("\n", ""), pxx, pxy, paint);
                pxx += lineWeight + spaceWeight;
            } else {
                //If '\n' is somewhere in the middle
                //and it also can contain few '\n'
                //Split line to sublines
                String[] subline = line.split("\n");
                for (int i = 0; i < subline.length; i++) {
                    //Get weight of new word
                    lineWeight = paint.measureText(subline[i]);
                    //If it's empty subline that's mean that we have '\n'
                    if (subline[i].equals("")) {
                        pxx = defaultMargin;
                        pxy += paint.descent() - paint.ascent();
                        cnv.drawText(subline[i], pxx, pxy, paint);
                        continue;
                    }
                    //If we have only one word
                    if (subline.length == 1 && i == 0) {
                        if (cnv.getWidth() - pxx >= lineWeight) {
                            cnv.drawText(subline[0], pxx, pxy, paint);
                            pxx = defaultMargin;
                            pxy += paint.descent() - paint.ascent();
                        } else {
                            pxx = defaultMargin;
                            pxy += paint.descent() - paint.ascent();
                            cnv.drawText(subline[0], pxx, pxy, paint);
                            pxx = defaultMargin;
                            pxy += paint.descent() - paint.ascent();
                        }
                        continue;
                    }
                    //If we have set of words separated with '\n'
                    //it is the first word
                    //Make sure we can put it into current line
                    if (i == 0) {
                        if (cnv.getWidth() - pxx >= lineWeight) {
                            cnv.drawText(subline[0], pxx, pxy, paint);
                            pxx = defaultMargin;
                        } else {
                            pxx = defaultMargin;
                            pxy += paint.descent() - paint.ascent();
                            cnv.drawText(subline[0], pxx, pxy, paint);
                            pxx = defaultMargin;
                        }
                    } else {
                        pxx = defaultMargin;
                        pxy += paint.descent() - paint.ascent();
                        cnv.drawText(subline[i], pxx, pxy, paint);
                        pxx += lineWeight + spaceWeight;
                    }
                }

            }
        }
    }
person Roman    schedule 20.03.2017

Я работал с тем, что у меня было, которое уже преобразовывало отдельные строки в холсты, и я работал над ответом Люмиса, и в итоге я получил это. 1.3 и 1.3f означают отступы между строками относительно размера шрифта.

public static Bitmap getBitmapFromString(final String text, final String font, int textSize, final int textColor)
{
    String lines[] = text.split("\n");
    textSize = getRelX(textSize);  //a method in my app that adjusts the font size relative to the screen size
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(textSize);
    paint.setColor(textColor);
    paint.setTextAlign(Paint.Align.LEFT);
    Typeface face = Typeface.createFromAsset(GameActivity.getContext().getAssets(),GameActivity.getContext().getString(R.string.font) + font + GameActivity.getContext().getString(R.string.font_ttf));
    paint.setTypeface(face);
    float baseline = -paint.ascent(); // ascent() is negative
    int width = (int) (paint.measureText(text) + 0.5f); // round
    int height = (int) (baseline + paint.descent() + 0.5f);
    Bitmap image = Bitmap.createBitmap(width, (int)(height * 1.3 * lines.length), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    for (int i = 0; i < lines.length; ++i)
    {
        canvas.drawText(lines[i], 0, baseline + textSize * 1.3f * i, paint);
    }
    return image;
}
person Peter Griffin    schedule 13.10.2018

Я столкнулся с подобной проблемой. но я должен вернуть путь к тексту. вы можете нарисовать этот путь на холсте. это мой код. Я использую Разрыв текста. и path.op

           public Path createClipPath(int width, int height) {
            final Path path = new Path();
            if (textView != null) {
                mText = textView.getText().toString();
                mTextPaint = textView.getPaint();
                float text_position_x = 0;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    text_position_x = findTextBounds(textView).left;

                }
                boolean flag = true;
                int line = 0;
                int startPointer = 0;
                int endPointer = mText.length();

                while (flag) {
                    Path p = new Path();
                    int breakText = mTextPaint.breakText(mText.substring(startPointer), true, width, null);
                    mTextPaint.getTextPath(mText, startPointer, startPointer + breakText, text_position_x,
                            textView.getBaseline() + mTextPaint.getFontSpacing() * line, p);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        path.op(p, Path.Op.UNION);
                    }
                    endPointer -= breakText;
                    startPointer += breakText;
                    line++;
                    if (endPointer == 0) {
                        flag = false;
                    }
                }

            }
            return path;
        }

и для поиска привязки к тексту я использовал эту функцию

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
private Rect findTextBounds(TextView textView) {
    // Force measure of text pre-layout.
    textView.measure(0, 0);
    String s = (String) textView.getText();

    // bounds will store the rectangle that will circumscribe the text.
    Rect bounds = new Rect();
    Paint textPaint = textView.getPaint();

    // Get the bounds for the text. Top and bottom are measured from the baseline. Left
    // and right are measured from 0.
    textPaint.getTextBounds(s, 0, s.length(), bounds);
    int baseline = textView.getBaseline();
    bounds.top = baseline + bounds.top;
    bounds.bottom = baseline + bounds.bottom;
    int startPadding = textView.getPaddingStart();
    bounds.left += startPadding;

    // textPaint.getTextBounds() has already computed a value for the width of the text,
    // however, Paint#measureText() gives a more accurate value.
    bounds.right = (int) textPaint.measureText(s, 0, s.length()) + startPadding;
    return bounds;
}
person mohandesR    schedule 27.06.2020

Для пользователей Котлин. Многострочный текст можно создать с помощью StaticLayout. Нашел отличное объяснение и как использовать его в качестве функции расширения здесь. https://medium.com/over-engineering/drawing-multiline-text-to-canvas-on-android-9b98f0bfa16a

person Ravi.Dudi    schedule 08.07.2020

В дополнение к рисованию многострочного текста могут возникнуть трудности с получением границ многострочного текста (например, чтобы выровнять его на холсте).
Значение по умолчанию paint.getTextBounds() в этом случае не будет работать, поскольку оно измерьте единственную линию.

Для удобства я создал эти 2 функции расширения: одну для рисования многострочного текста, а другую для получения границ текста.

private val textBoundsRect = Rect()

/**
 * Draws multi-line text on the Canvas with the origin at (x,y), using the specified paint. The origin is interpreted
 * based on the Align setting in the paint.
 *
 * @param text The text to be drawn
 * @param x The x-coordinate of the origin of the text being drawn
 * @param y The y-coordinate of the baseline of the text being drawn
 * @param paint The paint used for the text (e.g. color, size, style)
 */
fun Canvas.drawTextMultiLine(text: String, x: Float, y: Float, paint: Paint) {
    var lineY = y
    for (line in text.split("\n")) {
        lineY += paint.descent().toInt() - paint.ascent().toInt()
        drawText(line, x, lineY, paint)
    }
}

/**
 * Retrieve the text boundary box, taking into account line breaks [\n] and store to [boundsRect].
 *
 * Return in bounds (allocated by the caller [boundsRect] or default mutable [textBoundsRect]) the smallest rectangle that
 * encloses all of the characters, with an implied origin at (0,0).
 *
 * @param text string to measure and return its bounds
 * @param start index of the first char in the string to measure. By default is 0.
 * @param end 1 past the last char in the string to measure. By default is test length.
 * @param boundsRect rect to save bounds. Note, you may not supply it. By default, it will apply values to the mutable [textBoundsRect] and return it.
 * In this case it will be changed by each new this function call.
 */
fun Paint.getTextBoundsMultiLine(
    text: String,
    start: Int = 0,
    end: Int = text.length,
    boundsRect: Rect = textBoundsRect
): Rect {
    getTextBounds(text, start, end, boundsRect)
    val linesCount = text.split("\n").size
    val allLinesHeight = (descent().toInt() - ascent().toInt()) * linesCount
    boundsRect.bottom = boundsRect.top + allLinesHeight
    return boundsRect
}

Теперь использовать его так же просто: Для рисования многострочного текста:

canvas.drawTextMultiLine(text, x, y, yourPaint)

Для измерения текста:

val bounds = yourPaint.getTextBoundsMultiLine(текст)

В этом случае он будет измерять весь текст от начала до конца и использовать по умолчанию один раз выделенный (изменяемый) Rect.
Вы можете поиграть с передачей дополнительных параметров для дополнительной гибкости.

person Leo Droidcoder    schedule 26.07.2020

Это мое решение. Это не идеально, но сработало для меня.

public static Bitmap textAsBitmap(String text, float textSize, int textColor) {
    int lines = 1;
    String lineString1 = "", lineString2 = "";
    String[] texts = text.split(" ");
    if (texts.length > 2) {
        for (int i = 0; i < 2; i++) {
            lineString1 = lineString1.concat(texts[i] + " ");
        }
        for (int i = 2; i < texts.length; i++) {
            lineString2 = lineString2.concat(texts[i] + "");
        }
    } else {
        lineString1 = text;
    }
    lineString1 = lineString1.trim();
    lineString2 = lineString2.trim();

    String[] lastText = new String[2];
    lastText[0] = lineString1;
    if (!lineString2.equals("")) {
        lines = 2;
        lastText[1] = lineString2;
    }

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(textSize);
    paint.setColor(textColor);
    paint.setTextAlign(Paint.Align.LEFT);
    float baseline = -paint.ascent(); // ascent() is negative
    String maxLengthText = "";
    if (lines == 2) {
        if (lineString1.length() > lineString2.length()) {
            maxLengthText = maxLengthText.concat(lineString1);
        } else {
            maxLengthText = maxLengthText.concat(lineString2);
        }
    } else {
        maxLengthText = maxLengthText.concat(text);
    }
    int width = (int) (paint.measureText(maxLengthText) + 0.5f); // round
    int height = (int) ((baseline + paint.descent() + 0.5f) * lines);
    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);

    for (int i = 0; i < lines; i++) {
        canvas.drawText(lastText[i], 0, baseline, paint);
        baseline *= lines;
    }

    return image;
}
person Khánh Nguyễn Kim    schedule 13.10.2020

Я разработал лучший способ (не могу точно сказать, лучше он или нет, но этот метод должен быть простым) для многострочного текста на холсте, как в SurfaceView.

Вот будет код:

public class MultiLineText implements ObjectListener {

    private String[] lines;
    private float x, y, textSize;
    private int textColor;

    private float currentY;

    public MultiLineText(String[] lines, float x, float y, float textSize, int textColor) {
        this.lines = lines;
        this.x = x;
        this.y = y;
        this.textSize = textSize;
        this.textColor = textColor;
    }

    @Override
    public void draw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(textColor);
        paint.setTextSize(textSize);

        currentY = y;

        for (int i = 0; i < lines.length; i++) {
            if (i == 0)
                canvas.drawText(lines[i], x, y, paint);
            else {
                currentY = currentY + textSize;
                canvas.drawText(lines[i], x, currentY, paint);
            }
        }
    }

    @Override
    public void update() {

    }
}

Импортируйте 2 класса с import android.graphics.Canvas; и import android.graphics.Paint;, чтобы исключить возникновение ошибок.

Для удобства создайте интерфейсный класс с именем ObjectListener (или назовите его как хотите, просто измените имя) и добавьте две следующие строки кода:

void draw(Canvas canvas);

void update();

Чтобы реализовать это, используйте этот код в методе View или Renderer on draw(Canvas canvas):

new MultiLineText(new String[]{
        "This is a multi-line text.",
        "It's setup is basic. Just do the following code,",
        "and you would be done."
}, 150, 150, 32, Color.WHITE).draw(canvas);

Извините, я просто хотел реализовать этот текст, так что да... Вы можете изменить координаты X и Y со 150 по своему вкусу. Текст размером 26 удобочитаем, и он не слишком велик, потому что Canvas по умолчанию отображает мелкий текст.

person BeChris 100    schedule 01.07.2021

Мой пример с динамическим размером текста и интервалами, отлично работает для меня...

public Bitmap fontTexture(String string, final Context context) {
    float text_x = 512;
    float text_y = 512;
    final float scale = context.getResources().getDisplayMetrics().density;

    int mThreshold = (int) (THRESHOLD_DIP * scale + 0.5f);

    String[] splited = string.split("\\s+");
    double longest = 0;
    for(String s:splited){
        if (s.length() > longest) {
            longest = s.length();
        }
    }
    if(longest > MAX_STRING_LENGTH) {
        double ratio = (double) MAX_STRING_LENGTH / longest;
        mThreshold = (int) ((THRESHOLD_DIP * ((float) ratio)) * scale + 0.5f);
    }

    Bitmap bitmap = Bitmap.createBitmap(1024, 1024, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);

    Typeface font = Typeface.createFromAsset(context.getAssets(),
            "fonts/dotted_font.ttf");

    TextPaint mTextPaint=new TextPaint();
    mTextPaint.setColor(Color.YELLOW);
    mTextPaint.setTextAlign(Paint.Align.CENTER);
    mTextPaint.setTextSize(mThreshold);
    mTextPaint.setTypeface(font);
    StaticLayout mTextLayout = new StaticLayout(string, mTextPaint, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

    canvas.save();

    canvas.translate(text_x, text_y);
    mTextLayout.draw(canvas);
    canvas.restore();


    return bitmap;
}
person Thunderstick    schedule 20.04.2016