I came across Sweep Gradient on Android and was intrigued if it could give me a better control over the Color spacing and transitions. Did a quick search and found almost nothing about it!!
The only few references I found (and they do not meet the criteria of coming even close to explaining it properly or comprehensively) are listed below :
- Sweep Gradient on Android (which only mentions commands to use – which is of almost no worth since right now I have no clue what Sweep Gradient is!)
- android create pie dount with gradient and similar Questions (most of them relate to Rings – for which sweep is used)
- Gradients in Photoshop (which talks about Angular Gradient which I think might be related to Sweep gradient)
- Java Android Program to Demonstrate Sweep Gradient in Android (Again works with Rings)
So thats almost about it. Others are similar or disparagingly brief.
Few conclusions I have drawn so far (and maybe are wrong) :
- Sweep Gradient are mainly used for Rings
- They are used for similar transactions as the Angular sweep (not sure about this one) which talks about transaction similar to sweep of a clock’s hand.
- Very similar to Linear Gradients. The only thing I can think about for a difference between them is for the usage (like Rings).
- Sweep deals with Center (again a strong reference towards the clock theory)
I know people may consider this as not really a technical question. But for me to work with Sweep Gradients or atleast know what they mean, the description has to come from somewhere. And please do provide the answers with some examples of usage of Sweep in cases other than Rings.
2
Answers
The word gradient in this context (as in many graphics editors, including Photoshop) refers to a smooth transition between multiple colors, as opposed to using just a single color to fill an area.
The Android API provides 3 different gradients:
LinearGradient
,RadialGradient
andSweepGradient
.These are all subclasses of
Shader
. You can set aShader
on aPaint
object, and then draw any shape with thatPaint
. The shape will be filled with colors and transitions between them, according to the kind of the gradient.For example:
This image can be produced with this code:
So the
LinearGradient
isLinear Gradient
in Photoshop,RadialGradient
isRadial Gradient
, andSweepGradient
isAngular Gradient
, as described in your 3rd reference. I recommend trying these in an image editor first (all popular image editors have these tools), and you’ll quickly get the idea of how they work.You can use these gradients in an XML drawable too (like in your 4th reference), with a limitation of using 3 colors maximum.
In a
SweepGradient
, when providing positions, the 0.0 point is at 3 o’clock, going clockwise (with 0.25 being at 6 o’clock, 0.5 at 9 o’clock, 0.75 at 12 o’clock, and 1.0 back at 3 o’clock).About your conclusions:
SweepGradient
, not just a ring. Even the labels are drawn with the gradients in the example above.SweepGradient
is very similar toLinearGradient
, except you don’t need to provide aTileMode
, because you cannot run outside of the bounds of your color list.I hope this clears it up.
For xml drawable this can be specified with
android:type can have values(case-sensitive):
linear
,radial
orsweep