简体   繁体   中英

How to draw a bar diagram on Android

I need to draw a bar diagram like the picture showing below. I am able to draw a ordinary bar diagram with filling a color on text view or some layouts. But this is a bit different. How do I draw a bar diagram with slanting bars?

在此处输入图片说明 .

Try Android canvas and custom View. You can use View's onDraw method. The method provides the canvas. You should try drawArc, drawLine, etc. If your bar view contains a textView, you must use a custom viewgroup.

Do something like this in your onDraw of View.

RectF oval = new RectF();
Paint paint  = new Paint();
paint.setColor(ContextCompat.getColor(context, R.color.color1));
paint.setStrokeWidth(widthOfArc);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path, yourPaint);
oval.set(x1,y1,x2,y2);
//eg:-  startAngle = 10, sweepAngle = 40
canvas.drawArc(oval, startAngle, sweepAngle, false, paint);

Please read about how RectF works. I will update the answer if I found any related blog/tutorial

Calculating start and end angle is pure Mathematics refer this http://www.html5canvastutorials.com/tutorials/html5-canvas-arcs/

You have to change the color and keep drawing Arc to get the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM