在android圈子内绘制文本 [英] Draw text inside android circle

查看:56
本文介绍了在android圈子内绘制文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
}

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

How can I draw a text inside the arc? I get IndexOutOfBoundException in drawtext... What parameter shall i pass? how can i get it inside the arc?

推荐答案

您可以尝试一下.

    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)是填充文本.

Note : (-3,15) is the starting co-ordinates to draw the text and (+5) is the padding.

这应该给您类似通知徽章的输出->

This should give you an output like the notification badge here ->

这篇关于在android圈子内绘制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆