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.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
GradientDrawable.Orientation | Controls how the gradient is oriented relative to the drawable's bounds |
XML Attributes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
android:angle | Angle of the gradient. | ||||||||||
android:bottom | Amount of bottom padding inside the gradient shape. | ||||||||||
android:centerColor | Optional center color. | ||||||||||
android:centerX | X coordinate of the origin of the gradient within the shape. | ||||||||||
android:centerY | Y coordinate of the origin of the gradient within the shape. | ||||||||||
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 | |||||||||||
android:useLevel | Indicates whether the drawable's level affects the way the gradient is drawn. | ||||||||||
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 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Create a new gradient drawable given an orientation and an array
of colors for the gradient.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Draw in its bounds (set via setBounds) respecting optional effects such
as alpha (set via setAlpha) and color filter (set via setColorFilter).
| |||||||||||
Gets the current alpha value for the drawable.
| |||||||||||
Return a mask of the configuration parameters for which this drawable
may change, requiring that it be re-created.
| |||||||||||
Return a
Drawable.ConstantState instance that holds the shared state of this Drawable. | |||||||||||
Return the intrinsic height of the underlying drawable object.
| |||||||||||
Return the intrinsic width of the underlying drawable object.
| |||||||||||
Return the opacity/transparency of this Drawable.
| |||||||||||
Returns the orientation of the gradient defined in this drawable.
| |||||||||||
Return in padding the insets suggested by this Drawable for placing
content inside the drawable's bounds.
| |||||||||||
Inflate this Drawable from an XML resource.
| |||||||||||
Make this drawable mutable.
| |||||||||||
Specify an alpha value for the drawable.
| |||||||||||
Changes this drawbale to use a single color instead of a gradient. | |||||||||||
Specify an optional colorFilter for the drawable.
| |||||||||||
Sets the colors used to draw the gradient. | |||||||||||
Specify radii for each of the 4 corners. | |||||||||||
Specify radius for the corners of the gradient. | |||||||||||
Set to true to have the drawable dither its colors when drawn to a device
with fewer than 8-bits per color component.
| |||||||||||
Sets the center location of the gradient. | |||||||||||
Sets the radius of the gradient. | |||||||||||
Sets the type of gradient used by this drawable.. | |||||||||||
Changes the orientation of the gradient defined in this drawable. | |||||||||||
Sets the type of shape used to draw the gradient. | |||||||||||
Sets the size of the shape drawn by this drawable. | |||||||||||
Set the stroke width and color for the drawable. | |||||||||||
Set the stroke width and color for the drawable. | |||||||||||
Sets whether or not this drawable will honor its |
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Override this in your subclass to change appearance if you recognize the
specified state.
| |||||||||||
Override this in your subclass to change appearance if you vary based
on level.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.graphics.drawable.Drawable
| |||||||||||
From class
java.lang.Object
|
Angle of the gradient.
Must be a floating point value, such as "1.2
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol angle
.
Amount of bottom padding inside the gradient shape.
Must 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), mm (millimeters).
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol bottom
.
Optional center color. For linear gradients, use centerX or centerY to place the center color.
Must be a color value, in the form of "#rgb
", "#argb
",
"#rrggbb
", or "#aarrggbb
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol centerColor
.
X coordinate of the origin of the gradient within the shape.
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.
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol centerX
.
Y coordinate of the origin of the gradient within the shape.
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.
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol centerY
.
Solid color for the gradient shape.
Must be a color value, in the form of "#rgb
", "#argb
",
"#rrggbb
", or "#aarrggbb
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol color
.
Color of the gradient shape's stroke.
Must be a color value, in the form of "#rgb
", "#argb
",
"#rrggbb
", or "#aarrggbb
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol color
.
Gap between dashes in the stroke.
Must 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), mm (millimeters).
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol dashGap
.
Length of a dash in the stroke.
Must 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), mm (millimeters).
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol dashWidth
.
End color of the gradient.
Must be a color value, in the form of "#rgb
", "#argb
",
"#rrggbb
", or "#aarrggbb
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol endColor
.
Radius of the gradient, used only with radial gradient.
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.
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol gradientRadius
.
Height of the gradient shape.
Must 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), mm (millimeters).
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol height
.
Inner radius of the ring. When defined, innerRadiusRatio is ignored.
Must 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), mm (millimeters).
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol innerRadius
.
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.
Must be a floating point value, such as "1.2
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol innerRadiusRatio
.
Amount of left padding inside the gradient shape.
Must 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), mm (millimeters).
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol left
.
Amount of right padding inside the gradient shape.
Must 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), mm (millimeters).
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol right
.
Indicates what shape to fill with a gradient.
Must be one of the following constant values.
Constant | Value | Description |
---|---|---|
rectangle | 0 | Rectangle shape, with optional rounder corners. |
oval | 1 | Oval shape. |
line | 2 | Line shape. |
ring | 3 | Ring shape. |
This corresponds to the global attribute
resource symbol shape
.
Start color of the gradient.
Must be a color value, in the form of "#rgb
", "#argb
",
"#rrggbb
", or "#aarrggbb
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol startColor
.
Thickness of the ring. When defined, thicknessRatio is ignored.
Must 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), mm (millimeters).
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol thickness
.
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.
Must be a floating point value, such as "1.2
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol thicknessRatio
.
Amount of top padding inside the gradient shape.
Must 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), mm (millimeters).
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol top
.
Type of gradient. The default type is linear.
Must be one of the following constant values.
Constant | Value | Description |
---|---|---|
linear | 0 | Linear gradient. |
radial | 1 | Radial, or circular, gradient. |
sweep | 2 | Sweep, or angled or diamond, gradient. |
This corresponds to the global attribute
resource symbol type
.
Indicates whether the drawable's level affects the way the gradient is drawn.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol useLevel
.
Indicates whether the drawable should intially be visible.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol visible
.
Width of the gradient shape.
Must 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), mm (millimeters).
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol width
.
Width of the gradient shape's stroke.
Must 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), mm (millimeters).
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol width
.
Gradient is linear (default.)
Shape is an ellipse
Gradient is circular.
Shape is a rectangle, possibly with rounded corners
Gradient is a sweep.
Create a new gradient drawable given an orientation and an array of colors for the gradient.
Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).
canvas | The canvas to draw into |
---|
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.
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.
Configuration
.Return a Drawable.ConstantState
instance that holds the shared state of this Drawable.
q
Return the intrinsic height of the underlying drawable object. Returns -1 if it has no intrinsic height, such as with a solid color.
Return the intrinsic width of the underlying drawable object. Returns -1 if it has no intrinsic width, such as with a solid color.
Return the opacity/transparency of this Drawable. The returned value is
one of the abstract format constants in
PixelFormat
:
UNKNOWN
,
TRANSLUCENT
,
TRANSPARENT
, or
OPAQUE
.
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 take into account a
custom alpha or color filter that has been applied by the client through
the setAlpha(int)
or setColorFilter(ColorFilter)
methods.
Returns the orientation of the gradient defined in this drawable.
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). Returns true if this drawable actually has a padding, else false. When false is returned, the padding is always set to 0.
Inflate this Drawable from an XML resource.
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.
Specify an alpha value for the drawable. 0 means fully transparent, and 255 means fully opaque.
Changes this drawbale 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.
argb | The color used to fill the shape |
---|
Specify an optional colorFilter for the drawable. Pass null to remove any filters.
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 orientation will affect all instances
of a drawable loaded from a resource. It is recommended to invoke
mutate()
before changing the orientation.
colors | 2 or more ARGB colors |
---|
Specify 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.
radii | 4 pairs of X and Y radius for each corner, specified in pixels. The length of this array must be >= 8 |
---|
Specify 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.
radius | The radius in pixels of the corners of the rectangle shape |
---|
Set to true to have the drawable dither its colors when drawn to a device with fewer than 8-bits per color component. This can improve the look on those devices, but can also slow down the drawing a little.
Sets the center location of the gradient. The radius is honored only when
the gradient type is set to RADIAL_GRADIENT
or SWEEP_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.
x | The x coordinate of the gradient's center |
---|---|
y | The y coordinate of the gradient's center |
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.
gradientRadius | The radius of the gradient in pixels |
---|
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.
gradient | The type of the gradient: LINEAR_GRADIENT ,
RADIAL_GRADIENT or SWEEP_GRADIENT |
---|
Changes 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.
orientation | The desired orientation (angle) of the gradient |
---|
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.
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.
width | The width of the shape used by this drawable |
---|---|
height | The height of the shape used by this drawable |
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.
width | The width in pixels of the stroke |
---|---|
color | The color of the stroke |
dashWidth | The length in pixels of the dashes, set to 0 to disable dashes |
dashGap | The gap in pixels between dashes |
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.
width | The width in pixels of the stroke |
---|---|
color | The color of the stroke |
Sets whether or not this drawable will honor its level
property.
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.
useLevel | True if this drawable should honor its level, false otherwise |
---|
Override this in your subclass to change appearance if you recognize the specified state.
Override this in your subclass to change appearance if you vary based on level.