android-在位图上绘制文本
作者:互联网
我正在尝试将文字放在地图标记上,但它始终显示在其下方.
首先,我将drawable转换为bitmap,然后在其上绘制文本.可绘制到位图的转换效果很好,我只有文字覆盖问题.
我已经尝试过这些:
> Adding text to a bitmap in memory in Android
> https://stackoverflow.com/a/7328777/3423468
> https://stackoverflow.com/a/8831182/3423468
还有更多没有运气的.
这是我目前的方法:
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;
}
有什么想法我做错了吗?
解决方法:
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern
在这里,您使用SRC_OVER,这意味着源将在DST下方. DST是要绘制的新像素.
您应该使用DST_OVER在旧像素之上绘制新像素.
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER)); // Text Overlapping Pattern
标签:bitmap,android-drawable,android 来源: https://codeday.me/bug/20191112/2023735.html