Android 绘制饼图

本文介绍了如何在Android中实现一个自定义的饼图组件,包括如何确定饼图中心点、计算半径、设置起始角度和绘制文本,详细阐述了绘制饼图的步骤,并提供了相关代码片段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:

 1. 将一个圆形6等分的饼图,在每个饼图的中心显示当前饼图代表的含义

绘制步骤:

1. 画布中心点: centerX = getWidht() / 2; centerY = getWidth() / 2;

2. 原型的半径, 定义为高度的4分之一, radius = getHeight() / 4 > getWidth() /2 ? getWidth() /2 : getHeight() /4;

3. 由于绘制弧度图时是从相对Y轴90度开始的,定义开始角度为 startAngle, 绘制的角度为 sweepAngle, 弧度图中心点的位置用来显示数字,则其中心点相对于Y轴角度为 textAngle = 90 + startAngle + sweepAngle / 2;

4. 计算绘制文本的坐标, textX = centerX + radius * Math.sin(Math.toRadians(textAngle); 

                                       textY = centerY - radius * Math.sin(Math.toRadians(textAngle);

 

具体代码片段如下:

private float[] calculatePosition(float centerX, float centerY, float radius, float degree) {
    //由于Math.sin(double a)中参数a不是度数而是弧度,所以需要将度数转化为弧度
    //而Math.toRadians(degree)的作用就是将度数转化为弧度
    //sin 一二正,三四负 sin(180-a)=sin(a)
    //扇形弧线中心点距离圆心的x坐标
    float x = (float) (Math.sin(Math.toRadians(degree)) * radius);
    //cos 一四正,二三负
    //扇形弧线中心点距离圆心的y坐标
    float y = (float) (Math.cos(Math.toRadians(degree)) * radius);
    //每段弧度的中心坐标(扇形弧线中心点相对于view的坐标)
    float startX = centerX + x * 2/ 3;
    float startY = centerY - y * 2/ 3;
    float[] position = new float[2];
    position[0] = startX;
    position[1] = startY;
    return position;
}

private void drawPie(Canvas canvas) {
    float centerX = getWidth() / 2F;
    float centerY = getHeight() / 2F;
    float radius = getHeight() / 4 > getWidth() / 2 ? getWidth() / 2 : getHeight() / 4;
    RectF oval = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);

    canvas.drawRect(oval,mPaint);

    for (int i = 0; i < 6; ++i) {

        float startAngle = i * 60;
        float sweepAngle = 60;

        canvas.drawArc(oval, startAngle, sweepAngle, true, mPaint);
        float textAngle = 90 + startAngle + sweepAngle / 2;
        float[] textPos = calculatePosition(centerX, centerY, radius, textAngle);
        mPaint.setTextSize(30F);
        canvas.drawText(String.valueOf(i + 1), textPos[0], textPos[1], mPaint);

        Paint p = createPaint(Color.WHITE);
        p.setStyle(Paint.Style.FILL);
        canvas.drawCircle(centerX, centerY, radius /3, p);

        Paint p1 = createPaint(Color.LTGRAY);
        p.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(centerX, centerY, radius / 3, p1);
    }

}

 

效果如下图:

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值