55

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 8

75

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)
Sign up to request clarification or add additional context in comments.

2 Comments

work only on android 12+
Doesn't apply to the composables underneath, thus not possible to create a blur overlay. It allows only to blur content inside the composable with the modifier applied.
12

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

This works for JetBrains Compose on Desktop (using Linux here) out of the box
8

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

7

Check out Haze (A blurring modifier for Compose Multiplatform / Jetpack Compose)

enter image description here

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

3

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

Unfortunately, the GitHub repo is not available (404 Not found)
@OlehKuchura My bad the link was pointing to dev branch; which has been removed. i updated the link. you can try it now. here is the link "github.com/iZakirSheikh/toolkit"
0
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

0

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

-2

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

They asked for blurring the background. Modifier.blur() blurs the content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.