开发者

How do you wrap DynamicLayout text?

Hey there- I'm creatin开发者_开发百科g a Live Wallpaper which includes text that is drawn directly to the canvas via a TextPaint and DynamicLayout object. Within my DynamicLayout Object I set the width to the canvas width, and am now looking for a way to wrap the text that extends past the canvas. Here is my text set up 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 do I wrap this text if it extends past the canvasWidth? Any help is appreciated!


Got this after a few hours... looking to StringTokenizer, finding the longest word from my string and wrapping the remaining text to that 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜