[英]Adding text to a bitmap with canvas
我有一些將文本添加到位圖的代碼。 除非文本寬於原始圖像,否則此方法效果很好。
如何使圖像足夠寬以使文本正確顯示?
這是我的代碼:
private BitmapDescriptor GetCustomBitmapDescriptor(string text)
{
Bitmap baseBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.Icon);
Bitmap bitmap = baseBitmap.Copy(Bitmap.Config.Argb8888, true);
Paint paint = new Paint(PaintFlags.AntiAlias);
Rect bounds = new Rect();
paint.GetTextBounds(text, 0, text.Length, bounds);
float bitmapMiddle = bitmap.Width / 2.0f;
Canvas canvas = new Canvas(bitmap);
canvas.DrawText(text, bitmapMiddle - (bounds.Right / 2.0f), bitmap.Height, paint);
BitmapDescriptor icon = BitmapDescriptorFactory.FromBitmap(bitmap);
return (icon);
}
提前致謝。
只是顏色代碼,它正在工作
私有位圖GetCustomBitmapDescriptor(String text){位圖baseBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launche); 位圖位圖= baseBitmap.copy(Bitmap.Config.ARGB_8888,true); Paint paint =新的Paint(Paint.ANTI_ALIAS_FLAG); 矩形邊界=新的Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
float bitmapMiddle = bitmap.getWidth() / 2.0f;
Canvas canvas = new Canvas(bitmap);
paint.setColor(Color.RED);
canvas.drawText(text, bitmapMiddle - (bounds.right / 2.0f), bitmap.getHeight(), paint);
return bitmap;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.