CircularProgressDrawable


public class CircularProgressDrawable extends Drawable implements Animatable


Drawable that renders the animated indeterminate progress indicator in the Material design style without depending on API level 11.

While this may be used to draw an indeterminate spinner using start and stop methods, this may also be used to draw a progress arc using setStartEndTrim method. CircularProgressDrawable also supports adding an arrow at the end of the arc by setArrowEnabled and setArrowDimensions methods.

To use one of the pre-defined sizes instead of using your own, setStyle should be called with one of the DEFAULT or LARGE styles as its parameter. Doing it so will update the arrow dimensions, ring size and stroke width to fit the one specified.

If no center radius is set via setCenterRadius or setStyle methods, CircularProgressDrawable will fill the bounds set via setBounds.

Summary

Constants

static final int

Maps to ProgressBar default style.

static final int
LARGE = 0

Maps to ProgressBar.Large style.

Public constructors

Public methods

void
draw(Canvas canvas)
int
boolean

Returns true if the arrow at the end of the spinner is shown.

float

Returns the arrow height in pixels.

float

Returns the scale of the arrow at the end of the spinner.

float

Returns the arrow width in pixels.

int

Returns the background color of the circle drawn inside the drawable.

float

Returns the center radius for the progress spinner in pixels.

@NonNull int[]

Returns the colors used in the progress animation

float

Returns the end trim for the progress spinner arc

int

This method is deprecated.

float

Returns the amount of rotation applied to the progress spinner.

float

Returns the start trim for the progress spinner arc

@NonNull Paint.Cap

Returns the stroke cap of the progress spinner.

float

Returns the stroke width for the progress spinner in pixels.

boolean
void
setAlpha(int alpha)
void
setArrowDimensions(float width, float height)

Sets the dimensions of the arrow at the end of the spinner in pixels.

void
setArrowEnabled(boolean show)

Sets if the arrow at the end of the spinner should be shown.

void
setArrowScale(float scale)

Sets the scale of the arrow at the end of the spinner.

void
setBackgroundColor(int color)

Sets the background color of the circle inside the drawable.

void
setCenterRadius(float centerRadius)

Sets the center radius for the progress spinner in pixels.

void
void

Sets the colors used in the progress animation from a color list.

void
setProgressRotation(float rotation)

Sets the amount of rotation to apply to the progress spinner.

void
setStartEndTrim(float start, float end)

Sets the start and end trim for the progress spinner arc.

void

Sets the stroke cap of the progress spinner.

void
setStrokeWidth(float strokeWidth)

Sets the stroke width for the progress spinner in pixels.

void
setStyle(int size)

Sets the overall size for the progress spinner.

void

Starts the animation for the spinner.

void

Stops the animation for the spinner.

Inherited methods

From android.graphics.drawable.Drawable
void
boolean
void
final Rect
final void
copyBounds(Rect bounds)
static Drawable
static Drawable
createFromResourceStream(
    Resources res,
    TypedValue value,
    InputStream is,
    String srcName
)
static Drawable
static Drawable
static Drawable
createFromXmlInner(
    Resources r,
    XmlPullParser parser,
    AttributeSet attrs
)
final Rect
Drawable.Callback
int
ColorFilter
Drawable.ConstantState
Drawable
Rect
void
int
int
int
final int
int
int
Insets
void
boolean
getPadding(Rect padding)
int[]
Region
boolean
void
void
boolean
boolean
boolean
boolean
final boolean
void
Drawable
void
boolean
onLayoutDirectionChanged(int layoutDirection)
boolean
onLevelChange(int level)
boolean
onStateChange(int[] state)
static int
resolveOpacity(int op1, int op2)
void
scheduleSelf(Runnable what, long when)
void
setAutoMirrored(boolean mirrored)
void
setBounds(int left, int top, int right, int bottom)
final void
void
void
setDither(boolean dither)

This method is deprecated.

void
setFilterBitmap(boolean filter)
void
setHotspot(float x, float y)
void
setHotspotBounds(int left, int top, int right, int bottom)
final boolean
setLayoutDirection(int layoutDirection)
final boolean
setLevel(int level)
boolean
setState(int[] stateSet)
void
setTint(int tintColor)
void
void
void
boolean
setVisible(boolean visible, boolean restart)
void

Constants

DEFAULT

Added in 1.0.0
public static final int DEFAULT = 1

Maps to ProgressBar default style.

LARGE

Added in 1.0.0
public static final int LARGE = 0

Maps to ProgressBar.Large style.

Public constructors

CircularProgressDrawable

Added in 1.0.0
public CircularProgressDrawable(@NonNull Context context)
Parameters
@NonNull Context context

application context

Public methods

draw

Added in 1.0.0
public void draw(Canvas canvas)

getAlpha

public int getAlpha()

getArrowEnabled

Added in 1.0.0
public boolean getArrowEnabled()

Returns true if the arrow at the end of the spinner is shown.

Returns
boolean

true if the arrow is shown, false otherwise.

getArrowHeight

Added in 1.0.0
public float getArrowHeight()

Returns the arrow height in pixels.

Returns
float

arrow height in pixels

getArrowScale

Added in 1.0.0
public float getArrowScale()

Returns the scale of the arrow at the end of the spinner.

Returns
float

scale of the arrow

getArrowWidth

Added in 1.0.0
public float getArrowWidth()

Returns the arrow width in pixels.

Returns
float

arrow width in pixels

getBackgroundColor

Added in 1.0.0
public int getBackgroundColor()

Returns the background color of the circle drawn inside the drawable.

Returns
int

an ARGB color

getCenterRadius

Added in 1.0.0
public float getCenterRadius()

Returns the center radius for the progress spinner in pixels.

Returns
float

center radius in pixels

getColorSchemeColors

Added in 1.0.0
public @NonNull int[] getColorSchemeColors()

Returns the colors used in the progress animation

Returns
@NonNull int[]

list of ARGB colors

getEndTrim

Added in 1.0.0
public float getEndTrim()

Returns the end trim for the progress spinner arc

Returns
float

end trim from [0..1]

getOpacity

Added in 1.0.0
public int getOpacity()

getProgressRotation

Added in 1.0.0
public float getProgressRotation()

Returns the amount of rotation applied to the progress spinner.

Returns
float

amount of rotation from [0..1]

getStartTrim

Added in 1.0.0
public float getStartTrim()

Returns the start trim for the progress spinner arc

Returns
float

start trim from [0..1]

getStrokeCap

Added in 1.0.0
public @NonNull Paint.Cap getStrokeCap()

Returns the stroke cap of the progress spinner.

Returns
@NonNull Paint.Cap

stroke cap

getStrokeWidth

Added in 1.0.0
public float getStrokeWidth()

Returns the stroke width for the progress spinner in pixels.

Returns
float

stroke width in pixels

isRunning

Added in 1.0.0
public boolean isRunning()

setAlpha

Added in 1.0.0
public void setAlpha(int alpha)

setArrowDimensions

Added in 1.0.0
public void setArrowDimensions(float width, float height)

Sets the dimensions of the arrow at the end of the spinner in pixels.

Parameters
float width

width of the baseline of the arrow in pixels

float height

distance from tip of the arrow to its baseline in pixels

setArrowEnabled

Added in 1.0.0
public void setArrowEnabled(boolean show)

Sets if the arrow at the end of the spinner should be shown.

Parameters
boolean show

true if the arrow should be drawn, false otherwise

setArrowScale

Added in 1.0.0
public void setArrowScale(float scale)

Sets the scale of the arrow at the end of the spinner.

Parameters
float scale

scaling that will be applied to the arrow's both width and height when drawing.

setBackgroundColor

Added in 1.0.0
public void setBackgroundColor(int color)

Sets the background color of the circle inside the drawable. Calling setAlpha does not affect the visibility background color, so it should be set separately if it needs to be hidden or visible.

Parameters
int color

an ARGB color

setCenterRadius

Added in 1.0.0
public void setCenterRadius(float centerRadius)

Sets the center radius for the progress spinner in pixels. If set to 0, this drawable will fill the bounds when drawn.

Parameters
float centerRadius

center radius in pixels

setColorFilter

Added in 1.0.0
public void setColorFilter(ColorFilter colorFilter)

setColorSchemeColors

public void setColorSchemeColors(@NonNull int[] colors)

Sets the colors used in the progress animation from a color list. The first color will also be the color to be used if animation is not started yet.

Parameters
@NonNull int[] colors

list of ARGB colors to be used in the spinner

setProgressRotation

Added in 1.0.0
public void setProgressRotation(float rotation)

Sets the amount of rotation to apply to the progress spinner.

Parameters
float rotation

rotation from [0..1]

setStartEndTrim

Added in 1.0.0
public void setStartEndTrim(float start, float end)

Sets the start and end trim for the progress spinner arc. 0 corresponds to the geometric angle of 0 degrees (3 o'clock on a watch) and it increases clockwise, coming to a full circle at 1.

Parameters
float start

starting position of the arc from [0..1]

float end

ending position of the arc from [0..1]

setStrokeCap

Added in 1.0.0
public void setStrokeCap(@NonNull Paint.Cap strokeCap)

Sets the stroke cap of the progress spinner. Default stroke cap is SQUARE.

Parameters
@NonNull Paint.Cap strokeCap

stroke cap

setStrokeWidth

Added in 1.0.0
public void setStrokeWidth(float strokeWidth)

Sets the stroke width for the progress spinner in pixels.

Parameters
float strokeWidth

stroke width in pixels

setStyle

Added in 1.0.0
public void setStyle(int size)

Sets the overall size for the progress spinner. This updates the radius and stroke width of the ring, and arrow dimensions.

Parameters
int size

one of LARGE or DEFAULT

start

Added in 1.0.0
public void start()

Starts the animation for the spinner.

stop

Added in 1.0.0
public void stop()

Stops the animation for the spinner.