How do you exchange DynamicLayout text?

Hey, I am creating a Live Wallpaper that includes text that is drawn directly on the canvas through the TextPaint object and DynamicLayout. In my DynamicLayout object, I set the width of the canvas, and now I'm looking for a way to wrap text that passes by the canvas. Here is my text code:

    //token is my large text string
    TextPaint tp = new TextPaint();
    Layout sl = new DynamicLayout(token, tp, (int) canvasWidth, Layout.Alignment.ALIGN_NORMAL, 0, 0, true);
    canvas.translate(startPositionX , startPositionY);
    sl.draw(canvas);

How to wrap this text if it passes by canvasWidth? Any help is appreciated!

+3
source share
1 answer

Got it in a few hours ... looking at the StringTokenizer, finding the longest word from my string and wrapping the remaining text with this width.

// Assume s contains a string of words
String longestWord = "";
StringTokenizer st = new StringTokenizer(s, " ,\t");
while (st.hasMoreTokens()) {
    String w = st.nextToken();
    if (w.length() > longestWord.length()) {
        longestWord = w;
    }
}

float textWidth = tp.measureText(longestWord+" ");
Layout sl = new DynamicLayout(token, tp, (int) textWidth, Layout.Alignment.ALIGN_CENTER, lineSpacing, 0, true);
0
source

Source: https://habr.com/ru/post/1777144/


All Articles