public class GradientDrawable extends Drawable
It can be defined in an XML file with the <shape>
element. For more
information, see the guide to Drawable Resources.
Modifier and Type | Class and Description |
---|---|
static interface |
GradientDrawable.GradientType |
static class |
GradientDrawable.Orientation
Controls how the gradient is oriented relative to the drawable's bounds
|
static interface |
GradientDrawable.RadiusType |
static interface |
GradientDrawable.Shape |
Drawable.Callback, Drawable.ConstantState
Modifier and Type | Field and Description |
---|---|
static int |
LINE
Shape is a line
|
static int |
LINEAR_GRADIENT
Gradient is linear (default.)
|
static int |
OVAL
Shape is an ellipse
|
static int |
RADIAL_GRADIENT
Gradient is circular.
|
static int |
RECTANGLE
Shape is a rectangle, possibly with rounded corners
|
static int |
RING
Shape is a ring.
|
static int |
SWEEP_GRADIENT
Gradient is a sweep.
|
Constructor and Description |
---|
GradientDrawable() |
GradientDrawable(GradientDrawable.Orientation orientation,
int[] colors)
Create a new gradient drawable given an orientation and an array
of colors for the gradient.
|
Modifier and Type | Method and Description |
---|---|
void |
applyTheme(Resources.Theme t)
Applies the specified theme to this Drawable and its children.
|
boolean |
canApplyTheme() |
void |
clearMutated()
Clears the mutated state, allowing this drawable to be cached and
mutated again.
|
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. 0 means fully transparent,
255 means fully opaque.
|
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
Drawable.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.
|
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 |
getIntrinsicHeight()
Returns the drawable's intrinsic height.
|
int |
getIntrinsicWidth()
Returns the drawable's intrinsic width.
|
int |
getOpacity()
Return the opacity/transparency of this Drawable.
|
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()
|
boolean |
getUseLevel()
Returns whether this drawable's
level property will be used to
scale the gradient. |
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.
|
protected void |
onBoundsChange(Rect r)
Override this in your subclass to change appearance if you vary based on
the bounds.
|
protected boolean |
onLevelChange(int level)
Override this in your subclass to change appearance if you vary based
on level.
|
protected boolean |
onStateChange(int[] stateSet)
Override this in your subclass to change appearance if you recognize the
specified state.
|
void |
setAlpha(int alpha)
Specify an alpha value for the drawable. 0 means fully transparent, and
255 means fully opaque.
|
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)
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)
Set to true to have the drawable dither its colors when drawn to a
device with fewer than 8-bits per color component.
|
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 |
setOrientation(GradientDrawable.Orientation orientation)
Sets the orientation of the gradient defined in this drawable.
|
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)
Set the stroke width and color for the drawable.
|
void |
setStroke(int width,
int color,
float dashWidth,
float dashGap)
Set the stroke width and color for the drawable.
|
void |
setTintList(ColorStateList tint)
Specifies tint color for this drawable as a color state list.
|
void |
setTintMode(PorterDuff.Mode tintMode)
Specifies a tint blending mode for this drawable.
|
void |
setUseLevel(boolean useLevel)
Sets whether this drawable's
level property will be used to
scale the gradient. |
clearColorFilter, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromResourceStream, createFromStream, createFromXml, createFromXml, createFromXmlInner, createFromXmlInner, getBounds, getCallback, getCurrent, getDirtyBounds, getHotspotBounds, getLayoutDirection, getLevel, getMinimumHeight, getMinimumWidth, getState, getTransparentRegion, inflate, invalidateSelf, isAutoMirrored, isFilterBitmap, isProjected, isVisible, jumpToCurrentState, obtainAttributes, onLayoutDirectionChanged, parseTintMode, resolveOpacity, scheduleSelf, setAutoMirrored, setBounds, setBounds, setCallback, setChangingConfigurations, setColorFilter, setFilterBitmap, setHotspot, setHotspotBounds, setLayoutDirection, setLevel, setState, setTint, setVisible, setXfermode, unscheduleSelf
public static final int RECTANGLE
public static final int OVAL
public static final int LINE
public static final int RING
public static final int LINEAR_GRADIENT
public static final int RADIAL_GRADIENT
public static final int SWEEP_GRADIENT
public GradientDrawable()
public GradientDrawable(GradientDrawable.Orientation orientation, int[] colors)
public boolean getPadding(Rect padding)
Drawable
getPadding
in class Drawable
public void setCornerRadii(float[] radii)
[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
- an array of length >= 8 containing 4 pairs of X and Y
radius for each corner, specified in pixelsmutate()
,
setShape(int)
,
setCornerRadius(float)
public float[] getCornerRadii()
[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
.
null
setCornerRadii(float[])
public void setCornerRadius(float radius)
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 shapemutate()
,
setCornerRadii(float[])
,
setShape(int)
public float getCornerRadius()
If the radius was previously set with setCornerRadii(float[])
,
or if the corners are not rounded, this method will return null
.
setCornerRadius(float)
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.
width
- The width in pixels of the strokecolor
- The color of the strokemutate()
,
setStroke(int, int, float, float)
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.
width
- The width in pixels of the strokecolorStateList
- The color state list of the strokemutate()
,
setStroke(int, ColorStateList, float, float)
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.
width
- The width in pixels of the strokecolor
- The color of the strokedashWidth
- The length in pixels of the dashes, set to 0 to disable dashesdashGap
- The gap in pixels between dashesmutate()
,
setStroke(int, int)
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.
width
- The width in pixels of the strokecolorStateList
- The color state list of the strokedashWidth
- The length in pixels of the dashes, set to 0 to disable dashesdashGap
- The gap in pixels between dashesmutate()
,
setStroke(int, ColorStateList)
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.
width
- The width of the shape used by this drawableheight
- The height of the shape used by this drawablemutate()
,
setGradientType(int)
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.
public int getShape()
setShape(int)
public void setGradientType(int 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.
gradient
- The type of the gradient: LINEAR_GRADIENT
,
RADIAL_GRADIENT
or SWEEP_GRADIENT
mutate()
,
getGradientType()
public int getGradientType()
LINEAR_GRADIENT
, RADIAL_GRADIENT
, or
SWEEP_GRADIENT
.setGradientType(int)
public void setGradientCenter(float x, float y)
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.
x
- the X-position of the center of the gradienty
- the Y-position of the center of the gradientmutate()
,
setGradientType(int)
,
getGradientCenterX()
,
getGradientCenterY()
public float getGradientCenterX()
setGradientCenter(float, float)
public float getGradientCenterY()
setGradientCenter(float, float)
public void setGradientRadius(float gradientRadius)
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 pixelsmutate()
,
setGradientType(int)
,
getGradientRadius()
public float getGradientRadius()
RADIAL_GRADIENT
.setGradientRadius(float)
public void setUseLevel(boolean useLevel)
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.
useLevel
- true
if the gradient should be scaled based on
level, false
otherwisemutate()
,
Drawable.setLevel(int)
,
Drawable.getLevel()
,
getUseLevel()
public boolean getUseLevel()
level
property will be used to
scale the gradient.true
if the gradient should be scaled based on level,
false
otherwisesetUseLevel(boolean)
public GradientDrawable.Orientation getOrientation()
setOrientation(Orientation)
public void setOrientation(GradientDrawable.Orientation orientation)
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 gradientmutate()
,
getOrientation()
public void setColors(int[] colors)
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.
colors
- an array containing 2 or more ARGB colorsmutate()
,
setColor(int)
public int[] getColors()
null
if the
gradient is drawn using a single color or no colors.null
setColors(int[] colors)
public void draw(Canvas canvas)
Drawable
public void setColor(int argb)
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 shapemutate()
,
setColors(int[])
,
getColor()
public void setColor(ColorStateList colorStateList)
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.
colorStateList
- The color state list used to fill the shapemutate()
,
getColor()
public ColorStateList getColor()
null
if
the shape is filled with a gradient or has no fill color.null
setColor(int)
,
setColor(ColorStateList)
protected boolean onStateChange(int[] stateSet)
Drawable
onStateChange
in class Drawable
public boolean isStateful()
Drawable
isStateful
in class Drawable
Drawable.setState(int[])
public int getChangingConfigurations()
Drawable
Drawable.setChangingConfigurations(int)
or 0 by default. Subclasses
may extend this to or in the changing configurations of any other
drawables they hold.getChangingConfigurations
in class Drawable
ActivityInfo
.ActivityInfo
public void setAlpha(int alpha)
Drawable
public int getAlpha()
Drawable
public void setDither(boolean dither)
Drawable
setDither
in class Drawable
Paint.setDither(boolean);
public ColorFilter getColorFilter()
Drawable
null
if none set.getColorFilter
in class Drawable
null
if none setpublic void setColorFilter(ColorFilter colorFilter)
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
.
setColorFilter
in class Drawable
colorFilter
- The color filter to apply, or null
to remove the
existing color filterpublic void setTintList(ColorStateList tint)
Drawable
A Drawable's drawing content will be blended together with its tint
before it is drawn to the screen. This functions similarly to
Drawable.setColorFilter(int, PorterDuff.Mode)
.
Note: Setting a color filter via
Drawable.setColorFilter(ColorFilter)
or
Drawable.setColorFilter(int, PorterDuff.Mode)
overrides tint.
setTintList
in class Drawable
tint
- Color state list to use for tinting this drawable, or
null
to clear the tintDrawable.setTint(int)
,
Drawable.setTintMode(PorterDuff.Mode)
public void setTintMode(PorterDuff.Mode tintMode)
Drawable
Defines how this drawable's tint color should be blended into the drawable
before it is drawn to screen. Default tint mode is PorterDuff.Mode#SRC_IN
.
Note: Setting a color filter via
Drawable.setColorFilter(ColorFilter)
or
Drawable.setColorFilter(int, PorterDuff.Mode)
overrides tint.
setTintMode
in class Drawable
tintMode
- A Porter-Duff blending modeDrawable.setTint(int)
,
Drawable.setTintList(ColorStateList)
public int getOpacity()
Drawable
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 Drawable.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 Drawable.setAlpha(int)
or Drawable.setColorFilter(android.graphics.ColorFilter)
methods. Some subclasses,
such as BitmapDrawable
, ColorDrawable
, and GradientDrawable
,
do account for the value of Drawable.setAlpha(int)
, but the general behavior is dependent
upon the implementation of the subclass.
getOpacity
in class Drawable
PixelFormat
protected void onBoundsChange(Rect r)
Drawable
onBoundsChange
in class Drawable
protected boolean onLevelChange(int level)
Drawable
onLevelChange
in class Drawable
public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) throws XmlPullParserException, IOException
Drawable
inflate
in class Drawable
r
- Resources used to resolve attribute valuesparser
- XML parser from which to inflate this Drawableattrs
- Base set of attribute valuestheme
- Theme to apply, may be nullXmlPullParserException
IOException
public void applyTheme(Resources.Theme t)
Drawable
applyTheme
in class Drawable
t
- the theme to applypublic boolean canApplyTheme()
canApplyTheme
in class Drawable
public int getIntrinsicWidth()
Drawable
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.
getIntrinsicWidth
in class Drawable
public int getIntrinsicHeight()
Drawable
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.
getIntrinsicHeight
in class Drawable
public Insets getOpticalInsets()
Drawable
getOpticalInsets
in class Drawable
public Drawable.ConstantState getConstantState()
Drawable
Drawable.ConstantState
instance that holds the shared state of this Drawable.getConstantState
in class Drawable
Drawable.ConstantState
,
Drawable.mutate()
public void getOutline(Outline outline)
Drawable
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.
getOutline
in class Drawable
View.setOutlineProvider(android.view.ViewOutlineProvider)
public Drawable mutate()
Drawable
mutate
in class Drawable
Drawable.ConstantState
,
Drawable.getConstantState()
public void clearMutated()
Drawable
This is hidden because only framework drawables can be cached, so custom drawables don't need to support constant state, mutate(), or clearMutated().
clearMutated
in class Drawable