[笔记]自定义View -- 一个简单扇形图的实现思路

效果图

[笔记]自定义View -- 一个简单扇形图的实现思路_第1张图片
效果图
public class MView extends View {

    private Paint mPaint = new Paint();//设置画笔

    private int[] mColors = {0xFFCCFF00, 0xFF6495ED, 0xFFE32636,
            0xFFE6B800, 0xFF7cFC00};//设置区块颜色

    private float mStartAngle = 0;//起始角度

    private ArrayList mPieDatas;//数据源

    private int mWidth, mHeight;//屏幕高度、宽度

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

    public MView(Context context) {
        this(context, null);
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        mPaint.setAntiAlias(true);//抗锯齿
        mPaint.setStyle(Paint.Style.FILL);//画笔样式,填充

    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;//获取真实宽度
        mHeight = h;//获取真实高度
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (null == mPieDatas)
            return;
        //起始角度
        float currentStartAngle = mStartAngle;
        //将圆中心,位移到屏幕中间
        canvas.translate(mWidth / 2, mHeight / 2);
        //通过取最小值,通过计算获取圆半径
        float r = (float) (Math.min(mWidth, mHeight) / 2 * 0.8);
        //设置区域
        RectF rectF = new RectF(-r, -r, r, r);
        for (int i = 0; i < mPieDatas.size(); i++) {
            PieData pieData = mPieDatas.get(i);
            //设置画笔原色
            mPaint.setColor(pieData.getColor());
            //画弧
            canvas.drawArc(rectF, currentStartAngle, pieData.getAngle(), true, mPaint);
            //开始角度累加
            currentStartAngle += pieData.getAngle();
        }

    }

    //设置起始弧度
    public void setStartAngle(int mStartAngle) {
        this.mStartAngle = mStartAngle;
        invalidate();
    }

    public void setPieDatas(ArrayList pieDatas) {
        this.mPieDatas = pieDatas;
        initData(pieDatas);
        invalidate();
    }

    private void initData(ArrayList pieDatas) {
        if (pieDatas == null && pieDatas.size() == 0) {
            return;
        }
        //设置数据源当中所以数值的综合
        float sumValue = 0;
        for (int i = 0; i < pieDatas.size(); i++) {
            PieData pie = pieDatas.get(i);
            sumValue += pie.getValue();
            int j = i % mColors.length;
            pie.setColor(mColors[j]);//设置弧的颜色
        }

        float sumAngle = 0;//这是角度值
        for (int i = 0; i < mPieDatas.size(); i++) {
            PieData pie = mPieDatas.get(i);
            //设置百分比
            float percentage = pie.getValue() / sumValue;
            //获取所占角度值
            float angle = percentage * 360;
            //赋值
            pie.setPercentage(percentage);
            pie.setAngle(angle);
            sumAngle += angle;
        }

    }


    public static class PieData {
        private String name;
        private float value;
        private float percentage;//百分比

        private int color = 0;
        private float angle = 0;

        public PieData(String name, float value) {
            this.name = name;
            this.value = value;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }

        public float getAngle() {
            return angle;
        }

        public void setAngle(float angle) {
            this.angle = angle;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public float getValue() {
            return value;
        }

        public void setValue(float value) {
            this.value = value;
        }

        public float getPercentage() {
            return percentage;
        }

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

你可能感兴趣的:([笔记]自定义View -- 一个简单扇形图的实现思路)