RemoteBrush · Gradients
Linear, horizontal, vertical, radial and sweep gradients.
Open the live, interactive demo →
Change its controls, read the remote document it produces and the diff after every change.
What it is
Colour gradients — straight, circular or sweeping — used to fill shapes.
Good for
- Gradient backgrounds and buttons
- Colourful progress indicators
Code
The story's source with androidx.compose.remote imports. CI compiles every snippet on this site against AndroidX.
import androidx.compose.remote.creation.compose.layout.RemoteCanvas
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteOffset
import androidx.compose.remote.creation.compose.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.height
import androidx.compose.remote.creation.compose.modifier.size
import androidx.compose.remote.creation.compose.shaders.RemoteBrush
import androidx.compose.remote.creation.compose.shaders.RemoteShaderBrush
import androidx.compose.remote.creation.compose.shaders.horizontalGradient
import androidx.compose.remote.creation.compose.shaders.linearGradient
import androidx.compose.remote.creation.compose.shaders.radialGradient
import androidx.compose.remote.creation.compose.shaders.sweepGradient
import androidx.compose.remote.creation.compose.shaders.verticalGradient
import androidx.compose.remote.creation.compose.state.RemotePaint
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.rf
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
@Composable
@RemoteComposable
fun CanvasGradients(kind: String, first: Color, second: Color) {
RemoteCanvas(modifier = RemoteModifier.size(280.rdp, 180.rdp)) {
val colors = listOf(first.rc, second.rc)
val brush: RemoteShaderBrush =
when (kind) {
"linear" ->
RemoteBrush.linearGradient(
colors,
start = RemoteOffset.Zero,
end = RemoteOffset(width, height),
)
"horizontal" -> RemoteBrush.horizontalGradient(colors)
"vertical" -> RemoteBrush.verticalGradient(colors)
"radial" ->
RemoteBrush.radialGradient(colors, center = center, radius = height / 2f.rf)
else -> RemoteBrush.sweepGradient(colors, center = center)
}
val paint = RemotePaint()
with(brush) { applyTo(paint, size) }
drawRoundRect(paint, size = size, cornerRadius = RemoteOffset(24f.rf, 24f.rf))
}
}
API
creation.compose.shaders.RemoteBrush
class RemoteBrush
void applyTo(RemoteStateScope, RemotePaint paint, RemoteSize size)
RemoteBrush.Companion Companioncreation.compose.shaders.linearGradient
RemoteShaderBrush linearGradient(RemoteBrush.Companion, List<RemoteColor> colors, optional RemoteOffset? start, optional RemoteOffset? end, optional RemoteTileMode tileMode)
RemoteShaderBrush linearGradient(RemoteBrush.Companion, Pair<RemoteFloat,RemoteColor>... colorStops, optional RemoteOffset? start, optional RemoteOffset? end, optional RemoteTileMode tileMode)creation.compose.shaders.radialGradient
RemoteShaderBrush radialGradient(RemoteBrush.Companion, List<RemoteColor> colors, optional RemoteOffset? center, optional RemoteFloat? radius, optional RemoteTileMode tileMode)
RemoteShaderBrush radialGradient(RemoteBrush.Companion, Pair<RemoteFloat,RemoteColor>... colorStops, optional RemoteOffset? center, optional RemoteFloat? radius, optional RemoteTileMode tileMode)creation.compose.shaders.sweepGradient
RemoteShaderBrush sweepGradient(RemoteBrush.Companion, List<? extends RemoteColor> colors, optional RemoteOffset? center)
RemoteShaderBrush sweepGradient(RemoteBrush.Companion, Pair<? extends RemoteFloat,? extends RemoteColor>[] colorStops, optional RemoteOffset? center)creation.compose.shaders.image
RemoteShaderBrush image(RemoteBrush.Companion, RemoteImageBitmap image, optional RemoteTileMode tileModeX, optional RemoteTileMode tileModeY, optional RemoteContentScale contentScale)