Class Gradient
- All Implemented Interfaces:
Paint
- Direct Known Subclasses:
ConicGradient, 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).
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byteCycle modes mirroringMultipleGradientPaint.CycleMethod.static final bytestatic final bytestatic final byteSentinel returned bygetKind()forConicGradientinstances.static final byteSentinel returned bygetKind()forLinearGradientinstances.static final byteSentinel returned bygetKind()forRadialGradientinstances. -
Method Summary
Modifier and TypeMethodDescriptionabstract Gradientcopy()Returns a defensive deep copy.final ImagegetCachedRaster(int width, int height) Returns a software-rasterized image of this gradient at the requested rectangle size, reusing a weakly-cached bitmap when possible.final int[]ARGB stop colors (length >= 2).final byteOne ofCYCLE_NONE/CYCLE_REPEAT/CYCLE_REFLECT.abstract bytegetKind()Returns one ofKIND_LINEAR,KIND_RADIAL,KIND_CONIC.final float[]Stop positions in [0,1] aligned withgetColors().protected final voidDrops the weak cached raster - call after any mutation that changes the painted pixels (cycle method change, subclass parameter change).final voidPaints in the given bounds.final voidpaint(Graphics g, Rectangle2D bounds) Paints in the given bounds.static GradientParses a CSS gradient function string and returns the correspondingGradientsubclass.abstract intsampleArgb(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 intsampleStops(float t) Samples one of the stops at fractional position t.final GradientsetCycleMethod(byte cycleMethod) Sets the cycle method.
-
Field Details
-
KIND_LINEAR
public static final byte KIND_LINEARSentinel returned bygetKind()forLinearGradientinstances.- See Also:
-
KIND_RADIAL
public static final byte KIND_RADIALSentinel returned bygetKind()forRadialGradientinstances.- See Also:
-
KIND_CONIC
public static final byte KIND_CONICSentinel returned bygetKind()forConicGradientinstances.- See Also:
-
CYCLE_NONE
public static final byte CYCLE_NONECycle modes mirroringMultipleGradientPaint.CycleMethod. Repeated as byte constants here so that this class (and the .res serializer) does not pull the enum across the resource format boundary.- See Also:
-
CYCLE_REPEAT
public static final byte CYCLE_REPEAT- See Also:
-
CYCLE_REFLECT
public static final byte CYCLE_REFLECT- See Also:
-
-
Method Details
-
parseCss
Parses a CSS gradient function string and returns the corresponding
Gradientsubclass. Supportslinear-gradient,radial-gradient,conic-gradient, and therepeating-*variants. Mirrors the syntax accepted by the build-time CSS compiler so a string copied verbatim from a.cssfile produces the same gradient at runtime.Returns null if
cssis null/empty or does not look like a gradient function call. ThrowsIllegalArgumentExceptionon hard parse errors (unknown direction, missing stops, malformed colors). -
getKind
public abstract byte getKind()Returns one ofKIND_LINEAR,KIND_RADIAL,KIND_CONIC. -
getColors
public final int[] getColors()ARGB stop colors (length >= 2). -
getPositions
public final float[] getPositions()Stop positions in [0,1] aligned withgetColors(). -
getCycleMethod
public final byte getCycleMethod()One ofCYCLE_NONE/CYCLE_REPEAT/CYCLE_REFLECT. Defaults to NONE. -
setCycleMethod
Sets the cycle method. Returnsthisfor chaining. -
getCachedRaster
Returns a software-rasterized image of this gradient at the requested rectangle size, reusing a weakly-cached bitmap when possible. Ports without a hardware shader path can call this from theirfillGradientoverride to avoid re-rasterizing on every frame; the cache is aDisplay.createSoftWeakRefso the bitmap is reclaimable when idle. -
invalidateRasterCache
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 so subclass setters can keep the cache coherent. -
copy
Returns a defensive deep copy. Implemented by each concrete subclass so async-paint queues can capture an immutable snapshot. -
paint
Description copied from interface:PaintPaints in the given bounds.
Parameters
-
g: Graphics context to paint in. -
bounds: Bounds to paint in. User coordinates.
-
-
paint
Description copied from interface:PaintPaints in the given bounds.
Parameters
-
g: Graphics context to paint in. -
x: x coordinate. User space. -
y: y coordinate. USer space. -
w: width. User space. -
h: Hight. User space.
-
-
sampleArgb
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. Samples an ARGB color for the pixel at (px, py) within a rectangle of the given width / height. Ports overridingfillGradientdirectly 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-*-gradientstops define one period frompositions[0]topositions[last], not[0, 1]. Forwhite 0%, red 16%the period is 0.16 of the gradient extent and the pattern must wrap on that range; collapsing tot - floor(t)would leak the final color across the rest of the rect.
-