How to set gradient background in Jetpack Compose
Enhance your app's UI with smooth gradients in just a few steps
While it often appears in the world of web front-ends, a gradient background can add a professional and aesthetic touch to your mobile app, making it more appealing to your users.
Step 1: Create a Gradient Brush
To implement a gradient background, we'll first create a reusable function that takes a list of colors and returns a Brush. See the code snippet below for composable code:
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
fun createGradientBrush (
colors: List < Color >,
isVertical: Boolean = true
)
: Brush {
val
endOffset = if (isVertical) {
Offset( 0f , Float .POSITIVE_INFINITY)
} else {
Offset( Float .POSITIVE_INFINITY, 0f )
}
return
Brush.linearGradient(
colors = colors,
start = Offset( 0f , 0f ),
end = endOffset,
tileMode = TileMode.Clamp
)
}
This function has two parameters:
colors
: A list of colors for gradient effects.isVertical
: A boolean indicating whether the gradient is vertical or horizontal.
Depending onisVertical
the variable, we create an end offset, and if we want a vertical gradient we useFloat.POSITIVE_INFINITY
as y offset, and if we want a horizontal gradient we use the x offset.
The initial offset of the created linear gradient Brush is (0,0). To ensure that the edge of the gradient matches the last color in the colors list, we use tileMode
TileMode.Clamp
.
Step 2: Implement the Gradient Background
Now that we have our gradient brush ready, it's time to actually place it in the sample screen. In this example, we'll create a simple Box composable and apply a gradient effect to the background. code show as below:
@Composable
fun GradientBackgroundSampleScreen () {
val
gradientColors = listOf(
Color( 0xFFFFF176 ),
Color( 0xFFFFEE58 ),
Color( 0xFFFFEB3B ),
Color( 0xFFFFD600 ),
Color( 0xFFFFC107 )
) Box(
modifier = Modifier
.fillMaxSize()
.background(
画笔= createGradientEffect(
颜色 = gradientColors,
isVertical = true
)
),
contentAlignment = Alignment.Center
) {
Text(
text = "Your screen content" ,
color = Color.Black,
fontSize = 24.sp ,
modifier = Modifier.align(Alignment.Center)
)
}
}
In this example, we create a list of colors for the yellow gradient background and pass it to createGradientEffect()
the function. Next, we background(brush = gradientColors)
apply the gradient to our Box component using modifiers. Finally, we added some text to the center of the screen to demonstrate the effect of the gradient background.
Now, when you run your application, you should see a nice smooth gradient background on the screen!
in conclusion
Adding gradient backgrounds to our app screens has never been easier and faster with Jetpack Compose.
The days when we had to create our own shape drawables using legacy XML systems to achieve the same goal are over.
By creating reusable gradient functions, you can easily implement gradient backgrounds to enhance the visual appeal of your Android applications.