how to draw text with background color using canvas

If you want to make like this then implement below code snippet:

enter image description here

     /**
     * PUT THIS METHOD FOR IMPLEMENT WATER-MARK IN COMMON FILE
     */
    public static Bitmap waterMark(Bitmap src, String watermark) {
        //get source image width and height
        int w = src.getWidth();
        int h = src.getHeight();

        Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(src, 0, 0, null);
        Paint paint = new Paint();
        Paint.FontMetrics fm = new Paint.FontMetrics();
        paint.setColor(Color.WHITE);
        paint.getFontMetrics(fm);
        int margin = 5;
        canvas.drawRect(50 - margin, 50 + fm.top - margin,
                50 + paint.measureText(watermark) + margin, 50 + fm.bottom
                        + margin, paint);

        paint.setColor(Color.RED);

        canvas.drawText(watermark, 50, 50, paint);
        return result;
    }

// To Get Bitmap from URI:

  private Bitmap getBitmapFromUri(String photoPath) {
        Bitmap image = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
        return bitmap;
    }

// Save Image :

 private String SaveImage(Bitmap finalBitmap) {

        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/shareImage");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image" + n + ".jpg";

        File file = new File(myDir, fname);
        if (file.exists()) file.delete();
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 20, out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }

// Call like this :

                Bitmap bitmap = getBitmapFromUri(attachment.get(i).getPath()); // Enter here your Image path

                Bitmap bitmapp = waterMark(bitmap, "ENTER YOUR TEXT FOR WATERMARK LABEL");

                String path = SaveImage(bitmapp);
                Uri uri = Uri.fromFile(new File(path));

Here at last from uri you can get a new implemented watermark image.

Hope this helps you.


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

Tags:

Android