首页 > 代码库 > Android 饼图绘制

Android 饼图绘制

private BlurMaskFilter PaintBGBlur;
private int ScrHeight;
private int ScrWidth;
private Paint[] arrPaintArc;
private Paint PaintText = null;
private Path pathArc = null;
private RectF arcRF0 = null;
private int[] colors = new int[] { Color.RED, Color.BLUE, };
// 演示用的比例,实际使用中,即为外部传入的比例参数
private int arrPer[] = new int[] { 100, 0 };

private String typeText[] = new String[] {"已读","未读"};

private int total = 0;

 

public ChartContentView(Context context, int[] colors, int[] per, String[] typeText) {
super(context);

// 初始化数据
initData(colors, per, typeText);

initView();
}

 

/**
* 初始化页面
*/
private void initView() {
// 解决4.1版本以下canvas.drawTextOnPath()不显示问题
this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
// 屏幕信息
DisplayMetrics dm = getResources().getDisplayMetrics();
ScrHeight = dm.heightPixels;
ScrWidth = dm.widthPixels;
// 设置边缘特殊效果
PaintBGBlur = new BlurMaskFilter(1, BlurMaskFilter.Blur.INNER);

arrPaintArc = new Paint[colors.length];
for (int i = 0; i < colors.length; i++) {
arrPaintArc[i] = new Paint();
arrPaintArc[i].setColor(colors[i]);
arrPaintArc[i].setStyle(Paint.Style.FILL);
arrPaintArc[i].setStrokeWidth(4);
arrPaintArc[i].setMaskFilter(PaintBGBlur);
}

PaintText = new Paint();
PaintText.setColor(Color.BLACK);
PaintText.setTextSize(25);
PaintText.setTypeface(Typeface.DEFAULT_BOLD);

pathArc = new Path();
arcRF0 = new RectF();
}

public void onDraw(Canvas canvas) {
// 画布背景
canvas.drawColor(Color.TRANSPARENT);

if (arrPer.length > arrPaintArc.length || arrPer.length > colors.length || arrPer.length > typeText.length || arrPer.length <= 0) {
return;
}

float cirX = ScrWidth / 2;
float cirY = ScrHeight / 3;
float radius = ScrHeight / 5;

float arcLeft = cirX - radius;
float arcTop = cirY - radius;
float arcRight = cirX + radius;
float arcBottom = cirY + radius;
arcRF0.set(arcLeft, arcTop, arcRight, arcBottom);
// x,y,半径,CW为顺时针绘制
pathArc.addCircle(cirX, cirY, radius, Direction.CW);
// 绘出饼图大轮廓
canvas.drawPath(pathArc, arrPaintArc[0]);
float CurrPer = 0f; // 偏移角度
float Percentage = 0f; // 当前所占比例
int scrOffsetW = ScrWidth - 200;
int scrOffsetH = ScrHeight - 300;
int scrOffsetT = 40;

for (int i = 0; i < arrPer.length; i++) {
if (i != 0) {
// 将百分比转换为饼图显示角度
Percentage = 360 * ((float) arrPer[i] / (float) total);
Percentage = (float) (Math.round(Percentage * 100)) / 100;
// 在饼图中显示所占比例
canvas.drawArc(arcRF0, CurrPer, Percentage, true,
arrPaintArc[i]);
}

// 当前颜色
canvas.drawRect(scrOffsetW - 60, scrOffsetH + i * scrOffsetT,
scrOffsetW, scrOffsetH - 30 + i * scrOffsetT,
arrPaintArc[i]);
// 当前比例
canvas.drawText(typeText[i] + String.valueOf(arrPer[i]) + "条",
scrOffsetW, scrOffsetH + i * scrOffsetT, PaintText);

// 下次的起始角度
CurrPer += Percentage;
}

}

/**
* 改变数据,刷新画面
*
* @param colors
* @param per
* @param typeText
*/
public void changeData(int[] colors, int[] per, String[] typeText) {
initData(colors, per, typeText);

invalidate();
}

/**
* 初始化数据
*
* @param colors
* @param per
* @param typeText
*/
private void initData(int[] colors, int[] per, String[] typeText) {
if (colors != null && colors.length > 0) {
this.colors = colors;
}
else {
this.colors = new int[] {};
}
total = 0;
if (per != null && per.length > 0) {
this.arrPer = per;
for (int i = 0; i < per.length; i++) {
total += per[i];
}
}
else {
this.arrPer = new int[] {};
}
if (typeText != null && typeText.length > 0) {
this.typeText = typeText;
}
else {
this.typeText = new String[] {};
}
}