Android:canvas.drawText在圆圈边

时间:2016-08-31 08:47:23

标签: android android-canvas android-custom-view

我想在圆的两侧绘制文本,在其下面绘制另一个。我在这个answer中编辑了代码,但问题是圆和圆弧占据了矩形的整个空间,这使得文本没有空间被绘制。

这是我的代码

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // getHeight() is not reliable, use getMeasuredHeight() on first run:
    // Note: mRect will also be null after a configuration change,
    // so in this case the new measured height and width values will be used:
    if (mRect == null) {
        // take the minimum of width and height here to be on he safe side:
        centerX = getMeasuredWidth() / 2;
        centerY = getMeasuredHeight() / 2;
        radius = Math.min(centerX, centerY);

        // mRect will define the drawing space for drawArc()
        // We have to take into account the STROKE_WIDTH with drawArc() as well as drawCircle():
        // circles as well as arcs are drawn 50% outside of the bounds defined by the radius (radius for arcs is calculated from the rectangle mRect).
        // So if mRect is too large, the lines will not fit into the View
        int startTop = STROKE_WIDTH / 2;
        int startLeft = startTop;

        int endBottom = 2 * radius - startTop;
        int endRight = endBottom;

        mRect = new RectF(startTop, startLeft, endRight, endBottom);
    }


    // subtract half the stroke width from radius so the blue circle fits inside the View
    canvas.drawCircle(centerX, centerY, radius - STROKE_WIDTH / 2, mBasePaint);


    // Or draw arc from degree 192 to degree 90 like this ( 258 = (360 - 192) + 90:
    // canvas.drawArc(mRect, 192, 258, false, mBasePaint);

    // draw an arc from 90 degrees to 192 degrees (102 = 192 - 90)
    // Note that these degrees are not like mathematical degrees:
    // they are mirrored along the y-axis and so incremented clockwise (zero degrees is always on the right hand side of the x-axis)
    canvas.drawArc(mRect, 270, mTemp * 6, false, mDegreesPaint); // Each degree in the temp scale = 6 degrees on circle
    canvas.drawArc(mRect, 270 + mSeparator * 6, 3, false, mSeparatorPaint); // The separator size = 3 degrees

    // subtract stroke width from radius so the white circle does not cover the blue circle/ arc
    canvas.drawCircle(centerX, centerY, radius - STROKE_WIDTH, mCenterPaint);

    drawCenter(canvas, mTextPaint, mTemp + "°");

    canvas.drawText("Temp 1", mRect.centerX(), radius * 2, mTextPaint);
}

这段代码产生了我需要的弧形和圆形,但是我无法在圆形边上绘制度数并在其下面显示文本

任何帮助??

0 个答案:

没有答案