How can I blur a background or create a Blur Overlay in Jetpack Compose? There is no documentation or resources whatsoever addressing this topic. Simply said: I'm looking to implement something like this natively in Jetpack Compose
8 Answers
This is a feature that has been requested (implemented in Compose 1.1.0-alpha03 for android 12 and above only) and which you might want to star.
The idea would be to create a blur modifier that would look like this:
Modifier.blur(radius = 16.dp)
In Jetpack Compose:1.1.0-alpha03, you can use:
Modifier.blur(30.dp)
but is only supported on Android 12 and above. Attempts to use this Modifier on older Android versions will be ignored.
Reference: Modifier.blur
1 Comment
For earlier versions than API 31 you still can use RenderScript (deprecated) or RenderScript Intrinsics Replacement Toolkit. For the first way:
val bitmap = BitmapFactory.decodeResource(
LocalContext.current.resources,
R.drawable.your_image
)
val rs = RenderScript.create(LocalContext.current)
val bitmapAlloc = Allocation.createFromBitmap(rs, bitmap)
ScriptIntrinsicBlur.create(rs, bitmapAlloc.element).apply {
setRadius(BLUR_RADIUS)
setInput(bitmapAlloc)
forEach(bitmapAlloc)
}
bitmapAlloc.copyTo(bitmap)
rs.destroy()
For the second you have to add renderscript-toolkit module to your project (look here) and then use:
val bitmap = BitmapFactory.decodeResource(
LocalContext.current.resources,
R.drawable.your_image
).let { Toolkit.blur(it, BLUR_RADIUS) }
Finally you can show blurred bitmap like this:
Image(
bitmap = bitmap.asImageBitmap(),
"some description"
)
Comments
Check out Haze (A blurring modifier for Compose Multiplatform / Jetpack Compose)
The most basic usage would be something like:
repositories {
mavenCentral()
}
dependencies {
// For Compose Multiplatform AND Jetpack Compose
implementation("dev.chrisbanes.haze:haze:0.6.0")
}
val hazeState = remember { HazeState() }
Scaffold(
topBar = {
TopAppBar(
// Need to make app bar transparent to see the content behind
colors = TopAppBarDefaults.largeTopAppBarColors(Color.Transparent),
modifier = Modifier
.hazeChild(state = hazeState)
.fillMaxWidth(),
) {
/* todo */
}
},
bottomBar = {
NavigationBar(
containerColor = Color.Transparent,
modifier = Modifier
.hazeChild(state = hazeState)
.fillMaxWidth(),
) {
/* todo */
}
},
) {
LazyVerticalGrid(
modifier = Modifier
.haze(
state = hazeState,
backgroundColor = MaterialTheme.colorScheme.surface,
),
) {
// todo
}
}
Comments
I'm the author of the Toolkit library, which contains a custom blur modifier that can blur the background behind any composable in real time. It works on any version of Android.
Example
OptIn(ExperimentalToolkitApi::class, ExperimentalMaterial3Api::class)
@Composable
fun BlurTopBar() {
TopAppBar(
title = { Text("Blur Top Bar") },
modifier = Modifier
.statusBarsPadding()
.legacyBackgroundBlur(
blurRadius = 25f, // the radius of the blur effect, in pixels
blurAlpha = 0.4f // the alpha value of the blur effect, between 0 and 1
)
.background(Color.White.copy(0.7f)) // the background color of the top bar
.noise(0.05f) // the noise level of the blur effect, between 0 and 1
)
}
I wrote an article that shows how to use my library and how to create stunning glassmorphic designs for your Android apps.
You can read it here: Blurring the Lines: How to Achieve a Glassmorphic Design with Jetpack Compose.
I hope you find my library and article helpful. If you have any feedback or suggestions, please let me know.
2 Comments
Scaffold(
modifier = Modifier
.fillMaxSize()
) {
Box(modifier = Modifier
.fillMaxSize()
.blur(10.dp)
){
AsyncImage(
modifier = Modifier.align(Alignment.Center).fillMaxSize(),
contentScale = ContentScale.FillBounds,
model =
"https://images.genius.com/225d6cb0fef34cc080a51fefb2384c8b.599x607x1.jpg",
contentDescription = "",
)
}
Column {//insert the rest of UI code here}
I was having the same problem, I tried this and it is working as Column part overlays on top of the Box. This should overlap on each other giving you a blurred background. Let me know if I am making any mistake. Hope it helps!
EDIT: Unfortunately Modifier.blur() is not backward compatible, so it will only work for Android 12+ devices.
Comments
I had a requirement to blur an image The 'Modifier.blur' is supported on Android 12 and above. I encountered problems (crashes) with some of the libraries mentioned earlier. After searching for various solutions, I ended up using this one, which works. if you are looking to blur an image use this
implementation("com.github.skydoves:landscapist-transformation:2.1.6")
implementation("com.github.skydoves:landscapist-glide:2.3.2")
add these dependecies
use it like this
GlideImage(
modifier= Modifier
.padding(2.dp)
.width(64.dp)
.height(64.dp)
.clip(RoundedCornerShape(8.dp)),
imageModel = { imageUrl },
component = rememberImageComponent {
+BlurTransformationPlugin(radius = 10) // between 0 to Int.MAX_VALUE.
}
)
This configuration applies a blur transformation with a radius of 10 to your image. Adjust the radius as needed within the valid range from 0 to Int.MAX_VALUE
Comments
Simply use blue modifier function like below.
Image(
painter = painterResource (R.drawable.my_image)
modifier = Modifier
blur (radiusX = 15.dp, radiusY = 15.dp)
)
1 Comment
Modifier.blur() blurs the content.