1

I am drawing text on a canvas. I would like to draw a solid circle of color over the text, and only have the circle be painted where it intersects the text. Example:

enter image description here

and what I want to do:

enter image description here

I'm not sure if this is possible, my draw code is simply:

public void onDraw(Canvas canvas) {
    canvas.drawText("Hello", x, y, paint);
    paint.setColor(orange);
    canvas.drawOval(...);
}

I suppose I would need to apply some masking, but not sure how to get started.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3203425
  • 2,919
  • 4
  • 29
  • 48

2 Answers2

1

follow this tutorial from a googler... android-shaders-filters

BitmapShader may help you

Arnav M.
  • 2,590
  • 1
  • 27
  • 44
-1

You can use PorterDuffXfermode in Android to achieve this.

If you use below code it will work fine:

Bitmap original = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), 
Bitmap.Config.ARGB_8888); // Created from Canvas
Bitmap mask = 
Bitmap.createBitmap(getContext().getResources(),R.drawable.mask_image);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), 
Config.ARGB_8888);
Canvas tempCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
tempCanvas.drawBitmap(original, 0, 0, null);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
tempCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
canvas.drawBitmap(result, 0, 0, new Paint());

What does PorterDuff.Mode mean in android graphics.What does it do?

Community
  • 1
  • 1
Savyasachi
  • 151
  • 1
  • 5