Android custom vertical seekbar multicolor progress bar
Write in front
Because there is such a scene, it is necessary to realize the multi-color progress bar in the vertical direction. Then I found it on the Internet and didn't see one that meets the needs, so I customized one. The effect is as follows:
Concrete implementation
Originally, I wanted to define the horizontal, and then rotate it. Later, I found that it was better to directly define the vertical direction, so I drew it directly in the vertical direction.
First, let's talk about the idea, which is to inherit the view, and then draw through the OnDraw () method. When drawing, you need to deal with some small details.
For example, if we need to draw a circular sliding block, then our background color band cannot occupy the whole width. Otherwise, the small round block can only be as wide as the color band, and the effect is not very good-looking. Therefore, when drawing, the width of the background painting should be less than the actual width of view.
Next, I'll post the code:
Look at the background:
Here, LinearGradient is used to realize multiple color gradients. The default initialization definition is as follows:
Then look at the operation of drawing a circle:
Here, a circle is drawn through the canvas, with inner filling and outer edge. The above process can show the effect, but it cannot be operated. We need to add events to it:
A callback interface is written here, and then we can receive the corresponding sliding progress in the activity and operate. Of course, we have to add another method to change the state of seekbar:
Here, the function is basically OK, and then we can use it in the activity. Here are the references in the layout:
How to use it is very simple:
That's it. Finally, I'd like to attach the whole code of the custom view:
end
It's really over here. Just record it, and then hope to help people in need. You can also tell me if there is a better implementation~
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.