Android – draw text on bitmap
•
Android
I'm trying to put text on the map marker, but it always appears below it. First, I convert drawable to bitmap, and then draw text on it. The conversion effect from drawable to bitmap is very good. I only have the problem of text coverage
I've tried these:
> Adding text to a bitmap in memory in Android > https://stackoverflow.com/a/7328777/3423468 > https://stackoverflow.com/a/8831182/3423468
There are more unlucky ones
This is my current approach:
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;
}
What do you think I did wrong?
resolvent:
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern
Here, you use Src_ Over, which means that the source will be below the DST. DST is the new pixel to be drawn
You should use DST_ Over draws a new pixel over the old one
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER)); // Text Overlapping Pattern
Here's an overview of how porterduff works
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码