定制视图的Android错误笔画宽度绘制圆

问题描述 投票:1回答:1

我写在android系统的自定义视图。我想提请覆盖我认为所有的宽度和高度的圆。这是我的代码

private void init() {

    bgpaint = new Paint();
    bgpaint.setColor(bgColor);
    bgpaint.setAntiAlias(true);
    bgpaint.setStyle(Paint.Style.STROKE);
    bgpaint.setStrokeWidth(strokeWidth);
    rect = new RectF();

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // draw background circle anyway
           int strokeWidth = 50;
    rect.set(strokeWidth, strokeWidth, getwidth()- strokeWidth, 
            getheight() - strokeWidth);
    canvas.drawArc(rect, -90, 360, fill, bgpaint);

}

但是,当我运行的结果会是这样

enter image description here

我想是这样的

enter image description here

什么问题,我的代码?

android drawing android-custom-view
1个回答
1
投票

问题是在设置你的矩形坐标。你必须设置描边宽度,而不是整个行程宽度的一半。

    float left=strokeWidth / 2;
    float top=strokeWidth / 2;
    float right=minDimen - strokeWidth/ 2;
    float bottom=minDimen - strokeWidth / 2;
    rect.set(left, top, right,bottom);

因为它涉及到圈子,我想你的宽度和你circleView的高度是一样的尺寸。这是在我的代码minDimen。因为我用这两个更小的尺寸。

final int minDimen = Math.min(width, height);
setMeasuredDimension(minDimen, minDimen);  

(这必须在onMeasure方法被调用)不管怎么说,这是设置宽度和高度尺寸相同的好方法。

© www.soinside.com 2019 - 2024. All rights reserved.