Android 使用Canvas绘制饼图

转载请注明出处,点击此处 查看更多精彩内容。

效果:
绘制饼图

嗯,一个很简单的饼图绘制。

用法

  • 可以在xml文件中配置,也可以直接new一个实例出来。
<com.paoword.oa.view.SectorGraphView
            android:id="@+id/sector_graph"
            android:layout_width="120dp"
            android:layout_height="120dp" />
  • 配置各区域的颜色和比例
mSectorGraph.setProportion(0.2f, 0.45f, 0.05f, 0.3f)
                .setColor(Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW)
                .draw();

setProportion()setColor()的参数都是不定长参数,但是要注意两个方法的参数个数要相同,不然就抛异常。
setProportion()的参数为0~1的float类型,但是各参数值加到一起要等于1,否则画出的饼图就不准确

主代码

SectorGraphView.java

package com.paoword.oa.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by shichaohui on 16/1/16.
 * <p>
 * 饼图
 */
public class SectorGraphView extends View {

    private int[] colors;
    private float[] proportions;

    private Paint mPaint;
    private Bitmap bitmap;
    private Canvas mCanvas;
    private RectF mRectf;

    private int startAngle = -90, endAngle = 270;
    private int[] colorStartAngles;

    public SectorGraphView(Context context) {
        super(context);
    }

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

    public SectorGraphView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SectorGraphView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    /**
     * 设置饼图每一块的颜色
     *
     * @param color 颜色
     */
    public SectorGraphView setColor(int... color) {
        colors = color.clone();
        return this;
    }

    /**
     * 设置饼图每一块的比例
     *
     * @param proportion 比例(0-1)
     */
    public SectorGraphView setProportion(float... proportion) {
        proportions = proportion.clone();
        return this;
    }

    /**
     * 绘制饼图
     */
    public void draw() {
        if (colors.length != proportions.length) {
            throw new IllegalArgumentException("color和proportion的数量不同!");
        } else {
            post(() -> {
                mPaint = new Paint();
                mPaint.setAntiAlias(true);

                mRectf = new RectF(0, 0, getWidth(), getHeight());

                colorStartAngles = new int[colors.length];
                colorStartAngles[0] = startAngle;
                for (int i = 1; i < colors.length; i++) {
                    colorStartAngles[i] = colorStartAngles[i - 1] + (int) (360 * proportions[i - 1]);
                }

                bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
                mCanvas = new Canvas(bitmap);

                logic();

            });
        }
    }

    private void logic() {
        new Thread(() -> {
            while (startAngle <= endAngle) {
                try {
                    Thread.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < colorStartAngles.length; i++) {
                    if (startAngle == colorStartAngles[i]) {
                        mPaint.setColor(colors[i]);
                    }
                }
                mCanvas.drawArc(mRectf, startAngle, 1, true, mPaint);
                postInvalidate();
                startAngle++;
            }
        }).start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mPaint == null) return;
        canvas.drawBitmap(bitmap, 0, 0, mPaint);
    }

}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值