【问题标题】:How to set the width for text in canvas method in android?如何在android的canvas方法中设置文本的宽度?
【发布时间】:2017-07-19 22:18:00
【问题描述】:

在我的应用程序中,我使用画布绘制文本。现在我想设置绘图文本的宽度。我只能设置 xy 位置。我无法设置宽度和高度。

我的问题

更新 例如“印度是我的国家”,或者如果任何长度的文本超出背景图像之外的画布。 我想打印“印度是

我的国家“如果我设置宽度意味着我认为它会进入下一行

 @Override
protected void onDraw(Canvas canvas1)
{
Paint paint = new Paint();
        Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ban_background);
        Bitmap resizeImage1=Bitmap.createScaledBitmap(myBitmap,590,350,false);
        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(20);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas1.drawText(CameraText, 100,175, paint);
        }
         }

【问题讨论】:

  • 当你设置文本大小时,宽度和高度会自动设置,我认为你不能明确设置宽度和高度。但是如果你能解释一下你真正的问题是我可以帮助你。您可以像这样设置文本大小后获取文本的高度和宽度 Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds);
  • 更新 mt 问题 rajesh
  • 希望这是你想要的,如果不抱歉,请参考我的回答兄弟

标签: android canvas


【解决方案1】:

我曾经如下创建位图,并且我能够设置宽度和高度

 Bitmap finalImage = Bitmap.createBitmap(IMAGE_WIDTH, IMAGE_HEIGHT,
            Bitmap.Config.RGB_565);
    Bitmap tempImage = Bitmap.createBitmap(IMAGE_WIDTH / 2, IMAGE_HEIGHT,
            Bitmap.Config.RGB_565);
    Canvas g = new Canvas(finalImage);
    Canvas gtemp = new Canvas(tempImage);
    g.drawColor(Color.WHITE);
    gtemp.drawColor(Color.WHITE);
    Paint pnt = new Paint();
    pnt.setColor(Color.BLACK);

    pnt.setTextAlign(Paint.Align.CENTER);
    pnt.setTextSize(40);
    pnt.setTypeface(font1);

    g.drawText(input, IMAGE_WIDTH / 2, 40, pnt);
    Rect grct = new Rect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
    Rect grctTemp = new Rect(0, 0, IMAGE_WIDTH / 2, IMAGE_HEIGHT);
    gtemp.drawBitmap(finalImage, grct, grctTemp, pnt);

`

【讨论】:

    【解决方案2】:

    我不确定你的意思,但你可以试试 setStrokeWidth(2.0f)

    我希望这会有所帮助:D

    【讨论】:

    • 我猜你知道文本位置的 x 和 y。只需测量您可以使用此链接 (stackoverflow.com/questions/4794484/…) 执行的文本长度,然后查看 x + 宽度是否超​​出画布范围,然后将字符串切成两半并像那样打印(可能使用某种递归)之类的东西(x + textWidth > canvas.width)然后继续剪切文本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 2012-08-23
    • 2015-10-04
    • 2012-06-02
    相关资源
    最近更新 更多