我正在尝试使用 Paint 根据其大小向我的文本添加椭圆形背景。我的代码中有一些问题。
到目前为止,这是我的代码:
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(500, 280, conf);
Canvas canvas1 = new Canvas(bmp);
Paint color = new Paint();
color.setTextSize(30);
color.setColor(Color.BLACK);
Paint clr2=new Paint();
clr2.setColor(Color.WHITE);
canvas1.translate(200/2f,100/2f);
canvas1.drawCircle(50,0, 50, clr2);
canvas1.drawText(new Random().nextInt()+" $", 0, 0, color);`
当前成绩:
预期结果:
如果要绘制圆角矩形:
Canvas.drawRoundRect(RectF(),10f,10f,Paint())
对于每个文本,你可以得到文本绑定(一个矩形一个圆形文本),然后用圆角绘制这个矩形:
String text = "hello";
Rect rectText = new Rect();
paint.getTextBounds(text, 0, text.length(), rectText);
canvas.drawRoundRect(rectText.left,rectText.top,rectText.right,rectText.bottom,radius,radius,paint);