how to draw text with background color using canvas

后端 未结 4 2075
-上瘾入骨i
-上瘾入骨i 2021-02-19 16:37

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

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

Please tell me,

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-19 17:26

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

    results

提交回复
热议问题