Android自定义控件横向柱状统计图
程序员文章站
2022-04-09 17:08:31
本文实例为大家分享了android实现横向柱状统计图的具体代码,供大家参考,具体内容如下碰到一个项目需要用到统计图功能,比较简单就自定义写了一个。没有写过多的样式和功能,仅有简单的横向柱状统计图。传入...
本文实例为大家分享了android实现横向柱状统计图的具体代码,供大家参考,具体内容如下
碰到一个项目需要用到统计图功能,比较简单就自定义写了一个。没有写过多的样式和功能,仅有简单的横向柱状统计图。
传入数据后大致样式如下:
/**横向柱状统计图 * created by administrator on 2018/1/16 0016. */ public class horizontalchartview extends view { /** * 间隔线画笔 */ private paint paint; /** * 线的颜色 */ private int color_line = color.rgb(230, 230, 230); /** * 字的颜色 */ private int color_font = color.rgb(51, 51, 51); /** * 比例图颜色 */ private int color_plan = color.rgb(22, 85, 164); /** * 比例图画笔 */ private paint paint_plan; /** * 比例图高度 */ private int plan_height; /** * 初始化比例 */ private float[] ratio = {0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}; /** * 文字画笔1 */ private paint paint_font; /** * 文字画笔2 */ private paint paint_font2; /** * 线的条数 */ private int line_num = 11; /** * 比例数 */ private string ratio_num = "0"; /** * 月份 */ private string month_num = "1月"; public horizontalchartview(context context) { super(context); } public horizontalchartview(context context, @nullable attributeset attrs) { super(context, attrs); init(context, attrs); } public horizontalchartview(context context, @nullable attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(context, attrs); } @requiresapi(api = build.version_codes.lollipop) public horizontalchartview(context context, @nullable attributeset attrs, int defstyleattr, int defstyleres) { super(context, attrs, defstyleattr, defstyleres); init(context, attrs); } /** * 初始化 * * @param context * @param attrs */ public void init(context context, attributeset attrs) { paint = new paint(); paint.setcolor(color_line); paint_plan = new paint(); paint_plan.setcolor(color_plan); paint_font = new paint(); paint_font.setcolor(color_font); paint_font.settextsize(densityutils.dp2px(context, 12)); paint_font.setantialias(true); paint_font.settextalign(paint.align.center); paint_font2 = new paint(); paint_font2.setcolor(color_font); paint_font2.settextsize(densityutils.dp2px(context, 12)); paint_font2.setantialias(true); paint_font2.settextalign(paint.align.right); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); int width = getwidth(); int height = getheight(); int lift_width = (int) (width * 0.15); int line_width = (int) (width * 0.78); //获取底部文字信息 paint.fontmetrics fm = paint_font.getfontmetrics(); int line_length = (int) (height - (fm.bottom - fm.top) - 4); plan_height = (int) (line_length / 12 * 0.3); for (int i = 0; i < line_num; i++) { canvas.save(); if (i == 0) { ratio_num = "0"; } else { ratio_num = i + "0"; } //底部百分比数字 canvas.drawtext(ratio_num, lift_width + i * line_width / 10, height - 10, paint_font); //网络线 canvas.drawline(lift_width + i * line_width / 10, 0, lift_width + i * line_width / 10, line_length, paint); canvas.restore(); } //获取月份文字信息 paint.fontmetrics fm1 = paint_font2.getfontmetrics(); for (int n = 12; n > 0; n--) { canvas.save(); month_num = n + "月"; //左侧月份 canvas.drawtext(month_num, lift_width / 4 * 3, ((line_length / 12)) * (13 - n) - line_length / 24 + (fm1.bottom - fm1.top) / 2, paint_font2); //比例图 canvas.drawrect(lift_width, line_length / 12 * (13 - n) - (line_length / 24 + plan_height / 2) + fm1.bottom, line_width * (ratio[n - 1] / 100) + lift_width, line_length / 12 * (13 - n) - (line_length / 24 + plan_height / 2) + plan_height + fm1.bottom, paint_plan); canvas.restore(); } } /** * 传入比例信息 * * @param ratio */ public void setratio(float[] ratio) { this.ratio = ratio; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。