GradientDrawable


public class GradientDrawable
extends Drawable

java.lang.Object
   ↳ android.graphics.drawable.Drawable
     ↳ android.graphics.drawable.GradientDrawable


A Drawable with a color gradient for buttons, backgrounds, etc.

It can be defined in an XML file with the <shape> element. For more information, see the guide to Drawable Resources.

Summary

XML attributes

android:angle Angle of the gradient, used only with linear gradient. 
android:bottom Amount of bottom padding inside the gradient shape. 
android:centerColor Optional center color. 
android:centerX X-position of the center point of the gradient within the shape as a fraction of the width. 
android:centerY Y-position of the center point of the gradient within the shape as a fraction of the height. 
android:color Solid color for the gradient shape. 
android:color Color of the gradient shape's stroke. 
android:dashGap Gap between dashes in the stroke. 
android:dashWidth Length of a dash in the stroke. 
android:endColor End color of the gradient. 
android:gradientRadius Radius of the gradient, used only with radial gradient. 
android:height Height of the gradient shape. 
android:innerRadius Inner radius of the ring. 
android:innerRadiusRatio Inner radius of the ring expressed as a ratio of the ring's width. 
android:left Amount of left padding inside the gradient shape. 
android:right Amount of right padding inside the gradient shape. 
android:shape Indicates what shape to fill with a gradient. 
android:startColor Start color of the gradient. 
android:thickness Thickness of the ring. 
android:thicknessRatio Thickness of the ring expressed as a ratio of the ring's width. 
android:top Amount of top padding inside the gradient shape. 
android:type Type of gradient. 
android:useLevel Whether the drawable level value (see Drawable.getLevel()) is used to scale the gradient. 
android:useLevel Whether the drawable level value (see Drawable.getLevel()) is used to scale the shape. 
android:visible Indicates whether the drawable should intially be visible. 
android:width Width of the gradient shape. 
android:width Width of the gradient shape's stroke. 

Constants

int LINE

Shape is a line

int LINEAR_GRADIENT

Gradient is linear (default.)

int OVAL

Shape is an ellipse

int RADIAL_GRADIENT

Gradient is circular.

int RECTANGLE

Shape is a rectangle, possibly with rounded corners

int RING

Shape is a ring.

int SWEEP_GRADIENT

Gradient is a sweep.

Public constructors

GradientDrawable()
GradientDrawable(GradientDrawable.Orientation orientation, int[] colors)

Create a new gradient drawable given an orientation and an array of colors for the gradient.

Public methods

void applyTheme(Resources.Theme t)

Applies the specified theme to this Drawable and its children.

boolean canApplyTheme()
void draw(Canvas canvas)

Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).

int getAlpha()

Gets the current alpha value for the drawable.

int getChangingConfigurations()

Return a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.

ColorStateList getColor()

Returns the color state list used to fill the shape, or null if the shape is filled with a gradient or has no fill color.

ColorFilter getColorFilter()

Returns the current color filter, or null if none set.

int[] getColors()

Returns the colors used to draw the gradient, or null if the gradient is drawn using a single color or no colors.

Drawable.ConstantState getConstantState()

Return a ConstantState instance that holds the shared state of this Drawable.

float[] getCornerRadii()

Returns the radii for each of the 4 corners.

float getCornerRadius()

Returns the radius for the corners of the gradient, that was previously set with setCornerRadius(float).

float getGradientCenterX()

Returns the X-position of the center of the gradient as a fraction of the width.

float getGradientCenterY()

Returns the Y-position of the center of this gradient as a fraction of the height.

float getGradientRadius()

Returns the radius of the gradient in pixels.

int getGradientType()

Returns the type of gradient used by this drawable, one of LINEAR_GRADIENT, RADIAL_GRADIENT, or SWEEP_GRADIENT.

int getInnerRadius()

Return the inner radius of the ring
The units of this value are pixels.

float getInnerRadiusRatio()

Return the inner radius of the ring expressed as a ratio of the ring's width.

int getIntrinsicHeight()

Returns the drawable's intrinsic height.

int getIntrinsicWidth()

Returns the drawable's intrinsic width.

int getOpacity()

This method is deprecated. This method is no longer used in graphics optimizations

Insets getOpticalInsets()

Return in insets the layout insets suggested by this Drawable for use with alignment operations during layout.

GradientDrawable.Orientation getOrientation()

Returns the orientation of the gradient defined in this drawable.

void getOutline(Outline outline)

Called to get the drawable to populate the Outline that defines its drawing area.

boolean getPadding(Rect padding)

Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds.

int getShape()

Returns the type of shape used by this drawable, one of LINE, OVAL, RECTANGLE or RING.

int getThickness()

Return the thickness of the ring
The units of this value are pixels.

float getThicknessRatio()

Return the thickness ratio of the ring expressed as a ratio of the ring's width.

boolean getUseLevel()

Returns whether this drawable's level property will be used to scale the gradient.

boolean hasFocusStateSpecified()

Indicates whether this drawable has at least one state spec explicitly specifying R.attr.state_focused.

void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme.

boolean isStateful()

Indicates whether this drawable will change its appearance based on state.

Drawable mutate()

Make this drawable mutable.

void setAlpha(int alpha)

Specify an alpha value for the drawable.

void setColor(ColorStateList colorStateList)

Changes this drawable to use a single color state list instead of a gradient.

void setColor(int argb)

Changes this drawable to use a single color instead of a gradient.

void setColorFilter(ColorFilter colorFilter)

Specify an optional color filter for the drawable.

void setColors(int[] colors, float[] offsets)

Sets the colors and offsets used to draw the gradient.

void setColors(int[] colors)

Sets the colors used to draw the gradient.

void setCornerRadii(float[] radii)

Specifies radii for each of the 4 corners.

void setCornerRadius(float radius)

Specifies the radius for the corners of the gradient.

void setDither(boolean dither)

This method is deprecated. This property is ignored.

void setGradientCenter(float x, float y)

Sets the position of the center of the gradient as a fraction of the width and height.

void setGradientRadius(float gradientRadius)

Sets the radius of the gradient.

void setGradientType(int gradient)

Sets the type of gradient used by this drawable.

void setInnerRadius(int innerRadius)

Configure the inner radius of the ring.

void setInnerRadiusRatio(float innerRadiusRatio)

Inner radius of the ring expressed as a ratio of the ring's width.

void setOrientation(GradientDrawable.Orientation orientation)

Sets the orientation of the gradient defined in this drawable.

void setPadding(int left, int top, int right, int bottom)

Configure the padding of the gradient shape

void setShape(int shape)

Sets the type of shape used to draw the gradient.

void setSize(int width, int height)

Sets the size of the shape drawn by this drawable.

void setStroke(int width, ColorStateList colorStateList)

Set the stroke width and color state list for the drawable.

void setStroke(int width, ColorStateList colorStateList, float dashWidth, float dashGap)

Set the stroke width and color state list for the drawable.

void setStroke(int width, int color, float dashWidth, float dashGap)

Set the stroke width and color for the drawable.

void setStroke(int width, int color)

Set the stroke width and color for the drawable.

void setThickness(int thickness)

Configure the thickness of the ring.

void setThicknessRatio(float thicknessRatio)

Configure the thickness of the ring expressed as a ratio of the ring's width.

void setTintBlendMode(BlendMode blendMode)

Specifies a tint blending mode for this drawable.

void setTintList(ColorStateList tint)

Specifies tint color for this drawable as a color state list.

void setUseLevel(boolean useLevel)

Sets whether this drawable's level property will be used to scale the gradient.

Protected methods

void onBoundsChange(Rect r)

Override this in your subclass to change appearance if you vary based on the bounds.

boolean onLevelChange(int level)

Override this in your subclass to change appearance if you vary based on level.

boolean onStateChange(int[] stateSet)

Override this in your subclass to change appearance if you recognize the specified state.

Inherited methods

XML attributes

android:angle

Angle of the gradient, used only with linear gradient. Must be a multiple of 45 in the range [0, 315].

May be a floating point value, such as "1.2".

android:bottom

Amount of bottom padding inside the gradient shape.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Related methods:

android:centerColor

Optional center color. For linear gradients, use centerX or centerY to place the center color.

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

android:centerX

X-position of the center point of the gradient within the shape as a fraction of the width. The default value is 0.5.

May be a floating point value, such as "1.2".

May be a fractional value, which is a floating point number appended with either % or %p, such as "14.5%". The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to some parent container.

android:centerY

Y-position of the center point of the gradient within the shape as a fraction of the height. The default value is 0.5.

May be a floating point value, such as "1.2".

May be a fractional value, which is a floating point number appended with either % or %p, such as "14.5%". The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to some parent container.

android:color

Solid color for the gradient shape.

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

android:color

Color of the gradient shape's stroke.

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

android:dashGap

Gap between dashes in the stroke.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

android:dashWidth

Length of a dash in the stroke.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

android:endColor

End color of the gradient.

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

android:gradientRadius

Radius of the gradient, used only with radial gradient. May be an explicit dimension or a fractional value relative to the shape's minimum dimension.

May be a floating point value, such as "1.2".

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

May be a fractional value, which is a floating point number appended with either % or %p, such as "14.5%". The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to some parent container.

android:height

Height of the gradient shape.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

android:innerRadius

Inner radius of the ring. When defined, innerRadiusRatio is ignored.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Related methods:

android:innerRadiusRatio

Inner radius of the ring expressed as a ratio of the ring's width. For instance, if innerRadiusRatio=9, then the inner radius equals the ring's width divided by 9. This value is ignored if innerRadius is defined. Default value is 9.

May be a floating point value, such as "1.2".

Related methods:

android:left

Amount of left padding inside the gradient shape.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Related methods:

android:right

Amount of right padding inside the gradient shape.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Related methods:

android:shape

Indicates what shape to fill with a gradient.

Must be one of the following constant values.

ConstantValueDescription
line2Line shape.
oval1Oval shape.
rectangle0Rectangle shape, with optional rounder corners.
ring3Ring shape.

android:startColor

Start color of the gradient.

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

android:thickness

Thickness of the ring. When defined, thicknessRatio is ignored.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Related methods:

android:thicknessRatio

Thickness of the ring expressed as a ratio of the ring's width. For instance, if thicknessRatio=3, then the thickness equals the ring's width divided by 3. This value is ignored if innerRadius is defined. Default value is 3.

May be a floating point value, such as "1.2".

Related methods:

android:top

Amount of top padding inside the gradient shape.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Related methods:

android:type

Type of gradient. The default type is linear.

Must be one of the following constant values.

ConstantValueDescription
linear0Linear gradient extending across the center point.
radial1Radial gradient extending from the center point outward.
sweep2Sweep (or angular) gradient sweeping counter-clockwise around the center point.

android:useLevel

Whether the drawable level value (see Drawable.getLevel()) is used to scale the gradient. Scaling behavior varies based on gradient type. For "linear", adjusts the ending position along the gradient's axis of orientation. For "radial", adjusts the outer radius. For "sweep", adjusts the ending angle. The default value is false.

May be a boolean value, such as "true" or "false".

Related methods:

android:useLevel

Whether the drawable level value (see Drawable.getLevel()) is used to scale the shape. Scaling behavior depends on the shape type. For "ring", the angle is scaled from 0 to 360. For all other types, there is no effect. The default value is true.

May be a boolean value, such as "true" or "false".

android:visible

Indicates whether the drawable should intially be visible.

May be a boolean value, such as "true" or "false".

android:width

Width of the gradient shape.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

android:width

Width of the gradient shape's stroke.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

Constants

LINE

Added in API level 1
public static final int LINE

Shape is a line

Constant Value: 2 (0x00000002)

LINEAR_GRADIENT

Added in API level 1
public static final int LINEAR_GRADIENT

Gradient is linear (default.)

Constant Value: 0 (0x00000000)

OVAL

Added in API level 1
public static final int OVAL

Shape is an ellipse

Constant Value: 1 (0x00000001)

RADIAL_GRADIENT

Added in API level 1
public static final int RADIAL_GRADIENT

Gradient is circular.

Constant Value: 1 (0x00000001)

RECTANGLE

Added in API level 1
public static final int RECTANGLE

Shape is a rectangle, possibly with rounded corners

Constant Value: 0 (0x00000000)

RING

Added in API level 1
public static final int RING

Shape is a ring.

Constant Value: 3 (0x00000003)

SWEEP_GRADIENT

Added in API level 1
public static final int SWEEP_GRADIENT

Gradient is a sweep.

Constant Value: 2 (0x00000002)

Public constructors

GradientDrawable

Added in API level 1
public GradientDrawable ()

GradientDrawable

Added in API level 1
public GradientDrawable (GradientDrawable.Orientation orientation, 
                int[] colors)

Create a new gradient drawable given an orientation and an array of colors for the gradient.

Parameters
orientation GradientDrawable.Orientation

colors int

Public methods

applyTheme

Added in API level 21
public void applyTheme (Resources.Theme t)

Applies the specified theme to this Drawable and its children.

Parameters
t Resources.Theme: This value cannot be null.

canApplyTheme

Added in API level 21
public boolean canApplyTheme ()

Returns
boolean

draw

Added in API level 1
public void draw (Canvas canvas)

Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).

Parameters
canvas Canvas: The canvas to draw into This value cannot be null.

getAlpha

Added in API level 19
public int getAlpha ()

Gets the current alpha value for the drawable. 0 means fully transparent, 255 means fully opaque. This method is implemented by Drawable subclasses and the value returned is specific to how that class treats alpha. The default return value is 255 if the class does not override this method to return a value specific to its use of alpha.

Returns
int Value is between 0 and 255 inclusive

getChangingConfigurations

Added in API level 1
public int getChangingConfigurations ()

Return a mask of the configuration parameters for which this drawable may change, requiring that it be re-created. The default implementation returns whatever was provided through setChangingConfigurations(int) or 0 by default. Subclasses may extend this to or in the changing configurations of any other drawables they hold.

Returns
int Value is either 0 or a combination of ActivityInfo.CONFIG_MCC, ActivityInfo.CONFIG_MNC, ActivityInfo.CONFIG_LOCALE, ActivityInfo.CONFIG_TOUCHSCREEN, ActivityInfo.CONFIG_KEYBOARD, ActivityInfo.CONFIG_KEYBOARD_HIDDEN, ActivityInfo.CONFIG_NAVIGATION, ActivityInfo.CONFIG_ORIENTATION, ActivityInfo.CONFIG_SCREEN_LAYOUT, ActivityInfo.CONFIG_UI_MODE, ActivityInfo.CONFIG_SCREEN_SIZE, ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE, ActivityInfo.CONFIG_DENSITY, ActivityInfo.CONFIG_LAYOUT_DIRECTION, ActivityInfo.CONFIG_COLOR_MODE, ActivityInfo.CONFIG_FONT_SCALE, and ActivityInfo.CONFIG_GRAMMATICAL_GENDER

getColor

Added in API level 24
public ColorStateList getColor ()

Returns the color state list used to fill the shape, or null if the shape is filled with a gradient or has no fill color.

Returns
ColorStateList the color state list used to fill this gradient, or null

getColorFilter

Added in API level 21
public ColorFilter getColorFilter ()

Returns the current color filter, or null if none set.

Returns
ColorFilter This value may be null.

getColors

Added in API level 24
public int[] getColors ()

Returns the colors used to draw the gradient, or null if the gradient is drawn using a single color or no colors.

Returns
int[] the colors used to draw the gradient, or null

See also:

getConstantState

Added in API level 1
public Drawable.ConstantState getConstantState ()

Return a ConstantState instance that holds the shared state of this Drawable.

Returns
Drawable.ConstantState The ConstantState associated to that Drawable. This value may be null.

getCornerRadii

Added in API level 24
public float[] getCornerRadii ()

Returns the radii for each of the 4 corners. For each corner, the array contains 2 values, [X_radius, Y_radius]. The corners are ordered top-left, top-right, bottom-right, bottom-left.

If the radius was previously set with setCornerRadius(float), or if the corners are not rounded, this method will return null.

Returns
float[] an array containing the radii for each of the 4 corners, or null

getCornerRadius

Added in API level 24
public float getCornerRadius ()

Returns the radius for the corners of the gradient, that was previously set with setCornerRadius(float).

If the radius was previously cleared via passing null to setCornerRadii(float[]), this method will return 0.

Returns
float the radius in pixels of the corners of the rectangle shape, or 0

getGradientCenterX

Added in API level 24
public float getGradientCenterX ()

Returns the X-position of the center of the gradient as a fraction of the width.

Returns
float the X-position of the center of the gradient

getGradientCenterY

Added in API level 24
public float getGradientCenterY ()

Returns the Y-position of the center of this gradient as a fraction of the height.

Returns
float the Y-position of the center of the gradient

getGradientRadius

Added in API level 21
public float getGradientRadius ()

Returns the radius of the gradient in pixels. The radius is valid only when the gradient type is set to RADIAL_GRADIENT.

Returns
float the radius of the gradient in pixels

getGradientType

Added in API level 24
public int getGradientType ()

Returns the type of gradient used by this drawable, one of LINEAR_GRADIENT, RADIAL_GRADIENT, or SWEEP_GRADIENT.

Returns
int the type of gradient used by this drawable Value is LINEAR_GRADIENT, RADIAL_GRADIENT, or SWEEP_GRADIENT

getInnerRadius

Added in API level 29
public int getInnerRadius ()

Return the inner radius of the ring
The units of this value are pixels.

Related XML Attributes:

Returns
int The units of this value are pixels. {}

getInnerRadiusRatio

Added in API level 29
public float getInnerRadiusRatio ()

Return the inner radius of the ring expressed as a ratio of the ring's width.

Related XML Attributes:

Returns
float

getIntrinsicHeight

Added in API level 1
public int getIntrinsicHeight ()

Returns the drawable's intrinsic height.

Intrinsic height is the height at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic height, such as a solid color, this method returns -1.

Returns
int the intrinsic height, or -1 if no intrinsic height

getIntrinsicWidth

Added in API level 1
public int getIntrinsicWidth ()

Returns the drawable's intrinsic width.

Intrinsic width is the width at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic width, such as a solid color, this method returns -1.

Returns
int the intrinsic width, or -1 if no intrinsic width

getOpacity

Added in API level 1
public int getOpacity ()

This method is deprecated.
This method is no longer used in graphics optimizations

Return the opacity/transparency of this Drawable. The returned value is one of the abstract format constants in PixelFormat: PixelFormat.UNKNOWN, PixelFormat.TRANSLUCENT, PixelFormat.TRANSPARENT, or PixelFormat.OPAQUE.

An OPAQUE drawable is one that draws all all content within its bounds, completely covering anything behind the drawable. A TRANSPARENT drawable is one that draws nothing within its bounds, allowing everything behind it to show through. A TRANSLUCENT drawable is a drawable in any other state, where the drawable will draw some, but not all, of the content within its bounds and at least some content behind the drawable will be visible. If the visibility of the drawable's contents cannot be determined, the safest/best return value is TRANSLUCENT.

Generally a Drawable should be as conservative as possible with the value it returns. For example, if it contains multiple child drawables and only shows one of them at a time, if only one of the children is TRANSLUCENT and the others are OPAQUE then TRANSLUCENT should be returned. You can use the method resolveOpacity(int, int) to perform a standard reduction of two opacities to the appropriate single output.

Note that the returned value does not necessarily take into account a custom alpha or color filter that has been applied by the client through the setAlpha(int) or setColorFilter(ColorFilter) methods. Some subclasses, such as BitmapDrawable, ColorDrawable, and GradientDrawable, do account for the value of setAlpha(int), but the general behavior is dependent upon the implementation of the subclass.

Returns
int int The opacity class of the Drawable. Value is PixelFormat.UNKNOWN, PixelFormat.TRANSLUCENT, PixelFormat.TRANSPARENT, or PixelFormat.OPAQUE

getOpticalInsets

Added in API level 29
public Insets getOpticalInsets ()

Return in insets the layout insets suggested by this Drawable for use with alignment operations during layout.

Returns
Insets This value cannot be null.

getOrientation

Added in API level 16
public GradientDrawable.Orientation getOrientation ()

Returns the orientation of the gradient defined in this drawable.

Returns
GradientDrawable.Orientation the orientation of the gradient defined in this drawable

getOutline

Added in API level 21
public void getOutline (Outline outline)

Called to get the drawable to populate the Outline that defines its drawing area.

This method is called by the default ViewOutlineProvider to define the outline of the View.

The default behavior defines the outline to be the bounding rectangle of 0 alpha. Subclasses that wish to convey a different shape or alpha value must override this method.

Parameters
outline Outline: This value cannot be null.

getPadding

Added in API level 1
public boolean getPadding (Rect padding)

Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds. Positive values move toward the center of the Drawable (set Rect.inset).

Parameters
padding Rect: This value cannot be null.

Returns
boolean true if this drawable actually has a padding, else false. When false is returned, the padding is always set to 0.

getShape

Added in API level 24
public int getShape ()

Returns the type of shape used by this drawable, one of LINE, OVAL, RECTANGLE or RING.

Returns
int the type of shape used by this drawable Value is RECTANGLE, OVAL, LINE, or RING

See also:

getThickness

Added in API level 29
public int getThickness ()

Return the thickness of the ring
The units of this value are pixels.

Related XML Attributes:

Returns
int The units of this value are pixels. {}

See also:

getThicknessRatio

Added in API level 29
public float getThicknessRatio ()

Return the thickness ratio of the ring expressed as a ratio of the ring's width.

Related XML Attributes:

Returns
float

getUseLevel

Added in API level 24
public boolean getUseLevel ()

Returns whether this drawable's level property will be used to scale the gradient.

Related XML Attributes:

Returns
boolean true if the gradient should be scaled based on level, false otherwise

hasFocusStateSpecified

Added in API level 31
public boolean hasFocusStateSpecified ()

Indicates whether this drawable has at least one state spec explicitly specifying R.attr.state_focused.

Note: A View uses a Drawable instance as its background and it changes its appearance based on a state. On keyboard devices, it should specify its R.attr.state_focused to make sure the user knows which view is holding the focus.

Returns
boolean true if R.attr.state_focused is specified for this drawable.

inflate

Added in API level 21
public void inflate (Resources r, 
                XmlPullParser parser, 
                AttributeSet attrs, 
                Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme. This can't be called more than once for each Drawable. Note that framework may have called this once to create the Drawable instance from XML resource.

Parameters
r Resources: This value cannot be null.

parser XmlPullParser: This value cannot be null.

attrs AttributeSet: This value cannot be null.

theme Resources.Theme: This value may be null.

Throws
IOException
XmlPullParserException

isStateful

Added in API level 1
public boolean isStateful ()

Indicates whether this drawable will change its appearance based on state. Clients can use this to determine whether it is necessary to calculate their state and call setState.

Returns
boolean True if this drawable changes its appearance based on state, false otherwise.

mutate

Added in API level 3
public Drawable mutate ()

Make this drawable mutable. This operation cannot be reversed. A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification. Calling this method on a mutable Drawable will have no effect.

Returns
Drawable This drawable. This value cannot be null.

setAlpha

Added in API level 1
public void setAlpha (int alpha)

Specify an alpha value for the drawable. 0 means fully transparent, and 255 means fully opaque.

Parameters
alpha int: Value is between 0 and 255 inclusive

setColor

Added in API level 21
public void setColor (ColorStateList colorStateList)

Changes this drawable to use a single color state list instead of a gradient. Calling this method with a null argument will clear the color and is equivalent to calling setColor(int) with the argument Color#TRANSPARENT.

Note: changing color will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the color.

Parameters
colorStateList ColorStateList: The color state list used to fill the shape This value may be null.

See also:

setColor

Added in API level 1
public void setColor (int argb)

Changes this drawable to use a single color instead of a gradient.

Note: changing color will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the color.

Parameters
argb int: The color used to fill the shape

setColorFilter

Added in API level 1
public void setColorFilter (ColorFilter colorFilter)

Specify an optional color filter for the drawable.

If a Drawable has a ColorFilter, each output pixel of the Drawable's drawing contents will be modified by the color filter before it is blended onto the render target of a Canvas.

Pass null to remove any existing color filter.

Note: Setting a non-null color filter disables tint.

Parameters
colorFilter ColorFilter: This value may be null.

setColors

Added in API level 29
public void setColors (int[] colors, 
                float[] offsets)

Sets the colors and offsets used to draw the gradient.

Each color is specified as an ARGB integer and the array must contain at least 2 colors.

Note: changing colors will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the colors.

Parameters
colors int: an array containing 2 or more ARGB colors This value may be null.

offsets float: optional array of floating point parameters representing the positions of the colors. Null evenly disperses the colors This value may be null.

setColors

Added in API level 16
public void setColors (int[] colors)

Sets the colors used to draw the gradient.

Each color is specified as an ARGB integer and the array must contain at least 2 colors.

Note: changing colors will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the colors.

Parameters
colors int: an array containing 2 or more ARGB colors This value may be null.

setCornerRadii

Added in API level 1
public void setCornerRadii (float[] radii)

Specifies radii for each of the 4 corners. For each corner, the array contains 2 values, [X_radius, Y_radius]. The corners are ordered top-left, top-right, bottom-right, bottom-left. This property is honored only when the shape is of type RECTANGLE.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
radii float: an array of length >= 8 containing 4 pairs of X and Y radius for each corner, specified in pixels This value may be null.

setCornerRadius

Added in API level 1
public void setCornerRadius (float radius)

Specifies the radius for the corners of the gradient. If this is > 0, then the drawable is drawn in a round-rectangle, rather than a rectangle. This property is honored only when the shape is of type RECTANGLE.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
radius float: The radius in pixels of the corners of the rectangle shape

setDither

Added in API level 1
public void setDither (boolean dither)

This method is deprecated.
This property is ignored.

Set to true to have the drawable dither its colors when drawn to a device with fewer than 8-bits per color component.

Parameters
dither boolean

setGradientCenter

Added in API level 1
public void setGradientCenter (float x, 
                float y)

Sets the position of the center of the gradient as a fraction of the width and height.

The default value is (0.5, 0.5).

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
x float: the X-position of the center of the gradient

y float: the Y-position of the center of the gradient

setGradientRadius

Added in API level 1
public void setGradientRadius (float gradientRadius)

Sets the radius of the gradient. The radius is honored only when the gradient type is set to RADIAL_GRADIENT.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
gradientRadius float: the radius of the gradient in pixels

setGradientType

Added in API level 1
public void setGradientType (int gradient)

Sets the type of gradient used by this drawable.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
gradient int: The type of the gradient: LINEAR_GRADIENT, RADIAL_GRADIENT or SWEEP_GRADIENT Value is LINEAR_GRADIENT, RADIAL_GRADIENT, or SWEEP_GRADIENT

setInnerRadius

Added in API level 29
public void setInnerRadius (int innerRadius)

Configure the inner radius of the ring.

Related XML Attributes:

Parameters
innerRadius int: The units of this value are pixels.

See also:

setInnerRadiusRatio

Added in API level 29
public void setInnerRadiusRatio (float innerRadiusRatio)

Inner radius of the ring expressed as a ratio of the ring's width.

Related XML Attributes:

Parameters
innerRadiusRatio float: Value is 0.0f or greater

setOrientation

Added in API level 16
public void setOrientation (GradientDrawable.Orientation orientation)

Sets the orientation of the gradient defined in this drawable.

Note: changing orientation will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the orientation.

Parameters
orientation GradientDrawable.Orientation: the desired orientation (angle) of the gradient

setPadding

Added in API level 29
public void setPadding (int left, 
                int top, 
                int right, 
                int bottom)

Configure the padding of the gradient shape

Related XML Attributes:

Parameters
left int: Left padding of the gradient shape The units of this value are pixels.

top int: Top padding of the gradient shape The units of this value are pixels.

right int: Right padding of the gradient shape The units of this value are pixels.

bottom int: Bottom padding of the gradient shape The units of this value are pixels.

setShape

Added in API level 1
public void setShape (int shape)

Sets the type of shape used to draw the gradient.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
shape int: The desired shape for this drawable: LINE, OVAL, RECTANGLE or RING Value is RECTANGLE, OVAL, LINE, or RING

See also:

setSize

Added in API level 1
public void setSize (int width, 
                int height)

Sets the size of the shape drawn by this drawable.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
width int: The width of the shape used by this drawable

height int: The height of the shape used by this drawable

setStroke

Added in API level 21
public void setStroke (int width, 
                ColorStateList colorStateList)

Set the stroke width and color state list for the drawable. If width is zero, then no stroke is drawn.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
width int: The width in pixels of the stroke

colorStateList ColorStateList: The color state list of the stroke

setStroke

Added in API level 21
public void setStroke (int width, 
                ColorStateList colorStateList, 
                float dashWidth, 
                float dashGap)

Set the stroke width and color state list for the drawable. If width is zero, then no stroke is drawn. This method can also be used to dash the stroke.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
width int: The width in pixels of the stroke

colorStateList ColorStateList: The color state list of the stroke

dashWidth float: The length in pixels of the dashes, set to 0 to disable dashes

dashGap float: The gap in pixels between dashes

setStroke

Added in API level 1
public void setStroke (int width, 
                int color, 
                float dashWidth, 
                float dashGap)

Set the stroke width and color for the drawable. If width is zero, then no stroke is drawn. This method can also be used to dash the stroke.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
width int: The width in pixels of the stroke

color int: The color of the stroke

dashWidth float: The length in pixels of the dashes, set to 0 to disable dashes

dashGap float: The gap in pixels between dashes

setStroke

Added in API level 1
public void setStroke (int width, 
                int color)

Set the stroke width and color for the drawable. If width is zero, then no stroke is drawn.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
width int: The width in pixels of the stroke

color int: The color of the stroke

setThickness

Added in API level 29
public void setThickness (int thickness)

Configure the thickness of the ring.

Related XML Attributes:

Parameters
thickness int: The units of this value are pixels.

setThicknessRatio

Added in API level 29
public void setThicknessRatio (float thicknessRatio)

Configure the thickness of the ring expressed as a ratio of the ring's width.

Related XML Attributes:

Parameters
thicknessRatio float: Value is 0.0f or greater

setTintBlendMode

Added in API level 29
public void setTintBlendMode (BlendMode blendMode)

Specifies a tint blending mode for this drawable.

Defines how this drawable's tint color should be blended into the drawable before it is drawn to screen. Default tint mode is BlendMode#SRC_IN.

Note: Setting a color filter via setColorFilter(android.graphics.ColorFilter)

Parameters
blendMode BlendMode: This value cannot be null.

setTintList

Added in API level 21
public void setTintList (ColorStateList tint)

Specifies tint color for this drawable as a color state list.

A Drawable's drawing content will be blended together with its tint before it is drawn to the screen. This functions similarly to setColorFilter(int, android.graphics.PorterDuff.Mode).

Note: Setting a color filter via setColorFilter(android.graphics.ColorFilter) or setColorFilter(int, android.graphics.PorterDuff.Mode) overrides tint.

Parameters
tint ColorStateList: This value may be null.

setUseLevel

Added in API level 1
public void setUseLevel (boolean useLevel)

Sets whether this drawable's level property will be used to scale the gradient. If a gradient is not used, this property has no effect.

Scaling behavior varies based on gradient type:

  • LINEAR_GRADIENT adjusts the ending position along the gradient's axis of orientation (see getOrientation())
  • RADIAL_GRADIENT adjusts the outer radius
  • SWEEP_GRADIENT adjusts the ending angle

      The default value for this property is false.

      Note: This property corresponds to the android:useLevel attribute on the inner <gradient> tag, NOT the android:useLevel attribute on the outer <shape> tag. For example,

      <shape ...>
           <gradient
               ...
               android:useLevel="true" />
       </shape>
       

      Note: Changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

      Related XML Attributes:

      Parameters
      useLevel boolean: true if the gradient should be scaled based on level, false otherwise

Protected methods

onBoundsChange

Added in API level 1
protected void onBoundsChange (Rect r)

Override this in your subclass to change appearance if you vary based on the bounds.

Parameters
r Rect: This value cannot be null.

onLevelChange

Added in API level 1
protected boolean onLevelChange (int level)

Override this in your subclass to change appearance if you vary based on level.

Parameters
level int

Returns
boolean Returns true if the level change has caused the appearance of the Drawable to change (that is, it needs to be drawn), else false if it looks the same and there is no need to redraw it since its last level.

onStateChange

Added in API level 1
protected boolean onStateChange (int[] stateSet)

Override this in your subclass to change appearance if you recognize the specified state.

Parameters
stateSet int: This value cannot be null.

Returns
boolean Returns true if the state change has caused the appearance of the Drawable to change (that is, it needs to be drawn), else false if it looks the same and there is no need to redraw it since its last state.