Android中有多个按钮的自定义圈子

时间:2017-10-24 01:01:51

标签: java android android-layout

我是一个崭露头角的Android开发人员。我想知道如何绘制一个带有厚边界环的空心圆,并在厚边界中嵌入选项。选项数量取决于用户选择的数量(1-10)。但是,我不知道如何跟踪屏幕上的信息,例如边界中选项的坐标具体

您也可以告诉我如何在将来自行修复这些自定义查询,因为文档仅供基本参考。

谢谢

圈子示例(选项数量不是静态但可变的)enter image description here

2 个答案:

答案 0 :(得分:0)

使用带坐标的MappedImage,创建mappedimage

答案 1 :(得分:0)

您需要使用画布绘制圆圈

public class DountChart extends View {

    private int ScrWidth, ScrHeight;

    //The percentage of presentation, in actual use, is the external scale parameter
    private final float arrPer[] = new float[]{10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f, 10f};
    //RGB array of colors
    private final int arrColorRgb[][] = {{77, 83, 97},
            {148, 159, 181},
            {77, 83, 97},
            {148, 159, 181}, {77, 83, 97},
            {148, 159, 181}, {77, 83, 97},
            {148, 159, 181}, {77, 83, 97},
            {148, 159, 181}};

    public PanelDountChart2(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

        //The screen information
        DisplayMetrics dm = getResources().getDisplayMetrics();
        ScrHeight = dm.heightPixels;
        ScrWidth = dm.widthPixels;
    }

    public void onDraw(Canvas canvas) {
        //Canvas background
        canvas.drawColor(Color.WHITE);

        float cirX = ScrWidth / 2;
        float cirY = ScrHeight / 3;
        float radius = ScrHeight / 5;//150;

        float arcLeft = cirX - radius;
        float arcTop = cirY - radius;
        float arcRight = cirX + radius;
        float arcBottom = cirY + radius;
        RectF arcRF0 = new RectF(arcLeft, arcTop, arcRight, arcBottom);

        //Brush the initialization
        Paint PaintArc = new Paint();
        Paint PaintLabel = new Paint();
        PaintLabel.setColor(Color.WHITE);
        PaintLabel.setTextSize(16);

        //Position calculation
        XChartCalc xcalc = new XChartCalc();

        float Percentage = 0.0f;
        float CurrPer = 0.0f;
        int i = 0;
        for (i = 0; i < arrPer.length; i++) {
            //The percentage of conversion as a pie chart display angle
            Percentage = 360 * (arrPer[i] / 100);
            Percentage = (float) (Math.round(Percentage * 100)) / 100;
            //The distribution of color
            PaintArc.setARGB(255, arrColorRgb[i][0], arrColorRgb[i][1], arrColorRgb[i][2]);

            //Display the proportion in a pie chart
            canvas.drawArc(arcRF0, CurrPer, Percentage, true, PaintArc);
            //Calculating the percentage of label
            xcalc.CalcArcEndPointXY(cirX, cirY, radius - radius / 2 / 2, CurrPer + Percentage / 2);
            //Identification
            canvas.drawText(Float.toString(arrPer[i]) + "%", xcalc.getPosX(), xcalc.getPosY(), PaintLabel);
            //The starting point of the next
            CurrPer += Percentage;
        }

        //Painting circle
        PaintArc.setColor(Color.WHITE);
        canvas.drawCircle(cirX, cirY, radius / 2, PaintArc);

    }

}