4

I'm trying to put text over map marker but it always appears under it. First I convert drawable to bitmap and then draw text on it. drawable to bitmap conversion works fine, I only have a problem with text overlay.

enter image description here

I have already tried these:

  1. Adding text to a bitmap in memory in Android
  2. https://stackoverflow.com/a/7328777/3423468
  3. https://stackoverflow.com/a/8831182/3423468

and many more with no luck.

This is my current method:

Bitmap drawableToBitmap(Drawable drawable)
    {
        var bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        var canvas = new Canvas(bitmap);
        if (shouldDrawText)
        {
            Paint paint = new Paint();
            paint.setColor(Color.WHITE);
            paint.setStrokeWidth(40);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern

            //canvas.drawBitmap(bitmap, 0, 0, paint);
            canvas.drawText("Testing...", 10, 10, paint);
        }
        drawable.SetBounds(0, 0, canvas.Width, canvas.Height);
        drawable.Draw(canvas);
        return bitmap;
    }

Any ideas what I'm doing wrong?

Community
  • 1
  • 1
arsena
  • 1,935
  • 19
  • 36

1 Answers1

4
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern

Here you use SRC_OVER, which means the source will be over the DST will be under. The DST is the new pixels to be drawn.

You should use DST_OVER to draw the new pixels on top of the old pixels.

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER)); // Text Overlapping Pattern

See here an overview of how porterduff works enter image description here

Raymond
  • 2,276
  • 2
  • 20
  • 34