自定义饼状图

自定义ViewGroup基本步骤

  • 写构造方法
  • 写onLayout 确定子View的位置
  • onMesure
    根据子View的布局文件确定子View的大小,及位置 测量分为三种
    EXACTLY 300dp matchParent
    AT_MOST wrapContent
    UNSPECIFIED 很少用
@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
    }
  • 得到ViewGroup的parameter view.getLayoutParameter
    LineLayout.LayoutParameters等

  • onDraw()

先看一个圆饼图 主要就这么一个API
canvas.drawArc(mRectF ,start ,sweepAngle ,true ,mPaint);
看效果图
这里写图片描述
先准备一个bean类

public class PieDataEntity {
    private String name;
    private float value;
    private float percent;
    private int color = 0;
    private float angle = 0;

    public PieDataEntity(String name, float value, int color) {
        this.name = name;
        this.value = value;
        this.color = color;
    }

    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 getpercent() {
        return percent;
    }

    public void setpercent(float percent) {
        this.percent = percent;
    }

    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 class PieChartView extends View{
    private Paint mPaint;
    private float mRadius;
    private RectF mRectF ;
    private int width ;
    private int height ;
    private List<PieDataEntity> mPieDataEntityList ;
    public PieChartView(Context context) {
        super(context);
        init(context);
    }

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

    public PieChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }

    private void init(Context context){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mRectF = new RectF();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawPie(canvas);
    }

    private void drawPie(Canvas canvas) {
        if (mPieDataEntityList == null){
            return;
        }

        width = getMeasuredWidth()- getPaddingLeft() - getPaddingRight() ;
        height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom() ;
        mRadius = (float) (Math.min(width,height)/2*0.8);
        mRectF.left = (width - 2*mRadius)/2 ;
        mRectF.top = (height - 2*mRadius)/2 ;
        mRectF.right = mRectF.left + 2* mRadius ;
        mRectF.bottom = mRectF.top + 2* mRadius ;

        float start = 180 ;
        for (int i = 0 ; i< mPieDataEntityList.size();i++){
            float sweepAngle = mPieDataEntityList.get(i).getValue()/mTotalValue*360-1;//每个扇形的角度
            mPaint.setColor(mPieDataEntityList.get(i).getColor());

            canvas.drawArc(mRectF ,start ,sweepAngle ,true ,mPaint);
            start += sweepAngle+1 ;
        }
    }

    private int mTotalValue ;
    public void setData(List<PieDataEntity> pieList){
        mPieDataEntityList = pieList ;
        for (int i = 0 ; i< mPieDataEntityList.size() ; i++){
            mTotalValue += mPieDataEntityList.get(i).getValue() ;
        }
        invalidate();
    }
}

使用很简单

int [] colors = {R.color.red_color ,R.color.colorPrimary,R.color.colorAccent,R.color.green};
        List<PieDataEntity> entities = new ArrayList<>();
        for (int i = 0 ; i< 10 ;i ++){
            PieDataEntity pieDataEntity = new PieDataEntity("hhh"+i,i+5,getResources().getColor(colors[i% colors.length]));
            entities.add(pieDataEntity);
        }
        pieChart.setData(entities);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值