1

我在圆的外环上显示点时遇到问题。当一个点放置在圆的边缘之一时,该点被截去一半。截图:https ://drive.google.com/file/d/0B9BaYaKRY3v3Y2hXYkpSOE1nY28/edit?usp=sharing

我只是想让圆的半径稍微小一点,以便在它周围有一些空间,外部点有一些空间。当然,圆圈必须仍然停留在视图的中间。非常感谢有关如何执行此操作的提示。

工作代码打击完全可以接受,因为点被切断了。(感谢@Sherif elKhatib 帮助创建 2 色圈)

public class PercentView extends View {

public PercentView(Context context) {
    super(context);
    init();
}

public PercentView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public PercentView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init() {
    paint = new Paint();
    paint.setColor(Color.parseColor("#3498db"));
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    bgpaint = new Paint();
    bgpaint.setColor(Color.parseColor("#2980b9"));
    bgpaint.setAntiAlias(true);
    bgpaint.setStyle(Paint.Style.FILL);
    rect = new RectF();
    circlePaint = new Paint();
}

Paint paint;
Paint bgpaint;
RectF rect;
float percentage = 5;
Paint circlePaint;
float[] dots = new float[1000];
int dotsNum = -1;
String[] colorCode = new String[1000];

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // draw background circle anyway
    canvas.drawArc(rect, -90, 360, true, bgpaint);
    if (percentage != 0) {
        int left = 0;
        int width = getWidth();
        int top = 0;
        rect.set(left, top, left + width, top + width);
        canvas.drawArc(rect, -90, (float) (3.6 * percentage), true, paint);

        for (int i = 0; i <= dotsNum; i++) {
            circlePaint.setAntiAlias(true);
            circlePaint.setColor(Color.parseColor(colorCode[i]));
            circlePaint.setStyle(Paint.Style.FILL);
            float dotX = (float) (this.getWidth() / 2 + this.getWidth() / 2
                    * Math.cos((dots[i] * 3.6 - 90) * Math.PI / 180));
            float dotY = (float) (this.getHeight() / 2 + this.getWidth()
                    / 2 * Math.sin((dots[i] * 3.6 - 90) * Math.PI / 180));
            canvas.drawCircle(dotX, dotY, 30, circlePaint);
        }
    }
}

public void setPercentage(float inpercentage) {
    this.percentage = inpercentage;
    invalidate();
}

public void setDot(int type) {
    dotsNum++;

    switch (type) {
    case 0: 
        colorCode[dotsNum] = "#27ae60";
        break;
    case 1: 
        colorCode[dotsNum] = "#f1c40f";
        break;
    case 2: 
        colorCode[dotsNum] = "#e74c3c";
        break;
    case 3: 
        colorCode[dotsNum] = "#34495e";
        break;
    }

    dots[dotsNum] = percentage;
    invalidate();
}

}

4

1 回答 1

0

对我来说唯一一种可行的方法是那段代码“width = getWidth() - padding”。荣誉@Catherine。

于 2021-04-21T09:34:00.530 回答