如何适应android画布中的文本宽度?

发布于 2025-01-08 03:59:21 字数 569 浏览 1 评论 0原文

您好,我的应用程序是在画布中绘制文本,其工作正常。我的问题是,当我的文本很大时,它超出了画布。

例如 text="how are you" 它正确地适合画布。但是而 text="hi hello how are you" 就像很长的文本。它超出了画布。任何人都可以帮助我。提前致谢!

        canvas1.drawColor(Color.BLACK);
        canvas1.drawBitmap(resizeImage1,10,5, null);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setTextSize(25);
        paint.setColor(Color.BLUE);
        paint.setFakeBoldText(true);
        paint.setTextSize(30);
        canvas1.drawText(mytext, 10,175, paint);

hi my application is draw the text in canvas its working fine.my problem while my text is large is goes outside of the canvas.

for example text="how are you" its correctly fit into the canvas.but while text="hi hello how are you"
like lenghthy text.it goes outside form the canvas.anybody kindly help me.Thanks in advance!

        canvas1.drawColor(Color.BLACK);
        canvas1.drawBitmap(resizeImage1,10,5, null);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setTextSize(25);
        paint.setColor(Color.BLUE);
        paint.setFakeBoldText(true);
        paint.setTextSize(30);
        canvas1.drawText(mytext, 10,175, paint);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎夕旧梦 2025-01-15 03:59:21

使用静态布局,即使您通过自动对齐文本增加了一些限制,它也会自动对齐文本。

    Rect bounds = new Rect(x1, y1, x2, y2);// set the bounds
    String textOnCanvas = "text to be wriiten";
    StaticLayout sl = new StaticLayout(textOnCanvas, textPaint,
            bounds.width(), Layout.Alignment.ALIGN_CENTER, 1, 1, true);
    canvas.save();
    float textHeight = getTextHeight(textOnCanvas, textPaint);
    int numberOfTextLines = sl.getLineCount();
    float textYCoordinate = bounds.exactCenterY() -
            ((numberOfTextLines * textHeight) / 2);

    //text will be drawn from left
    float textXCoordinate = bounds.left;
    canvas.translate(textXCoordinate, textYCoordinate);
    //draws static layout on canvas
    sl.draw(canvas);
    canvas.restore();

Use Static layouts, it will take care of auto aligning your text even if you increase it by some limit, by auto aligning the text.

    Rect bounds = new Rect(x1, y1, x2, y2);// set the bounds
    String textOnCanvas = "text to be wriiten";
    StaticLayout sl = new StaticLayout(textOnCanvas, textPaint,
            bounds.width(), Layout.Alignment.ALIGN_CENTER, 1, 1, true);
    canvas.save();
    float textHeight = getTextHeight(textOnCanvas, textPaint);
    int numberOfTextLines = sl.getLineCount();
    float textYCoordinate = bounds.exactCenterY() -
            ((numberOfTextLines * textHeight) / 2);

    //text will be drawn from left
    float textXCoordinate = bounds.left;
    canvas.translate(textXCoordinate, textYCoordinate);
    //draws static layout on canvas
    sl.draw(canvas);
    canvas.restore();
贪了杯 2025-01-15 03:59:21
int IMAGE_WIDTH = text.length();

canvas1.drawBitmap(resizeImage1,IMAGE_WIDTH,5,空);

int IMAGE_WIDTH = text.length();

canvas1.drawBitmap(resizeImage1,IMAGE_WIDTH ,5, null);

罪歌 2025-01-15 03:59:21

很可能您超出了位图的宽度。

每个画布都有一个与其关联的位图,位图的宽度可能小于文本的宽度。

因此,当您尝试绘制文本时,它会超出位图的边界,因此您无法看到文本的其余部分。

尝试增加位图的大小。

Very lickely that you are exceeding the width of the bitmap.

Each canvas has a bitmap associated with it, the width of the bitmap might be smaller than the width of the text.

So when you try to draw the text it is going beyond the boundary of the bitmap and hence you are not able to see the rest of the text.

Try increasing the size of the bitmap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文