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).
Since
8.1
-
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 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().final voidPaints in the given bounds.final voidpaint(Graphics g, Rectangle2D bounds) Paints in the given bounds.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
-
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. -
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.
-