Android 磨损(长文本视图)

Android Wear(Long Textview)

大家好,我正在使用 canvas 绘制文本,我需要每小时更改一次文本视图, 如果 textview 扩展了它应该显示在下一行的宽度 例如,我有一个像 "Good Morning" 和 "Good Morning rachel ! welcome you"

这样的文本

第一个文本视图早上好将以单行正确显示,但第二个文本视图我需要用两行打印 怎么画呢

String mytext = "hi how are you how was the day "
  canvas.drawText(mytext , x, centerX, mTextPaint);

例如你有这样的字符串

String text = "This is\nmulti-line\ntext";

然后像这样在textview中绘制

canvas.drawText("This is", 100, 100, mTextPaint);
canvas.drawText("multi-line", 100, 150, mTextPaint);
canvas.drawText("text", 100, 200, mTextPaint);

第二种方式。

    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();

有关更多信息,请参阅此 link

scale = resources.getDisplayMetrics().density;

        TextPaint TextPaint=new TextPaint();
        TextPaint.setARGB(255, 255, 255, 255);
        TextPaint.setTextSize(28f);
        TextPaint.setTypeface(myfonttime);
        StaticLayout mTextLayout;
        mTextLayout = new StaticLayout(myText, TextPaint, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

        int textHeight = mTextLayout.getHeight()- (int) (16 * scale +5.0);
        int textWidth = canvas.getWidth() - (int) (16 * scale);
        float x = cWidth / 2f - textWidth/2f  - bounds.left;
        float y = cHeight / 2f - textHeight/2f;

        canvas.save();
        canvas.translate(x, y);
        mTextLayout.draw(canvas);
        canvas.restore();