向视图添加一些文本

时间:2018-06-12 06:05:48

标签: android animation view

我正在使用视图绘制一个圆圈,并且还为该圆圈设置了动画。现在我想在该圆圈中添加一个带有计数器的文本。为了绘制圆圈,我编写了以下代码。

Circle.java

input.next

我正在制作这种观点。现在我想在我创建的圆圈中添加一个textview。 我尝试使用

  public class Circle extends View {

private static final int START_ANGLE_POINT = 30;

private final Paint paint;
private final RectF rect;
private float angle;

public Circle(Context context, AttributeSet attrs) {
    super(context, attrs);
    final int strokeWidth = 5;
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(strokeWidth);
    paint.setColor(Color.RED);
    rect = new RectF(strokeWidth, strokeWidth, 100 + strokeWidth, 100 + strokeWidth);
    angle = 30;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawArc(rect, START_ANGLE_POINT, angle, false, paint);
}

public float getAngle() {
    return angle;
}

public void setAngle(float angle) {
    this.angle = angle;
}
}

但它没用。请帮我将textview添加到此循环视图中。

2 个答案:

答案 0 :(得分:1)

试试这个

public class Circle extends View {

    private static final int START_ANGLE_POINT = 30;

    private final Paint paint;
    private final RectF rect;
    private float angle;

    public Circle(Context context, AttributeSet attrs) {
        super(context, attrs);
        final int strokeWidth = 5;
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(strokeWidth);

        paint.setTextSize(30);
//        new Canvas().drawText("10", 5, 10,15, 30, paint);
        rect = new RectF(strokeWidth, strokeWidth, 100 + strokeWidth, 100 + strokeWidth);
        angle = 30;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLUE);
        canvas.drawArc(rect, START_ANGLE_POINT, angle, false, paint);
//        canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2, 200, paint);
        paint.setColor(Color.BLACK);
        paint.setTextAlign(Paint.Align.CENTER);

        canvas.drawText("10", 15, 30  , paint);

    }

    public float getAngle() {
        return angle;
    }

    public void setAngle(float angle) {
        this.angle = angle;
    }
}

答案 1 :(得分:0)

 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

 paint.setColor(Color.rgb(110, 110, 110));

 paint.setTextSize(whiteKeyTextSize);

 canvas.drawText(mText, start, end,x, y, paint);