简体   繁体   English

在Arc CustomView中绘制文本

[英]Draw Text inside Arc CustomView

i am trying to draw text inside of arc . 我试图在arc内绘制文本。 code is as follows 代码如下

  public Round(Context context, int totalSections, int activeSections, String mText) {
    super(context);
    int dpi = context.getResources().getDisplayMetrics().densityDpi;
    float x = 0.25f;
    final float radius = x * (new Float(dpi));
    mRadius = Math.round(radius) + 20;
    mRect = new RectF(
            getWidth() + mStrokeWidth, getWidth() + mStrokeWidth, getWidth() + mRadius - mStrokeWidth, getWidth() + mRadius - mStrokeWidth
    );
    text = mText;
    mTotalSections = totalSections;
    mActiveSections = activeSections;
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setStrokeWidth(mStrokeWidth);
    mPaint.setStyle(Paint.Style.STROKE);
    mSectionDegree = 360 / mTotalSections;
    mSectionDegree -= mGap;
}


public void setmActiveSections(int mActiveSections) {
    this.mActiveSections = mActiveSections;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    float lastDegree = 270 + (mGap / 2);
    for (int i = 0; i < mTotalSections; i++) {
        if (i < mActiveSections) {
            mPaint.setColor(Color.GREEN);
        } else {
            mPaint.setColor(Color.GRAY);
        }
        canvas.drawArc(mRect, lastDegree, mSectionDegree, false, mPaint);

        lastDegree += mSectionDegree + mGap;
    }
    Paint mPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint1.setStrokeWidth(1);
    mPaint1.setStyle(Paint.Style.STROKE);
    mPaint1.setTextSize(15);
    mPaint1.setColor(getResources().getColor(R.color.red));

    canvas.drawText(text, 20,30, mPaint1);

}

how i can draw text which is centre of radius ... what happening is on different device text is not coming in centre of radius 我如何绘制以半径为中心的文本...发生在不同设备上的文本不是以半径为中心 图片

in given image i want canvas.drawtext should where Text is present 在给定的图像中,我希望canvas.drawtext应该在出现Text的位置

Add this line to your onDraw() 将此行添加到您的onDraw()

mPaint1.setTextAlign(Paint.Align.CENTER);

then the x parameter in drawText can just be the center x of the circle. 那么drawText的x参数只能是圆的中心x。 You should be able to use mRect.centerX() to get that value. 您应该能够使用mRect.centerX()获得该值。

You should use mRect.centerY() for the y value too, except it needs to be adjusted. 您还应该将mRect.centerY()用作y值,除非需要对其进行调整。 The y parameter is actually the text baseline, so if you want to center vertically as well as horizontally, you will have to check some values in the paint's FontMetrics to see how much to lower your y value so that the text looks centered vertically. y参数实际上是文本基线,因此,如果要在垂直方向和水平方向居中,则必须检查绘画的FontMetrics中的一些值,以查看降低y值的多少,以便文本在垂直方向上居中。

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

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