简单死数据饼图

效果图:

简单死数据饼图_第1张图片

CustomPiechartView继承View

package com.example.piechart.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class CustomPiechartView extends View {

    /**
     * 记录传入的角度
     * @param context
     */
    float[] mAngles;

    /**
     * 位置控制
     * @param context
     */
    RectF mRectF;

    /**
     * 画笔
     * @param context
     */
    Paint mPaint;

    /**
     * 起始角度
     * @param context
     */
    float mStartAngles=0;

    public CustomPiechartView(Context context) {
        super(context);
        init();
    }



    public CustomPiechartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * 设置每个扇形的角度
     * @param angles 角度数组
     */
    public void setData(float[] angles){
        mAngles=angles;
    }
    private void init() {
        //初始化画笔
        mPaint=new Paint();
        //设置画笔颜色为蓝色
        mPaint.setColor(Color.BLUE);
        //设置宽度
        mPaint.setStrokeWidth(20);
        //设置填充模式
        mPaint.setStyle(Paint.Style.FILL);

        //设置上下左右位置
        mRectF=new RectF();
        mRectF.top=100;
        mRectF.bottom=400;
        mRectF.right=550;
        mRectF.left=150;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i=0;i

MainActivity

package com.example.piechart;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.piechart.view.CustomPiechartView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        float[] angles = new float[]{80, 60, 100, 50, 30, 40};
        CustomPiechartView customBingView = findViewById(R.id.piechart);
        customBingView.setData(angles);
        customBingView.invalidate();
    }
}

布局




    


你可能感兴趣的:(练习开发)