Class GradientDrawable
Drawable
with a color gradient for buttons, backgrounds, etc.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Controls how the gradient is oriented relative to the drawable's boundsNested classes/interfaces inherited from class icyllis.modernui.graphics.drawable.Drawable
Drawable.Callback, Drawable.ConstantState
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
static final int
Gradient is linear (default.)static final int
static final int
Radius is a fraction of the base size.static final int
Radius is a fraction of the bounds size.static final int
Radius is in pixels. -
Constructor Summary
ConstructorDescriptionGradientDrawable
(GradientDrawable.Orientation orientation, int[] colors) Create a new gradient drawable given an orientation and an array of colors for the gradient. -
Method Summary
Modifier and TypeMethodDescriptionint[]
Returns the colors used to draw the gradient, ornull
if the gradient is drawn using a single color or no colors.float
Returns the X-position of the center of the gradient as a fraction of the width.float
Returns the Y-position of the center of this gradient as a fraction of the height.float
Returns the radius of the gradient in pixels.int
Returns the type of gradient used by this drawable, one ofLINEAR_GRADIENT
,RADIAL_GRADIENT
, orANGULAR_GRADIENT
.Returns the orientation of the gradient defined in this drawable.boolean
Returns whether this drawable'slevel
property will be used to scale the gradient.boolean
isDither()
Returns true if color error may be distributed to smooth color transition.mutate()
Make this drawable mutable.void
setColors
(int[] colors) Sets the colors used to draw the gradient.void
setColors
(int[] colors, float[] offsets) Sets the colors and offsets used to draw the gradient.void
setDither
(boolean dither) Sets a hint that indicates if color error may be distributed to smooth color transition.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, int type) 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
setUseLevel
(boolean useLevel) Sets whether this drawable'slevel
property will be used to scale the gradient.Methods inherited from class icyllis.modernui.graphics.drawable.ShapeDrawable
clearMutated, draw, getAlpha, getColor, getColorFilter, getConstantState, getCornerRadius, getInnerRadius, getInnerRadiusRatio, getIntrinsicHeight, getIntrinsicWidth, getPadding, getShape, getThickness, getThicknessRatio, getUseLevelForShape, hasFocusStateSpecified, isStateful, modulateAlpha, onBoundsChange, onLevelChange, onStateChange, setAlpha, setColor, setColor, setColorFilter, setCornerRadius, setInnerRadius, setInnerRadiusRatio, setPadding, setShape, setSize, setStroke, setStroke, setThickness, setThicknessRatio, setTintBlendMode, setTintList, setUseLevelForShape
Methods inherited from class icyllis.modernui.graphics.drawable.Drawable
applyTheme, canApplyTheme, copyBounds, copyBounds, getBounds, getCallback, getChangingConfigurations, getCurrent, getHotspotBounds, getLayoutDirection, getLevel, getMinimumHeight, getMinimumWidth, getState, invalidateSelf, isAutoMirrored, isVisible, jumpToCurrentState, onLayoutDirectionChanged, scaleFromDensity, scaleFromDensity, scheduleSelf, setAutoMirrored, setBounds, setBounds, setCallback, setChangingConfigurations, setHotspot, setHotspotBounds, setLayoutDirection, setLevel, setState, setTint, setVisible, unscheduleSelf
-
Field Details
-
LINEAR_GRADIENT
public static final int LINEAR_GRADIENTGradient is linear (default.)- See Also:
-
RADIAL_GRADIENT
public static final int RADIAL_GRADIENT- See Also:
-
ANGULAR_GRADIENT
public static final int ANGULAR_GRADIENT- See Also:
-
RADIUS_TYPE_PIXELS
public static final int RADIUS_TYPE_PIXELSRadius is in pixels.- See Also:
-
RADIUS_TYPE_FRACTION
public static final int RADIUS_TYPE_FRACTIONRadius is a fraction of the base size.- See Also:
-
RADIUS_TYPE_FRACTION_PARENT
public static final int RADIUS_TYPE_FRACTION_PARENTRadius is a fraction of the bounds size.- See Also:
-
-
Constructor Details
-
GradientDrawable
public GradientDrawable() -
GradientDrawable
public GradientDrawable(@NonNull GradientDrawable.Orientation orientation, @Nullable @ColorInt int[] colors) Create a new gradient drawable given an orientation and an array of colors for the gradient.
-
-
Method Details
-
setGradientType
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
- The type of the gradient:LINEAR_GRADIENT
,RADIAL_GRADIENT
orANGULAR_GRADIENT
- See Also:
-
getGradientType
public int getGradientType()Returns the type of gradient used by this drawable, one ofLINEAR_GRADIENT
,RADIAL_GRADIENT
, orANGULAR_GRADIENT
.- Returns:
- the type of gradient used by this drawable
- See Also:
-
setGradientCenter
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
- the X-position of the center of the gradienty
- the Y-position of the center of the gradient- See Also:
-
getGradientCenterX
public float getGradientCenterX()Returns the X-position of the center of the gradient as a fraction of the width.- Returns:
- the X-position of the center of the gradient
- See Also:
-
getGradientCenterY
public float getGradientCenterY()Returns the Y-position of the center of this gradient as a fraction of the height.- Returns:
- the Y-position of the center of the gradient
- See Also:
-
setGradientRadius
public void setGradientRadius(float gradientRadius, int type) Sets the radius of the gradient. The radius is honored only when the gradient type is set toRADIAL_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
- the radius of the gradienttype
- the unit of the radius- See Also:
-
getGradientRadius
Returns the radius of the gradient in pixels. The radius is valid only when the gradient type is set toRADIAL_GRADIENT
.- Returns:
- the radius of the gradient in pixels
- See Also:
-
setUseLevel
public void setUseLevel(boolean useLevel) Sets whether this drawable'slevel
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 (seegetOrientation()
)RADIAL_GRADIENT
adjusts the outer radiusANGULAR_GRADIENT
adjusts the ending angle
The default value for this property is
false
.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:
useLevel
-true
if the gradient should be scaled based on level,false
otherwise- See Also:
-
getUseLevel
public boolean getUseLevel()Returns whether this drawable'slevel
property will be used to scale the gradient.- Returns:
true
if the gradient should be scaled based on level,false
otherwise- See Also:
-
getOrientation
Returns the orientation of the gradient defined in this drawable.- Returns:
- the orientation of the gradient defined in this drawable
- See Also:
-
setOrientation
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
- the desired orientation (angle) of the gradient- See Also:
-
setColors
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
- an array containing 2 or more ARGB colors- See Also:
-
setColors
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
- an array containing 2 or more ARGB colorsoffsets
- optional array of floating point parameters representing the positions of the colors. Null evenly disperses the colors- See Also:
-
getColors
Returns the colors used to draw the gradient, ornull
if the gradient is drawn using a single color or no colors.- Returns:
- the colors used to draw the gradient, or
null
- See Also:
-
setDither
public void setDither(boolean dither) Sets a hint that indicates if color error may be distributed to smooth color transition. The default value is false. -
isDither
public boolean isDither()Returns true if color error may be distributed to smooth color transition. The default value is false. -
mutate
Description copied from class:Drawable
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.
- Overrides:
mutate
in classShapeDrawable
- Returns:
- This drawable.
- See Also:
-