Make Your ProgressBar Smoother
If you use the android Gmail application, you probably noticed that the progress bar is a bit customized. I am not talking about the pull to resfresh but the indeterminate progress bar which appears just after. This indeterminate drawable is much smoother than the usual.
I will show you in this post a way to reproduce this smooth indeterminate horizontal progress bar. Here is the result: the first progress bar uses the default indeterminate drawable while the others are all custom.
There is apparently a problem with the size of the video, so you might want to click on this link to see it on Youtube.
How does the default animation work
First of all, you will need to use the Horizontal progress bar style:
Widget.ProgressBar.Horizontal for pre ICS and
Widget.Holo.ProgressBar.Horizontal for post ICS devices. This gives us two important parameters:
The default indeterminate drawable looks like this:
1 2 3 4 5 6 7 8 9 10
It’s a simple animation drawable. That’s why we cannot have the smoothness we want. We just have to make a custom indeterminate drawable which dynamically draws lines instead of drawing bitmaps.
The custom indeterminate drawable
Here is my original idea (don’t be impressed by my graphic skills):
Basically we have some kind of exponential function we use to compute the length of each part of the drawable. In fact, the code is even simpler than that. We just have to loop and draw lines bigger than the previous one. You also have to take the offset in account, as this value will let you to have an animation.
Note: My math skills are a bit poor, so I might be saying some stupid stuff, don’t hesitate to comment :)
Here is the code I used.
1 2 3 4 5 6 7
I used the expm1 function (which returns exp(x) – 1) but you can try any growing function. We could imagine a function which multiply by two the previous part each time, or some kind of fibonacci sequence.
Here is the result for the code above:
I could stop here but I wanted to go further and give to our drawable the possibility to be fully customizable.
I’ve always loved the relation between these curves and animations. It allows developers to radically change their animation in just one line. You can make it bounce, overshoot,… just by changing the interpolator. I used interpolators here to change the way the animation looks like.
A chart is better than words:
Well. Seeing this, code would be better than my chart. Sorry about that.
Basically, we set a number of sections. The interpolator will give us the ratio of each section’s length. The animation is done by an offset, incremented at each frame.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
Here is the result with some basic interpolators:
Sorry about the lag in the gif, a sample apk will be available soon, so stay tuned ;)
Limitations: Your interpolator must be a monotonic function!
I made a small lib available on Github! do not hesitate to fork it and make pull requests!