如何使用画布绘制具有背景色的文本 [英] how to draw text with background color using canvas

查看:64
本文介绍了如何使用画布绘制具有背景色的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以在位图(画布)上绘制文本

I have some code where I'm drawing my text on bitmap (canvas)

canvas.drawTextOnPath(Text, textPath[count], gipa, -10, text);

请告诉我,是否可以在具有背景颜色的path(textPath)中绘制此文本?

Please tell me, it's possible to draw this text in path(textPath) with background color?

它仅用于绘制文本的全功能

it's full function for drawing only text

 public void drawText(float x,float y ,String Text,Canvas canvas,Paint paint1 ,int count )
        {
            float xren =text.measureText(Text.trim());

            canvas.drawTextOnPath(Text, textPath[count], gipa, -10, text);

        }

使用此功能,我正在画布上绘制文本.那么如何修改此功能以在背景上绘制此文本呢?

Using this function I'm drawing text on my canvas. so how to modify this function for drawing this text with background?

推荐答案

此处最有可能需要两个步骤.您将首先沿路径绘制一条带有背景颜色的线,然后按照指示绘制文本.用油漆对象设置线条的粗细.另外,更改涂料的样式也可以提高效果.尝试 FILL STROKE FILL_AND_STROKE 具有不同的效果.

Most likely two steps are needed here. you would draw a line along path first with color for background and then draw the text as indicated. Set the thickness of the line with a paint object. Also, changing the style of the paint can help with the effect. try FILL, STROKE or FILL_AND_STROKE for different effects.

mpaint.setStyle(Paint.Style.STROKE);
mpaint.setStrokeWidth(strokeWidth);

添加了示例以绘制红色的路径(矩形):

Added sample to draw a path(rectangle) with red color:

         Paint mPaint = new Paint();
         mPaint.setColor(Color.RED);
         Path mPath = new Path();
         RectF mRectF = new RectF(20, 20, 240, 240);
         mPath.addRect(mRectF, Path.Direction.CCW);
         mPaint.setStrokeWidth(20);
         mPaint.setStyle(Paint.Style.STROKE);
         canvas.drawPath(mPath, mPaint);

然后沿着相同的路径绘制文字(蓝色):

Then draw text along same path (blue color):

        mPaint.setColor(Color.BLUE);
         mPaint.setStrokeWidth(0);
         mPaint.setStyle(Paint.Style.FILL);
         mPaint.setTextSize(20);
         canvas.drawTextOnPath("Draw the text, with origin at (x,y), using the specified paint, along the specified path.", mPath, 0, 5, mPaint);

这篇关于如何使用画布绘制具有背景色的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆