android 中图像上的中心文本?

发布于 2024-12-02 04:28:19 字数 1527 浏览 1 评论 0原文

在我的 Android 应用程序中,我需要在图像上显示文本。文本由用户在警报对话框中输入。我需要将这段文字放在图像底部的中心。我用这个在图像上绘制文本:

private Canvas drawTextImage(Bitmap b) {
        Canvas c = new Canvas(b);
        Paint paint = new Paint();
        paint.setColor(getResources().getColor(R.color.orange));

        paint.setStrokeWidth(30);
        paint.setAntiAlias(true);
        paint.setTextSize(40);
        c.drawText(text, 350, 900, paint);

        c.translate(300, 50);
        return c;
    }

我的alertDialg是这样的:

        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Enter a text ");
    final EditText input = new EditText(this);
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(25);
    input.setFilters(FilterArray);

    alert.setView(input);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            text = input.getText().toString().trim();
            Canvas c = new Canvas(bitmapResult);
            drawTextImage(bitmapResult);
            saveimage();
        }
    });

    alert.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                    saveimage();
                }
            });

    alert.show();

文本必须根据其长度居中。我该怎么做?

提前致谢..

In my android app I need to display a text on an image. The text is entered by user in an alertDialog. This text I need to center it on the bottom of the image. I draw the text on image with this :

private Canvas drawTextImage(Bitmap b) {
        Canvas c = new Canvas(b);
        Paint paint = new Paint();
        paint.setColor(getResources().getColor(R.color.orange));

        paint.setStrokeWidth(30);
        paint.setAntiAlias(true);
        paint.setTextSize(40);
        c.drawText(text, 350, 900, paint);

        c.translate(300, 50);
        return c;
    }

My alertDiallg is this :

        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Enter a text ");
    final EditText input = new EditText(this);
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(25);
    input.setFilters(FilterArray);

    alert.setView(input);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            text = input.getText().toString().trim();
            Canvas c = new Canvas(bitmapResult);
            drawTextImage(bitmapResult);
            saveimage();
        }
    });

    alert.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                    saveimage();
                }
            });

    alert.show();

Tehe text has to be center according to it's length. How can I do this?

Thanks in advance..

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

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

发布评论

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

评论(1

断桥再见 2024-12-09 04:28:19

用于

Paint.setTextAlign(Paint.Align.CENTER);

绘制文字的颜料。

public void setTextAlign(Paint.Align 对齐)

设置绘画的文本对齐方式。这控制文本的方式
相对于其原点定位。左对齐意味着所有的
文本将被绘制到其原点的右侧(即原点
指定文本的左边缘)等等。

来源


编辑:在绘制文本中,您必须将图像的一半指定为x坐标(中心),y坐标应保持不变(位于底部的某个位置,具体取决于您想要的高度) 。

Use

Paint.setTextAlign(Paint.Align.CENTER);

on the paint thats used to draw your text.

public void setTextAlign (Paint.Align align)

Set the paint's text alignment. This controls how the text is
positioned relative to its origin. LEFT align means that all of the
text will be drawn to the right of its origin (i.e. the origin
specifieds the LEFT edge of the text) and so on.

Source


Edit: In draw text you have to specify half the with of your image as the x-coordinate (center), the y-coordinate should stay the same (somewhere along the bottom, depending how high you want it).

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