Android custom view implements BMI index bar

For recent projects, it is necessary to make an indicator bar of BMI index, and first put on the renderings:

The BMI index ranges from 18 to 35, and then the color of the upper indicator bar will change with the change of the offset. The number shows the current BMI index, and the following BMI standards are also judged according to the range of different values. Considering the particularity of this view, the user-defined view is used to complete it.

1. Page layout:

On the left is the BMI text, and on the right is the customized view. There is nothing to say. The following is the specific process of view:

2. Code implementation: create a new newbmiview class, inherit from the view class, and then add a construction method;

Then rewrite the onmeasure and OnDraw methods to obtain the width and height of the view. For specific settings, please refer to the relevant instructions of Hongyang great God:

https://www.oudahe.com/p/17334/

Then the focus is on the OnDraw method:

It should be noted that the color value of the small square here is the gradient value of the RGB three primary colors according to the BMI value. I can't find the color value provided by the system's own method of rendering the gradient bar, so there will be a certain error if I use this method to calculate it. Then there is the textview. Because it has its own width and height, when drawing the textview, you need to consider the width and height before drawing. Pass parameters through the set method

Finally, the following is applied in the activity:

Then the expected effect is achieved, and the code is a little messy~

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
分享
二维码
< <上一篇
下一篇>>