Android custom view — implementation code of sector statistical chart
Android sector statistics
Let's see the effect first:
If you think it's OK, keep looking down!
Custom view
Define member variables
Measuring width and height
Drawing
Draw a fan
A circular statistical chart is composed of many sectors. We can calculate the angle of each sector according to the data. Note that when drawing radians, the angle increases clockwise! That is, our usual coordinates are reversed
Draw lines and text
It's mainly annoying to calculate the coordinates of each point
Here I draw a diagram. Let's compare the code with the diagram
In this way, we have completed the drawing work, and the next step is to bind data!
You just need to fill in the data with data and texts. You'd better call the invalidate () method to request redrawing (if it has been drawn).
I use an internal abstract class to implement data binding:
Reference this ArcView in the layout file, and then bind data in mainactivity:
You can set up various setter to invoke in Activity to improve ArcView's flexibility, such as setting color, radius, maximum number of blocks, and so on.
Demo download address: sectordiagram_ jb51.rar
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.