Android - 在画布上绘制带有实心背景的文本,以便用作位图。

3

我已经构建了一个应用程序,它可以在画布上绘制带边框的文本,并将其作为位图放入谷歌地图标记中。
现在,我想要移除文本边框,并在文本背后创建一个实心的黑色矩形背景。我尝试了一些方法,但似乎无法在屏幕上得到任何有效的结果。

目前的代码:

    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)));

更新:


我现在正在尝试的是,似乎只返回文本下方的一条细黑线。

//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)));
1个回答

3

不要这样做:

canvas.drawText(text, 150, 50, mTextOutline);

您应该使用Canvas的drawRect函数之一绘制矩形。

请注意:

mTextOutline.setStyle(Paint.Style.STROKE);

只绘制边框(描边),而

mTextOutline.setStyle(Paint.Style.FILL);

应填充矩形。

您可以使用Paint.getTextBounds测量文本的大小,可能需要稍微增加一些边框。

当然,在绘制文本之前绘制边框,否则会将其隐藏。


谢谢,这真的很有帮助。我正在努力将矩形绘制到文本的大小。 - Paul Alexander

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接