Android-在画布上绘制带有纯色背景的文本以用作位图 [英] Android - Draw text with solid background onto canvas to be used as a bitmap
问题描述
我已经构建了一个应用程序,可以将带有边界的文字绘制到画布上,然后将其用作位图并放入Google地图标记中.
我现在想做的是删除文本边界,而是在文本后面创建一个实心的黑色矩形背景.我尝试了几件事,但似乎无法在屏幕上显示任何内容.
到目前为止的代码:
I've built an app that draws text with a boarder onto canvas that is then used as a bitmap and put into a google maps marker.
What I would now like to do is remove the text boarder and create a solid black rectangle background behind the text instead. I've tried a couple of things but can't seem to get anything working on screen.
Code so far:
String text = "testText";
//create bitmap
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(300, 100, conf);
//--style text
//text font
Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
//--set text style, colour, alignment, size
//text
Paint mText = new Paint();
mText.setTextAlign(Align.CENTER);
mText.setColor(Color.WHITE);
mText.setStyle(Paint.Style.FILL);
mText.setTextSize(convertToPixels(context, 12));
mText.setTypeface(tf);
mText.setAntiAlias(true);
//text outline
Paint mTextOutline = new Paint();
mTextOutline.setTextAlign(Align.CENTER);
mTextOutline.setColor(Color.BLACK);
mTextOutline.setStyle(Paint.Style.STROKE);
mTextOutline.setTextSize(convertToPixels(context, 12));
mTextOutline.setTypeface(tf);
mTextOutline.setAntiAlias(true);
mTextOutline.setStrokeWidth(2);
//create and draw text and outline onto canvas
Canvas canvas = new Canvas(bmp);
canvas.drawText(text, 150, 50, mText);
canvas.drawText(text, 150, 50, mTextOutline);
//add text marker to map
textMarker[markerID] = mapView.addMarker(new MarkerOptions()
.title("TEXT_MARKER")
.position(point)
.icon(BitmapDescriptorFactory.fromBitmap(bmp)));
更新:
我现在正在尝试什么.似乎只在文本下方返回一条黑色细线.
what I am now trying. Only seems to return a thin black line below the text.
//create bitmap
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(300, 100, conf);
//--style text
//text font
Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
//--set text style, colour, alignment, size
//text
Paint mText = new Paint();
mText.setTextAlign(Align.CENTER);
mText.setColor(Color.WHITE);
mText.setStyle(Paint.Style.FILL);
mText.setTextSize(convertToPixels(context, 12));
mText.setTypeface(tf);
mText.setAntiAlias(true);
//text outline
Paint mTextBackground = new Paint();
mTextBackground.setColor(Color.BLACK);
mTextBackground.setStyle(Style.FILL);
Rect rectangle = new Rect();
mText.getTextBounds(text, 0, text.length(), rectangle);
//create and draw text and outline onto canvas
Canvas canvas = new Canvas(bmp);
canvas.drawRect(rectangle, mTextBackground);
canvas.drawText(text, 150, 50, mText);
//add text marker to map
textMarker[markerID] = mapView.addMarker(new MarkerOptions()
.title("TEXT_MARKER")
.position(point)
.icon(BitmapDescriptorFactory.fromBitmap(bmp)));
推荐答案
代替此:
canvas.drawText(text, 150, 50, mTextOutline);
您应该使用当心
mTextOutline.setStyle(Paint.Style.STROKE);
仅表示边框(笔画),而
Means only paint the border (stroke), while
mTextOutline.setStyle(Paint.Style.FILL);
应填充矩形.
You can measure the size of the text by using Paint.getTextBounds, and maybe increase it a little so you have some borders.
当然,请在文本之前 绘制边框,否则您将其隐藏.
Of course, paint the border before the text, or you will hide it.
这篇关于Android-在画布上绘制带有纯色背景的文本以用作位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!