代码之家  ›  专栏  ›  技术社区  ›  Paul Alexander

Android-在画布上绘制带有纯色背景的文本,用作位图

  •  3
  • Paul Alexander  · 技术社区  · 9 年前

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

    迄今为止的代码:

        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 回复  |  直到 9 年前
        1
  •  3
  •   Sebastian    9 年前

    而不是:

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

    你应该用 drawRect 功能。

    小心

    mTextOutline.setStyle(Paint.Style.STROKE);
    

    表示仅绘制边框(笔划),而

    mTextOutline.setStyle(Paint.Style.FILL);
    

    应填充矩形。

    您可以使用 Paint.getTextBounds ,也许再增加一点,这样你就有边界了。

    当然,画边框 之前 文本,否则您将隐藏它。