Class GradientDrawable
Drawable with a color gradient for buttons, backgrounds, etc.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumControls 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
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intGradient is linear (default.)static final intstatic final intRadius is a fraction of the base size.static final intRadius is a fraction of the bounds size.static final intRadius is in pixels. -
Constructor Summary
ConstructorsConstructorDescriptionGradientDrawable(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, ornullif the gradient is drawn using a single color or no colors.floatReturns the X-position of the center of the gradient as a fraction of the width.floatReturns the Y-position of the center of this gradient as a fraction of the height.floatReturns the radius of the gradient in pixels.intReturns 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.booleanReturns whether this drawable'slevelproperty will be used to scale the gradient.booleanisDither()Returns true if color error may be distributed to smooth color transition.mutate()Make this drawable mutable.voidsetColors(int[] colors) Sets the colors used to draw the gradient.voidsetColors(int[] colors, float[] offsets) Sets the colors and offsets used to draw the gradient.voidsetDither(boolean dither) Sets a hint that indicates if color error may be distributed to smooth color transition.voidsetGradientCenter(float x, float y) Sets the position of the center of the gradient as a fraction of the width and height.voidsetGradientRadius(float gradientRadius, int type) Sets the radius of the gradient.voidsetGradientType(int gradient) Sets the type of gradient used by this drawable.voidsetOrientation(GradientDrawable.Orientation orientation) Sets the orientation of the gradient defined in this drawable.voidsetUseLevel(boolean useLevel) Sets whether this drawable'slevelproperty will be used to scale the gradient.Methods inherited from class icyllis.modernui.graphics.drawable.ShapeDrawable
clearMutated, draw, getAlpha, getColor, getColorFilter, getConstantState, getCornerRadii, getCornerRadius, getInnerRadius, getInnerRadiusRatio, getIntrinsicHeight, getIntrinsicWidth, getOutline, getPadding, getShape, getThickness, getThicknessRatio, getUseLevelForShape, hasFocusStateSpecified, isStateful, modulateAlpha, onBoundsChange, onLevelChange, onStateChange, setAlpha, setColor, setColor, setColorFilter, setCornerRadii, setCornerRadius, setInnerRadius, setInnerRadiusRatio, setPadding, setShape, setSize, setStroke, setStroke, setThickness, setThicknessRatio, setTintBlendMode, setTintList, setUseLevelForShapeMethods inherited from class icyllis.modernui.graphics.drawable.Drawable
applyTheme, canApplyTheme, copyBounds, copyBounds, getBounds, getCallback, getChangingConfigurations, getCurrent, getDirtyBounds, 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_GRADIENTorANGULAR_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'slevelproperty 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_GRADIENTadjusts the ending position along the gradient's axis of orientation (seegetOrientation())RADIAL_GRADIENTadjusts the outer radiusANGULAR_GRADIENTadjusts 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-trueif the gradient should be scaled based on level,falseotherwise- See Also:
-
getUseLevel
public boolean getUseLevel()Returns whether this drawable'slevelproperty will be used to scale the gradient.- Returns:
trueif the gradient should be scaled based on level,falseotherwise- 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, ornullif 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:DrawableMake 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:
mutatein classShapeDrawable- Returns:
- This drawable.
- See Also:
-