Skip to content

Instantly share code, notes, and snippets.

@cedrickring
Last active April 18, 2024 10:29
Show Gist options
  • Star 89 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save cedrickring/0497965b0658d6727aaec531f59e8c5c to your computer and use it in GitHub Desktop.
Save cedrickring/0497965b0658d6727aaec531f59e8c5c to your computer and use it in GitHub Desktop.
Draw a colored shadow in Android Jetpack Compose
/*
Copyright 2020 Cedric Kring.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import androidx.compose.ui.Modifier
import androidx.compose.ui.drawBehind
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
fun Modifier.drawColoredShadow(
color: Color,
alpha: Float = 0.2f,
borderRadius: Dp = 0.dp,
shadowRadius: Dp = 20.dp,
offsetY: Dp = 0.dp,
offsetX: Dp = 0.dp
) = this.drawBehind {
val transparentColor = android.graphics.Color.toArgb(color.copy(alpha = 0.0f).value.toLong())
val shadowColor = android.graphics.Color.toArgb(color.copy(alpha = alpha).value.toLong())
this.drawIntoCanvas {
val paint = Paint()
val frameworkPaint = paint.asFrameworkPaint()
frameworkPaint.color = transparentColor
frameworkPaint.setShadowLayer(
shadowRadius.toPx(),
offsetX.toPx(),
offsetY.toPx(),
shadowColor
)
it.drawRoundRect(
0f,
0f,
this.size.width,
this.size.height,
borderRadius.toPx(),
borderRadius.toPx(),
paint
)
}
}
@Zhuinden
Copy link

android.graphics.Color.toArgb( requires API 26+

@MarioHBS
Copy link

Amazing, thanks !

@fatemeh-afshari
Copy link

@cedrickring I use this code and it works fine in emulator, but I does not work in real devices.
I put setLayerType(View.LAYER_TYPE_SOFTWARE, null) and set android:hardwareAccelerated="false" in my manifest, but still it is not working.
Note: I have tested it in 4 different devices

@awBSH
Copy link

awBSH commented Jul 4, 2022

Hello Cedric,
you gist is used in our code and I'd like to properly document and declare it according to FOSS guidelines.
Could you please tell me under which license you provided this sample code?
Cheers, Andy 👍

@cedrickring
Copy link
Author

@fatemeh-afshari I can't say which API versions exactly are supported or what's necessary to make it work on your devices. I tested it on my Samsung Galaxy S8 on which it worked perfectly fine. I'd be happy to update the snippet if you find a solution for your problem.

@awBSH Hey Andy, I added a basic license on top of the file. Feel free to use and distribute this snippet :)

@awBSH
Copy link

awBSH commented Jul 5, 2022

@cedrickring Hi Cedric, thank you very much 👍

@tasjapr
Copy link

tasjapr commented Sep 12, 2022

android.graphics.Color.toArgb( requires API 26+

you can use this version
https://gist.github.com/tasjapr/ed66e6ba1d70cc1c6ff65ae38500a7b4

@munisifyeshwanth
Copy link

color.copy(alpha = 0.0f).toArgb()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment