Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Draw a colored shadow in Android Jetpack Compose
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
)
}
}
@iamrajshekhar

This comment has been minimized.

Copy link

@iamrajshekhar iamrajshekhar commented Mar 4, 2021

Can you please suggest me how to achieve the Inner shadow.. like neumorphism design.

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