天天看点

531_自定义饼图

自定义饼图

public class CirclePie extends View {

    private Context context;

    private Paint paint;

    private int strokeWidth;

    private int halfStrokeWidth;

    private int width;

    private int height;

    public CirclePie(Context context) {

        this(context, null);

    }

    public CirclePie(Context context, AttributeSet attrs) {

        this(context, attrs, 0);

    }

    public AuthDataCirclePie(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

        this.context = context;

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        paint.setStyle(Paint.Style.STROKE);

        paint.setColor(ColorUtil.getColor(R.color.color_single_2d449f));

    }

    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {

            int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);

            int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

            width = sizeWidth;

            height = sizeHeight;

        } else {

            width = dp300;

            height = dp300;

        }

        setMeasuredDimension(width, height);

        strokeWidth = (int) (width / 2 * 0.6);

        halfStrokeWidth = strokeWidth / 2;

        paint.setStrokeWidth(strokeWidth);

    }

    @Override

    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        RectF rectF = new RectF(halfStrokeWidth, halfStrokeWidth, width - halfStrokeWidth, height - halfStrokeWidth);

        canvas.drawArc(rectF, 0, 90, false, paint);

    }

}

继续阅读