繁体   English   中英

在Arc CustomView中绘制文本

[英]Draw Text inside Arc CustomView

我试图在arc内绘制文本。 代码如下

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

}

我如何绘制以半径为中心的文本...发生在不同设备上的文本不是以半径为中心 图片

在给定的图像中,我希望canvas.drawtext应该在出现Text的位置

将此行添加到您的onDraw()

mPaint1.setTextAlign(Paint.Align.CENTER);

那么drawText的x参数只能是圆的中心x。 您应该能够使用mRect.centerX()获得该值。

您还应该将mRect.centerY()用作y值,除非需要对其进行调整。 y参数实际上是文本基线,因此,如果要在垂直方向和水平方向居中,则必须检查绘画的FontMetrics中的一些值,以查看降低y值的多少,以便文本在垂直方向上居中。

暂无
暂无

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

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