100 lines of Android code to easily realize the histogram with animation

Why use an animated histogram?

Recently, a histogram was used in a project. So this article mainly talks about how to make a pillar. 100 lines of code, get the histogram!

Rounded corners with numbers on the top of the head. Well, it can also be done with drawable. However, this column has an animation, that is, when entering the interface, the column keeps growing high. In this case, it is easier to use a custom view for comprehensive consideration. The effect is shown in the following figure:

For the complete demo address, please go to my GitHub download address: https://github.com/lixiaodaoaaa/ColumnAnimViewProject

About dimensions

The control size comes directly from the settings in XML without onmeasure measurement. So use getWidth and getHeight to get the height.

About data range

If the data is displayed separately by one column, the range of the data is not very important, but the histogram is usually displayed side by side by many columns, and the unit height of these columns should be the same, so the range of setting the maximum value is provided, and the minimum value is 0

About text size of numbers

Since the width of the column is the width of the whole view, the width of the number cannot exceed the width of the column. For this reason, the size of text needs to be calculated dynamically. When the numbers 0 and 100000 are displayed, the text size is different.

About boundary values

0 is a boundary value (minimum value). When 0 is displayed, it is not the column that is not displayed, but the column with the minimum height.

About animation

Keep setting values, you will form animation. It means setting data 1 first, then setting data 2.3.4.5... Until the final display value, there will be animation effect. However, if the final value is large, the increase of 1,1,1 will be very slow and the animation time will be very long.

The complete code is as follows:

The XML configuration is as follows:

For the complete demo address, please go to my GitHub download address: https://github.com/lixiaodaoaaa/ColumnAnimViewProject

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.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>