简体   繁体   English

在android圈内绘制文字

[英]Draw text inside android circle

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

How can I draw a text inside the arc? 如何在弧内绘制文本? I get IndexOutOfBoundException in drawtext... What parameter shall i pass? 我在drawtext中得到IndexOutOfBoundException ...我应该传递什么参数? how can i get it inside the arc? 我怎么能在弧内得到它?

You could try this. 你可以试试这个。

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

Note : (-3,15) is the starting co-ordinates to draw the text and (+5) is the padding. 注意:( - 3,15)是绘制文本的起始坐标,(+5)是填充。

This should give you an output like the notification badge here -> 这应该给你一个像通知徽章这样的输出 - > 样品

Idea: Use a simple TextView component. 想法:使用简单的TextView组件。

Then create this custom shape: 然后创建此自定义形状:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    <corners android:radius="10dip"/>
    <stroke android:color="@android:color/black" android:width="2dip"/>
    <solid android:color="@android:color/transparent"/>
</shape>

And finally set this shape to be the background of your textview. 最后将此形状设置为textview的背景。

For more suggestions check this link: Android draw circle around Text 有关更多建议,请查看此链接: Android围绕文本绘制圆圈

I had a similar requirement where needed text drawn in the centre of a circle. 我有一个类似的要求,在圆圈的中心绘制所需的文字。

Here's how I did it (within my customview's onDraw ) albeit it similar to @Anoop's answer. 这是我如何做到的(在我的customview的onDraw ),尽管它类似于@Anoop的回答。

//draw circle
canvas.drawCircle(xPos,yPos,mCircleSize, mCirclePaint);

mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

//this ensures X alignment
mTextPaint.setTextAlign(Paint.Align.CENTER);

// Measure the text rectangle to get the height        
Rect result = new Rect();
mTextPaint.getTextBounds(marker.label, 0, marker.label.length(), result);
//take half the height as the offset
int yOffset = result.height()/2;

//add offset to ensure Y is aligned center
canvas.drawText(marker.label, xPos, yPos+yOffset, mTextPaint);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM