public abstract class Gradient
- Object
- Gradient
ImplementsPaint
Known subtypesConicGradient, LinearGradient, RadialGradient
Abstract description of a CSS-style gradient that can be used as a
background or painted directly via Graphics#fillGradient. Three
concrete subclasses mirror the corresponding CSS functions:
LinearGradientforlinear-gradient(<angle>, <stops>)RadialGradientforradial-gradient([shape] [extent] [at pos], <stops>)ConicGradientforconic-gradient([from <angle>] [at pos], <stops>)
A Gradient is a Paint, so it can also be assigned via Graphics#setColor(Paint)
and consumed by fillRect / fillShape. The dedicated
Graphics#fillGradient(Gradient, int, int, int, int) entry point gives
the platform port the rectangle bounds up front so it can pick the
fastest native shader path (Java2D LinearGradientPaint /
RadialGradientPaint, Android LinearGradient / RadialGradient /
SweepGradient, Core Graphics CGGradient).
Subclass instances are intended to be immutable after construction;
modify via builder-style setters before handing the gradient off to
Graphics or Style. copy() produces a defensive deep clone for
places that must outlive caller mutation (e.g. async paint queues).
Fields
public static final byte KIND_LINEAR = 0 | Sentinel returned by getKind() for LinearGradient instances. |
public static final byte KIND_RADIAL = 1 | Sentinel returned by getKind() for RadialGradient instances. |
public static final byte KIND_CONIC = 2 | Sentinel returned by getKind() for ConicGradient instances. |
public static final byte CYCLE_NONE = 0 | Cycle modes mirroring MultipleGradientPaint.CycleMethod. |
public static final byte CYCLE_REPEAT = 1 | |
public static final byte CYCLE_REFLECT = 2 |
Methods
public static Gradient parseCss(String css) | Parses a CSS gradient function string and returns the corresponding Gradient subclass. |
public abstract byte getKind() | Returns one of KIND_LINEAR, KIND_RADIAL, KIND_CONIC. |
public final int[] getColors() | ARGB stop colors (length >= 2). |
public final float[] getPositions() | Stop positions in [0,1] aligned with getColors(). |
public final byte getCycleMethod() | One of CYCLE_NONE / CYCLE_REPEAT / CYCLE_REFLECT. |
public final Gradient setCycleMethod(byte cycleMethod) | Sets the cycle method. |
public final Image getCachedRaster(int width, int height) | Returns a software-rasterized image of this gradient at the requested rectangle size, reusing a weakly-cached bitmap when possible. |
protected final void invalidateRasterCache() | Drops the weak cached raster - call after any mutation that changes the painted pixels (cycle method change, subclass parameter change). |
public abstract Gradient copy() | Returns a defensive deep copy. |
public final void paint(Graphics g, Rectangle2D bounds) | Paints in the given bounds. |
public final void paint(Graphics g, double x, double y, double w, double h) | Paints in the given bounds. |
public abstract int sampleArgb(int px, int py, int width, int height) | Software-rasterizer hook used by the default port implementation when no native gradient shader is available. |
protected final int sampleStops(float t) | Samples one of the stops at fractional position t. Honors the configured cycle method. |
Inherited methods
Field details
KIND_LINEAR
public static final byte KIND_LINEAR = 0getKind() for LinearGradient instances.KIND_RADIAL
public static final byte KIND_RADIAL = 1getKind() for RadialGradient instances.KIND_CONIC
public static final byte KIND_CONIC = 2getKind() for ConicGradient instances.CYCLE_NONE
public static final byte CYCLE_NONE = 0MultipleGradientPaint.CycleMethod. Repeated as
byte constants here so that this class (and the .res serializer) does
not pull the enum across the resource format boundary.CYCLE_REPEAT
public static final byte CYCLE_REPEAT = 1CYCLE_REFLECT
public static final byte CYCLE_REFLECT = 2Method details
parseCss
public static Gradient parseCss(String css)Parses a CSS gradient function string and returns the corresponding
Gradient subclass. Supports linear-gradient, radial-gradient,
conic-gradient, and the repeating-* variants. Mirrors the syntax
accepted by the build-time CSS compiler so a string copied verbatim
from a .css file produces the same gradient at runtime.
Returns null if css is null/empty or does not look like a gradient
function call. Throws IllegalArgumentException on hard parse errors
(unknown direction, missing stops, malformed colors).
getKind
public abstract byte getKind()KIND_LINEAR, KIND_RADIAL, KIND_CONIC.getColors
public final int[] getColors()getPositions
public final float[] getPositions()getColors().getCycleMethod
public final byte getCycleMethod()CYCLE_NONE / CYCLE_REPEAT / CYCLE_REFLECT. Defaults to NONE.setCycleMethod
public final Gradient setCycleMethod(byte cycleMethod)this for chaining.getCachedRaster
public final Image getCachedRaster(int width, int height)fillGradient
override to avoid re-rasterizing on every frame; the cache is a
Display.createSoftWeakRef so the bitmap is reclaimable when idle.invalidateRasterCache
protected final void invalidateRasterCache()copy
public abstract Gradient copy()paint
public final void paint(Graphics g, Rectangle2D bounds)Parameters
gGraphics- Graphics context to paint in.
boundsRectangle2D- Bounds to paint in. User coordinates.
paint
public final void paint(Graphics g, double x, double y, double w, double h)Parameters
gGraphics- Graphics context to paint in.
xdouble- x coordinate. User space.
ydouble- y coordinate. USer space.
wdouble- width. User space.
hdouble- Hight. User space.
sampleArgb
public abstract int sampleArgb(int px, int py, int width, int height)fillGradient directly do not call this.sampleStops
protected final int sampleStops(float t)Samples one of the stops at fractional position t. Honors the configured cycle method. Shared by the three subclasses’ sampling implementations.
CSS repeating-*-gradient stops define one period from
positions[0] to positions[last], not [0, 1]. For
white 0%, red 16% the period is 0.16 of the gradient extent and
the pattern must wrap on that range; collapsing to t - floor(t)
would leak the final color across the rest of the rect.