The ProgressBar works by changing the level of the associated drawable. In doRefreshProgress()
:
final int level = (int) (scale * MAX_LEVEL);
(progressDrawable != null ? progressDrawable : d).setLevel(level);
A Drawable's level is, basically, an integer number that may have different meanings for different kinds of Drawable
subclasses.
This allows a drawable to vary its imagery based on a continuous
controller, for example to show progress or volume level.
Returns true if this change in level has caused the appearance of the Drawable to change (hence requiring an invalidate), otherwise returns false.
In particular, a GradientDrawable
(with useLevel="true"
, such as this one) uses the level value to know which fraction of the drawable should be drawn. For example, for a left-to-right linear gradient, the rectangle is calculated as:
final float level = st.mUseLevel ? (float) getLevel() / 10000.0f : 1.0f;
x0 = r.left; y0 = r.top;
x1 = level * r.right; y1 = y0;
In the case of a ring gradient such as this one, the level determines what fraction of the total 360 deg angle should be drawn:
float sweep = st.mUseLevelForShape ? (360.0f * getLevel() / 10000.0f) : 360f;
In short, as the setProgress()
method is called, this value changes, and the ring is progressively filled.
As for the RotateDrawable
, it uses the same exact mechanism to rotate the progress_particle
bitmap (which is just a transparent square with a white dot at 90 deg) around its center:
mState.mCurrentDegrees = mState.mFromDegrees +
(mState.mToDegrees - mState.mFromDegrees) * ((float) level / MAX_LEVEL);
Finally, about the "how does it know how to take only the ring shape from 0 degrees on the right" part, that's just a matter of convention. Ring GradientDrawables start from the right. The first lines in the ring path calculation are:
// inner top
ringPath.moveTo(x + radius, y);
// outer top
ringPath.lineTo(x + radius + thickness, y);