gpt4 book ai didi

android - 在android圆圈内绘制文字

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:59 25 4
gpt4 key购买 nike

public static Bitmap drawCircle(int width,int height, int borderWidth) {
Bitmap canvasBitmap = Bitmap.createBitmap( 350, 350, Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(canvasBitmap, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
paint.setShader(null);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(borderWidth);

Paint paint1 = new Paint();
paint1.setAntiAlias(true);
paint1.setShader(shader);
paint1.setShader(null);
paint1.setStyle(Paint.Style.STROKE);
paint1.setColor(Color.WHITE);
paint1.setStrokeWidth(borderWidth);
Canvas canvas = new Canvas(canvasBitmap);
float radius = width > height ? ((float) height) / 2f : ((float) width) / 2f;
//canvas.drawCircle(width / 2, height / 2, radius - borderWidth / 2, paint);
final RectF rect = new RectF();
rect.set(100, 100, 300, 300);
canvas.drawArc(rect, 270, 90, false, paint1);
canvas.drawText("25%", 100, 100, 100, 100, paint1);
return canvasBitmap;
}

如何在圆弧内绘制文本?我在 drawtext 中得到 IndexOutOfBoundException...我应该传递什么参数?我怎样才能让它进入弧线?

最佳答案

你可以试试这个。

    private Paint paint;
private Paint circlePaint;

paint = new Paint();
circlePaint = new Paint();

paint.setColor(Color.WHITE);
paint.setTextSize(18f);
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.CENTER);

Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);

circlePaint.setColor(Color.RED);
circlePaint.setAntiAlias(true);

canvas.drawCircle(-3, 15 - (bounds.height() / 2), bounds.width() + 5, circlePaint);

canvas.drawText(text, -3, 15, paint);

注意:(-3,15) 是绘制文本的起始坐标,(+5) 是填充。

这应该会为您提供类似通知标志的输出 -> sample

关于android - 在android圆圈内绘制文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22224384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com