观看大大博客照着写了个demo,注释都在代码里话不多说,上代码
public class MyAnalysisChart extends View {
private Paint mPaint = new Paint();
//颜色数组
private int[] mColors = {0xFFCCFF00, 0xFF6495ED, 0xFFE32636, 0xFF800000, 0xFF808000, 0xFFFF8C69, 0xFF808080,
0xFFE6B800, 0xFF7CFC00};
//饼状图绘制初始角度
private float mStartAngle = 0;
//数据集合
List<PieData> mData;
//宽高
private int mWidth, mHight;
public MyAnalysisChart(Context context) {
super(context, null);
}
public MyAnalysisChart(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true); //是否抗锯齿
}
//首先测量宽高
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = w;
mHight = h;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (null == mData)
return;
float currentStartAngle = mStartAngle; // 当前起始角度
canvas.translate(mWidth / 2, mHight / 2); // 将画布坐标原点移动到中心位置
float r = (float) (Math.min(mWidth, mHight) / 2 * 0.8); // 饼状图半径
RectF rect = new RectF(-r, -r, r, r); // 饼状图绘制区域
for (int i = 0; i < mData.size(); i++) {
PieData pie = mData.get(i);
mPaint.setColor(pie.getColor());
canvas.drawArc(rect, currentStartAngle, pie.getAngle(), true, mPaint);
currentStartAngle += pie.getAngle();
}
}
//设置起始角度
public void setCurrenStartAngle(int mStartAngle) {
this.mStartAngle = mStartAngle;
invalidate();//刷新
}
//设置数据
public void setData(ArrayList<PieData> mData) {
this.mData= mData;
initData(mData); //初始化数据
invalidate();//刷新
}
private void initData(ArrayList<PieData> mData) {
if (null == mData || mData.size() == 0) {
return;
}
float sumValue = 0;
for (int i = 0; i <mData.size(); i++) {
PieData pie =mData.get(i);
sumValue += pie.getValue(); //计算数值和
int j = i % mColors.length; //设置颜色
pie.setColor(mColors[j]);
}
float sumAngle = 0;
for (int i = 0; i < mData.size(); i++) {
PieData pie = mData.get(i);
float percentage = pie.getValue() / sumValue; //计算百分比
float angle = percentage * 360; //对应的角度
pie.setPercentage(percentage); //设置角度
pie.setAngle(angle); //设置角度
sumAngle += angle;
}
}
}
MainActivty插入伪数据
public class MainActivity extends AppCompatActivity {
private ArrayList<PieData> mlist = new ArrayList<PieData>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyAnalysisChart myAnalysisChart = (MyAnalysisChart) findViewById(R.id.myasc);
PieData pieData = new PieData("你好",10);
PieData pieData1 = new PieData("你好1",20);
PieData pieData2 = new PieData("你好2",30);
PieData pieData3= new PieData("你好3",40);
mlist.add(pieData);
mlist.add(pieData1);
mlist.add(pieData2);
mlist.add(pieData3);
myAnalysisChart.setData( mlist);
}
xml布局调用自定义控件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="study.chengbing.com.mystudyview.MainActivity">
<study.chengbing.com.mystudyview.MyAnalysisChart
android:id="@+id/myasc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
OK,下面贴上运行效果