Android 图形库,创建消息框

标签 android graphics libraries

我想做的事情很简单。我希望能够在屏幕上绘制一个消息框,将消息框的高度包裹到其中文本的大小。

我正在使用 android 的 Canvas 对象来执行此操作,但我不知道如何使用此库来完成此操作。

我正在尝试重现此内容:

enter image description here

我希望能够更改横幅内的文本并使其环绕高度。有人能告诉我这是怎么做到的吗?

编辑

这就是我目前所拥有的。有大量的数学知识,而且它几乎不适用于一种解决方案,更不用说所有解决方案了。

        int x = (int) SCREEN_WIDTH / 2, y = (int) ((SCREEN_HEIGHT / 1.3f) + messageBox.getPaint().getTextSize() + 10);
        String[] message = messageBox.getMessage().split("\n");
        for (String line: message)
        {
            System.out.println(message.length);
              canvas.drawText(line, x, y, messageBox.getPaint());
              y += -messageBox.getPaint().ascent() + messageBox.getPaint().descent();
        }

private void resizeBox() {
    if(message == null) 
        return;

    String[] msg = message.split("\n");

    this.bottom += (this.getPaint().getTextSize() + 10) * msg.length;

}

最佳答案

I am not using XML within my application

是否有人给您留下这样的印象:在 [Android] 应用程序中使用 XML 是一种选择

事实并非如此。

如果您使用您可以使用的工具,您想要完成的任务将只需要 5 分钟。

There must be an easy way to do this or else why do people use android =.=

不知道你说的是什么意思。

I just draw RectF's and draw Text using the canvas

好吧,那么:

enter image description here

在此图像中,原始文本是:

"Oops!\n\n" +
"Highscore: 59\n\n" +
"Your score: 12\n\n" +
"Better luck next time :(\n" +
"Touch the red box to try again."

正如您所看到的,线宽适合给定的空间 - 线条不会换行。

在下图中,最后一个换行符 (\n) 已被删除,为您提供了当行宽不适合屏幕宽度时会发生什么情况的示例:

enter image description here

第二张图片的文本是:

"Oops!\n\n" +
"Highscore: 59\n\n" +
"Your score: 12\n\n" +
"Better luck next time :( Touch the red box to try again."

我们使用 StaticLayout 在此处呈现文本:Link

StaticLayout 负责在 Canvas 上绘制文本所需的所有细节。 onDraw(Canvas) 如下所示:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mStaticLayout == null) {
        mStaticLayout = new StaticLayout(mText, mTextPaint, getWidth(),
            Layout.Alignment.ALIGN_CENTER, 1f, 0f, true);
    }

    canvas.save();

    // Go to where we need to draw this text
    canvas.translate(0, getHeight() / 2 - mStaticLayout.getHeight() / 2);

    // 'mBoundingBoxPaint' is an instance of 'Paint'
    mBoundingBoxPaint.setColor(Color.BLACK);
    mBoundingBoxPaint.setAlpha(100);

    // Draw the bounding box *before* drawing the text
    canvas.drawRect(0, /* paddingTop */-50, mStaticLayout.getWidth(),
                      mStaticLayout.getHeight() + /* paddingBottom */50, 
                      mBoundingBoxPaint);

    // Draw the text
    mStaticLayout.draw(canvas);
    canvas.restore();
}

您可以在上面给出的链接中阅读有关使用 StaticLayout 的更多信息。或者,您可以浏览其源代码以更好地了解它如何呈现文本。

关于Android 图形库,创建消息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30316249/

相关文章:

android - Google Cloud Messaging (GCM) 的 Android 文档的 Servlet 部分出错

android - 计算Android屏幕上两点之间的路径

android - 为什么调整大小会使我的图像看起来很奇怪?

algorithm - 动态最大流量计算的最佳图形算法/实现

c - 为什么我的代码在 x64 而不是 x32 中编译?

java - 是否有任何好的函数库可用于 Java 中的集合,例如

java - CMD中的Android备份提取错误

java - 当设备中安装了 .apk 时,谷歌地图无法工作

java - 使用 Graphics2D 使用亚像素级精度绘制图像

java - 从后台线程更新缓冲图像