简体   繁体   English

如何在画布中的圆圈周围制作可用的填充空间(Android)

[英]How to make usable padding space around circle in canvas (Android)

I am having problems displaying dots on the outer ring of a circle.我在圆的外环上显示点时遇到问题。 When a dot is placed at one of the edges of the circle, the dot is half cut off .当一个点放置在圆的边缘之一时,该点被半截。 Screenshot: https://drive.google.com/file/d/0B9BaYaKRY3v3Y2hXYkpSOE1nY28/edit?usp=sharing截图: https : //drive.google.com/file/d/0B9BaYaKRY3v3Y2hXYkpSOE1nY28/edit?usp=sharing

I simply want to make the circles radius a litte smaller so there is some space around it where the outer dots have got some space.我只是想让圆的半径小一点,这样它周围就有一些空间,外面的点有一些空间。 Of course the circle must still stay in the middle of the view.当然,圆圈必须仍然停留在视图的中间。 Tipps on how to do this are very appreciated.非常感谢有关如何执行此操作的提示。

The working code blow is fully working accept for the fact that the dots are being cut off.工作代码打击完全可以接受点被切断的事实。 (Thanks to @Sherif elKhatib for the help on creating a 2 color circle) (感谢@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();
}

} }

The only one working approach for me was that piece of code "width = getWidth() - padding".对我来说唯一的一种工作方法是那段代码“width = getWidth() - padding”。 Kudos @Catherine.荣誉@凯瑟琳。

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

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