博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 饼图绘制
阅读量:6670 次
发布时间:2019-06-25

本文共 3435 字,大约阅读时间需要 11 分钟。

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[] {};
}
}

转载于:https://www.cnblogs.com/chenlong-50954265/p/3924629.html

你可能感兴趣的文章
Ubuntu 14.04安装Nginx1.60
查看>>
aaa
查看>>
详解coredump
查看>>
神奇犁头草,治疗肿毒效如神
查看>>
linux的发行版
查看>>
PHP环境配置中遇到的各种问题解决方法: Cannot load php5apache2_2.dll into server
查看>>
我的友情链接
查看>>
无意看见的几句话
查看>>
常用Linux Shell命令组合-- 运维常用总结
查看>>
XBMC 在UBUNTU 12.04中安装及设置
查看>>
解决mac下无法剪贴、复制、粘贴问题
查看>>
Oracle运维脚本
查看>>
第1部分 Windows Server2008安装和配置
查看>>
cordova环境配置
查看>>
ORA-06553: PLS-553: character set name is not recognized, while starting Content Store
查看>>
Watches OpenCart 主题模板 ABC-0088
查看>>
linux iptables 相关应用
查看>>
Linux基础
查看>>
升级到FTK 4.0.2可免费使用可视化分析模块30天
查看>>
怎样做好DNS服务器的保护
查看>>